Skip to content

Commit

Permalink
sourcing-data events and view
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Dec 19, 2024
1 parent f3ca811 commit c38979c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
6 changes: 3 additions & 3 deletions api/src/modules/api-events/api-event.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export enum API_EVENT_KINDS {
user__passwordResetTokenGenerated__v1alpha1 = 'user.passwordResetTokenGenerated/v1alpha1',
user__passwordResetSucceeded__v1alpha1 = 'user.passwordResetSucceeded/v1alpha1',
user__passwordResetFailed__v1alpha1 = 'user.passwordResetFailed/v1alpha1',
data__importStarted__v1alpha1 = 'data.importStarted/v1alpha1',
data__importFailed__v1alpha1 = 'data.importFailed/v1alpha1',
data__importSucceeded__v1alpha1 = 'data.importSucceeded/v1alpha1',
sourcing_data__importStarted__v1alpha1 = 'sourcing-data.importStarted/v1alpha1',
sourcing_data__importFailed__v1alpha1 = 'sourcing-data.importFailed/v1alpha1',
sourcing_data__importSucceeded__v1alpha1 = 'sourcing-data.importSucceeded/v1alpha1',
}

/**
Expand Down
2 changes: 2 additions & 0 deletions api/src/modules/api-events/api-events.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'modules/api-events/api-event.topic+kind.entity';
import { ApiEventsController } from 'modules/api-events/api-events.controller';
import { ApiEventsService } from 'modules/api-events/api-events.service';
import { SourcingDataImportViewEntity } from './sourcing-data-import-view.entity';

export const logger: Logger = new Logger('ApiEvents');

Expand All @@ -18,6 +19,7 @@ export const logger: Logger = new Logger('ApiEvents');
ApiEvent,
LatestApiEventByTopicAndKind,
FirstApiEventByTopicAndKind,
SourcingDataImportViewEntity,
]),
],
providers: [ApiEventsService],
Expand Down
11 changes: 11 additions & 0 deletions api/src/modules/api-events/sourcing-data-import-view.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ViewEntity } from 'typeorm';
import { ApiEventByTopicAndKind } from './api-event.topic+kind.entity';

@ViewEntity({
name: 'sourcing_data_import_view',
expression: `SELECT topic, timestamp, kind, data
FROM api_events
WHERE kind::TEXT LIKE '%sourcing-data%'
ORDER BY topic, kind, timestamp DESC;`,
})
export class SourcingDataImportViewEntity extends ApiEventByTopicAndKind {}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { EventsHandler, IEvent, IEventHandler } from '@nestjs/cqrs';
import { API_EVENT_KINDS } from 'modules/api-events/api-event.entity';
import { API_EVENT_KINDS, ApiEvent } from 'modules/api-events/api-event.entity';
import { ApiEventsService } from 'modules/api-events/api-events.service';
import { Task } from 'modules/tasks/task.entity';

export enum IMPORT_DATA_EVENTS {
STARTED = API_EVENT_KINDS.data__importStarted__v1alpha1,
FAILED = API_EVENT_KINDS.data__importFailed__v1alpha1,
SUCCEED = API_EVENT_KINDS.data__importSucceeded__v1alpha1,
STARTED = API_EVENT_KINDS.sourcing_data__importStarted__v1alpha1,
FAILED = API_EVENT_KINDS.sourcing_data__importFailed__v1alpha1,
SUCCEED = API_EVENT_KINDS.sourcing_data__importSucceeded__v1alpha1,
}

export class ImportDataEvent implements IEvent {
constructor(
public readonly taskId: Task['id'],
public readonly kind: IMPORT_DATA_EVENTS,
public readonly payload: any,
public readonly data: any,
) {}
}

Expand All @@ -22,10 +22,11 @@ export class ImportDataEventHandler implements IEventHandler<ImportDataEvent> {
constructor(private readonly apiEvents: ApiEventsService) {}

async handle(event: ImportDataEvent): Promise<void> {
const { taskId, kind, data } = event;
await this.apiEvents.create({
topic: event.taskId,
kind: event.kind as unknown as API_EVENT_KINDS,
data: event.payload,
topic: taskId,
kind: kind as unknown as API_EVENT_KINDS,
data: data,
});
}
}
6 changes: 5 additions & 1 deletion api/src/modules/import-data/workers/import-data.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ export class ImportDataConsumer {
});
this.importSocket.emitImportFailureToSocket({ error: err });
this.eventBus.publish(
new ImportDataEvent(task.id, IMPORT_DATA_EVENTS.FAILED, err),
new ImportDataEvent(task.id, IMPORT_DATA_EVENTS.FAILED, {
error: { message: err.message, stack: err.stack },
}),
);

this.logger.error(
`Import Failed for file: ${job.data.xlsxFileData.filename} for task: ${task.id}: ${err}`,
);

// TODO: If the error is not related to the file, we should not send an error report (as it will be empty), we should send a generic error message
// Since we are registering api events for the import now, it might be useful to include the id of the event so that the client
// can share it with the support team

const errorReport: string = await this.tasksService.getTaskErrorReport(
task.id,
Expand Down

0 comments on commit c38979c

Please sign in to comment.