mautic

 avatar
unknown
plain_text
5 months ago
5.3 kB
2
Indexable
#!/usr/bin/env node

import pApi from '../dist/api.js'
import { Writable } from 'stream'
import { appendFile } from 'fs/promises'
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import { fromEvent } from 'promise-toolbox'
import { load as loadconfig } from 'app-conf'
import * as Utils from '@vates/www-xo-utils'
import o from '../dist/order-lib.js'
import dayjs from 'dayjs'
import fse from 'fs-extra'

const { default: OrderLib } = o
const { default: Api } = pApi

const appName = 'www-xo'
const MAUTIC_DATE_FORMAT = 'YYYY/MM/DD HH:MM'
const TRIAL_TYPES = {
  STARTER: 'starter',
  ENTERPRISE: 'enterprise',
  PREMIUM: 'premium',
}
const ERRORS_FOLDER_PATH = './mautic-pile'

export function formatDate(timestamp) {
  return moment(timestamp).format(MAUTIC_DATE_FORMAT)
}

export function getStarterTrials(trials) {
  return trials.filter((t) => t.productId === TRIAL_TYPES.STARTER)
}

export function getEnterpriseTrials(trials) {
  return trials.filter((t) => t.productId === TRIAL_TYPES.ENTERPRISE)
}

export function getPremiumTrials(trials) {
  return trials.filter((t) => t.productId === TRIAL_TYPES.PREMIUM)
}

export function getLatestTrial(trials) {
  return trials.sort((a, b) => b.end - a.end)[0]
}

async function main(args) {
  const output = args.pop()
  const __dirname = dirname(fileURLToPath(import.meta.url))
  const config = await loadconfig(appName, { appDir: dirname(__dirname) })
  const api = new Api(config)

  const readAccounts = api.db.createReadStream({
    gt: 'a_',
    lt: 'a`',
    values: true,
    keys: false,
  })
  let data = []
  for await (const account of readAccounts) {
    if (account.type === 'customer' && !account.deleted) {
      data.push({
        email: account.email,
        firstname: account.billingInfo?.firstName,
      })
    }
  }

  let n = 0
  for (let i = 0; i < data.length; i += 50) {
    const chunk = data.slice(i, i + 50  )
    await fse.outputJson(`${output}/accounts${n}`, chunk)
    n++
  }

  // const writeMetrics = new Writable({
  //   objectMode: true,
  //   write: async function (chunk, encoding, cb) {
  //     const fileData = fs.readFileSync('data.json');
  //     const fileJsonData = JSON.parse(fileData);
  //     if (chunk.type === 'customer' && !chunk.deleted) {
  //       const data = [
  //         'email',
  //         chunk.email,
  //         'firstname',
  //         chunk.billingInfo?.firstName,
  //         'lastname',
  //         chunk.billingInfo?.lastName,
  //         'company',
  //         chunk.billingInfo?.company,
  //         'address1',
  //         chunk.billingInfo?.street,
  //         'address2',
  //         chunk.billingInfo?.address2,
  //         'zipcode',
  //         chunk.billingInfo?.zip,
  //         'city',
  //         chunk.billingInfo?.city,
  //         'donotcontact',
  //         (chunk.preferences && chunk.preferences.nomail) || false,
  //         // 'starter_trial_start',
  //         // (getStarterTrials(trials).length > 0 &&
  //         //   formatDate(getLatestTrial(getStarterTrials(trials)).start)) ||
  //         //   undefined,
  //         // 'starter_trial_end',
  //         // (getStarterTrials(trials).length > 0 &&
  //         //   formatDate(getLatestTrial(getStarterTrials(trials)).end)) ||
  //         //   undefined,
  //         // 'starter_trial_extended'(
  //         //   (getStarterTrials(trials).length > 0 &&
  //         //     getLatestTrial(getStarterTrials(trials)).extended) ||
  //         //     undefined,
  //         //   'enterprise_trial_start',
  //         //   getEnterpriseTrials(trials).length > 0 &&
  //         //     formatDate(getLatestTrial(getEnterpriseTrials(trials)).start),
  //         // ) || undefined,
  //         // 'enterprise_trial_end',
  //         // (getEnterpriseTrials(trials).length > 0 &&
  //         //   formatDate(getLatestTrial(getEnterpriseTrials(trials)).end)) ||
  //         //   undefined,
  //         // 'enterprise_trial_extended',
  //         // (getEnterpriseTrials(trials).length > 0 &&
  //         //   getLatestTrial(getEnterpriseTrials(trials)).extended) ||
  //         //   undefined,
  //         // 'premium_trial_start',
  //         // (getPremiumTrials(trials).length > 0 &&
  //         //   formatDate(getLatestTrial(getPremiumTrials(trials)).start)) ||
  //         //   undefined,
  //         // 'premium_trial_end',
  //         // (getPremiumTrials(trials).length > 0 &&
  //         //   formatDate(getLatestTrial(getPremiumTrials(trials)).end)) ||
  //         //   undefined,
  //         // 'premium_trial_extended',
  //         // (getPremiumTrials(trials).length > 0 &&
  //         //   getLatestTrial(getPremiumTrials(trials)).extended) ||
  //         //   undefined,
  //         // 'end_user',
  //         // Boolean(
  //         //   account.role &&
  //         //     account.role.endUser &&
  //         //     account.role.endUser.status === 'granted',
  //         // ),
  //         // 'creation_date',
  //         // formatDate(account.signupDate),
  //       ]

  //       await appendFile(`${output}/accounts`, `${data.join(`\0`)}\n`)
  //     }
  //     cb()
  //   },
  // })
  // readAccounts.pipe(writeMetrics)
  // await fromEvent(readAccounts, 'end')
  console.log('mautic.js done')
}

main(process.argv.slice(2))
Editor is loading...
Leave a Comment