All files / apps/data-refreshments/src main.ts

0% Statements 0/54
0% Branches 0/1
0% Functions 0/1
0% Lines 0/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                                                                             
import '@amalia/kernel/config/server';

import './catch-error';

import { Logger, type INestApplicationContext } from '@nestjs/common';

import { assert } from '@amalia/ext/typescript';
import { bootstrapCloudRunPubsubTarget } from '@amalia/kernel/bootstrap/server';
import {
  MessageTasks,
  type QueueMessageAttributes,
  type QueueMessageContent,
  type RefreshmentExecutePayload,
} from '@amalia/kernel/queue/core';

import { AppModule } from './app/app.module';
import { configuration } from './configuration';
import { RefreshmentExecuteHandler } from './refreshments-execute/sync/handlers/refreshment-execute.handler';

const executeDataRefreshmentTasks = async (
  app: INestApplicationContext,
  attributes: QueueMessageAttributes<MessageTasks>,
  messageContent: QueueMessageContent,
) => {
  switch (attributes.taskIdentifier) {
    case MessageTasks.EXECUTE_REFRESHMENT:
      assert(messageContent?.['refreshmentEvent'], 'Refreshment event not specified.');
      await app
        .select(AppModule)
        .get(RefreshmentExecuteHandler)
        .handle(messageContent['refreshmentEvent'] as RefreshmentExecutePayload);
      break;

    default:
      // Will not handle any task.
      return false;
  }

  // Task have been handled.
  return true;
};

bootstrapCloudRunPubsubTarget({
  module: AppModule,
  appConfig: configuration.apps.dataRefreshments,
  topicName: configuration.queue.topicName,
  messageHandlers: [executeDataRefreshmentTasks],
}).catch((error) => {
  // Loggers may not be ready yet.
  // eslint-disable-next-line no-console
  console.error('Error at application startup', error);
  Logger.flush();
  process.exit(1);
});