|
| 1 | +import commandLineArgs from 'command-line-args' |
| 2 | +import commandLineUsage from 'command-line-usage' |
| 3 | +import * as fs from 'fs' |
| 4 | +import moment from 'moment' |
| 5 | +import path from 'path' |
| 6 | +import { createServiceLogger } from '../../utils/logging' |
| 7 | +import SequelizeRepository from '../../database/repositories/sequelizeRepository' |
| 8 | +import { timeout } from '../../utils/timing' |
| 9 | +import { sendNodeWorkerMessage } from '../../serverless/utils/nodeWorkerSQS' |
| 10 | +import { NodeWorkerMessageType } from '../../serverless/types/workerTypes' |
| 11 | +import { NodeWorkerMessageBase } from '../../types/mq/nodeWorkerMessageBase' |
| 12 | +import WeeklyAnalyticsEmailsHistoryRepository from '../../database/repositories/weeklyAnalyticsEmailsHistoryRepository' |
| 13 | + |
| 14 | +const banner = fs.readFileSync(path.join(__dirname, 'banner.txt'), 'utf8') |
| 15 | + |
| 16 | +const log = createServiceLogger() |
| 17 | + |
| 18 | +const options = [ |
| 19 | + { |
| 20 | + name: 'tenant', |
| 21 | + alias: 't', |
| 22 | + type: String, |
| 23 | + description: 'The unique ID of tenant that you would like to send weekly emails to.', |
| 24 | + }, |
| 25 | + { |
| 26 | + name: 'help', |
| 27 | + alias: 'h', |
| 28 | + type: Boolean, |
| 29 | + description: 'Print this usage guide.', |
| 30 | + }, |
| 31 | +] |
| 32 | +const sections = [ |
| 33 | + { |
| 34 | + content: banner, |
| 35 | + raw: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + header: 'Send weekly analytics email to given tenant.', |
| 39 | + content: |
| 40 | + 'Sends weekly analytics email to given tenant. The daterange will be from previous week.', |
| 41 | + }, |
| 42 | + { |
| 43 | + header: 'Options', |
| 44 | + optionList: options, |
| 45 | + }, |
| 46 | +] |
| 47 | + |
| 48 | +const usage = commandLineUsage(sections) |
| 49 | +const parameters = commandLineArgs(options) |
| 50 | + |
| 51 | +if (parameters.help || !parameters.tenant) { |
| 52 | + console.log(usage) |
| 53 | +} else { |
| 54 | + setImmediate(async () => { |
| 55 | + const options = await SequelizeRepository.getDefaultIRepositoryOptions() |
| 56 | + const tenantIds = parameters.tenant.split(',') |
| 57 | + const weekOfYear = moment().utc().startOf('isoWeek').subtract(7, 'days').isoWeek().toString() |
| 58 | + const waeRepository = new WeeklyAnalyticsEmailsHistoryRepository(options) |
| 59 | + |
| 60 | + for (const tenantId of tenantIds) { |
| 61 | + const tenant = await options.database.tenant.findByPk(tenantId) |
| 62 | + const isEmailAlreadySent = |
| 63 | + (await waeRepository.findByWeekOfYear(tenantId, weekOfYear)) !== null |
| 64 | + |
| 65 | + if (!tenant) { |
| 66 | + log.error({ tenantId }, 'Tenant not found! Skipping.') |
| 67 | + } else if (isEmailAlreadySent) { |
| 68 | + log.info( |
| 69 | + { tenantId }, |
| 70 | + 'Analytics email for this week is already sent to this tenant. Skipping.', |
| 71 | + ) |
| 72 | + } else { |
| 73 | + log.info({ tenantId }, `Tenant found - sending weekly email message!`) |
| 74 | + await sendNodeWorkerMessage(tenant.id, { |
| 75 | + type: NodeWorkerMessageType.NODE_MICROSERVICE, |
| 76 | + tenant: tenant.id, |
| 77 | + service: 'weekly-analytics-emails', |
| 78 | + } as NodeWorkerMessageBase) |
| 79 | + |
| 80 | + if (tenantIds.length > 1) { |
| 81 | + await timeout(1000) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + process.exit(0) |
| 87 | + }) |
| 88 | +} |
0 commit comments