diff --git a/server/src/domain/database-subscriber/asset.subscriber.ts b/server/src/domain/database-subscriber/asset.subscriber.ts deleted file mode 100644 index 9b674e8b5..000000000 --- a/server/src/domain/database-subscriber/asset.subscriber.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { AssetEntity } from '@app/infra/entities'; -import { EntitySubscriberInterface, EventSubscriber, InsertEvent, RemoveEvent, UpdateEvent } from 'typeorm'; - -@EventSubscriber() -export class AssetAudit implements EntitySubscriberInterface { - listenTo() { - return AssetEntity; - } - - afterInsert(event: InsertEvent): void | Promise { - console.log('AFTER ENTITY INSERTED: ', event.entity); - } - - afterRemove(event: RemoveEvent): void | Promise { - console.log('AFTER ENTITY WITH ID ' + event.entityId + ' REMOVED: ', event.entity); - } - - afterUpdate(event: UpdateEvent): void | Promise { - console.log('AFTER ENTITY UPDATED: ', event.entity); - } -} diff --git a/server/src/infra/database.config.ts b/server/src/infra/database.config.ts index 9c028056d..089fd6878 100644 --- a/server/src/infra/database.config.ts +++ b/server/src/infra/database.config.ts @@ -17,7 +17,7 @@ export const databaseConfig: PostgresConnectionOptions = { entities: [__dirname + '/entities/*.entity.{js,ts}'], synchronize: false, migrations: [__dirname + '/migrations/*.{js,ts}'], - subscribers: [__dirname + '/../domain/database-subscriber/*.{js,ts}'], + subscribers: [__dirname + '/subscribers/*.{js,ts}'], migrationsRun: true, connectTimeoutMS: 10000, // 10 seconds ...urlOrParts, diff --git a/server/src/infra/entities/audit.entity.ts b/server/src/infra/entities/audit.entity.ts index 84f5d6888..5ef553c21 100644 --- a/server/src/infra/entities/audit.entity.ts +++ b/server/src/infra/entities/audit.entity.ts @@ -13,7 +13,7 @@ export enum EntityType { @Entity('audit') export class AuditEntity { @PrimaryGeneratedColumn('increment') - id!: string; + id!: number; @Column() entityType!: EntityType; @@ -25,7 +25,7 @@ export class AuditEntity { action!: DatabaseAction; @Column() - onwerId!: string; + ownerId!: string; @Column() userId!: string; diff --git a/server/src/infra/migrations/1691490366889-AddAuditTable1.ts b/server/src/infra/migrations/1691490366889-AddAuditTable1.ts new file mode 100644 index 000000000..aecf7df99 --- /dev/null +++ b/server/src/infra/migrations/1691490366889-AddAuditTable1.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddAuditTable11691490366889 implements MigrationInterface { + name = 'AddAuditTable11691490366889' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "audit" RENAME COLUMN "onwerId" TO "ownerId"`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "audit" RENAME COLUMN "ownerId" TO "onwerId"`); + } + +} diff --git a/server/src/infra/subscribers/asset.subscriber.ts b/server/src/infra/subscribers/asset.subscriber.ts new file mode 100644 index 000000000..496d28744 --- /dev/null +++ b/server/src/infra/subscribers/asset.subscriber.ts @@ -0,0 +1,33 @@ +import { EntitySubscriberInterface, EventSubscriber, InsertEvent, LoadEvent, RemoveEvent, UpdateEvent } from 'typeorm'; +import { AssetEntity, AuditEntity, DatabaseAction, EntityType } from '../entities'; + +@EventSubscriber() +export class AssetAudit implements EntitySubscriberInterface { + private assetEntity: AssetEntity = new AssetEntity(); + + listenTo() { + return AssetEntity; + } + + afterLoad(entity: AssetEntity, event?: LoadEvent | undefined): void | Promise { + this.assetEntity = entity; + } + + afterInsert(event: InsertEvent): void | Promise { + console.log('AFTER ENTITY INSERTED: ', event.entity); + } + + afterRemove(event: RemoveEvent): void | Promise { + console.log('AFTER ENTITY WITH ID ' + event.entityId + ' REMOVED: ', event.entity); + } + + async afterUpdate(event: UpdateEvent): Promise { + const audit = event.manager.getRepository(AuditEntity); + const auditEntity = new AuditEntity(); + + auditEntity.action = DatabaseAction.UPDATE; + auditEntity.entityType = EntityType.ASSET; + + console.log('AFTER ENTITY UPDATED: ', this.assetEntity.isFavorite); + } +}