diff --git a/server/src/infra/subscribers/asset.subscriber.ts b/server/src/infra/subscribers/asset.subscriber.ts index 496d28744..bc3326ec7 100644 --- a/server/src/infra/subscribers/asset.subscriber.ts +++ b/server/src/infra/subscribers/asset.subscriber.ts @@ -13,21 +13,36 @@ export class AssetAudit implements EntitySubscriberInterface { this.assetEntity = entity; } - afterInsert(event: InsertEvent): void | Promise { - console.log('AFTER ENTITY INSERTED: ', event.entity); + async afterInsert(event: InsertEvent): Promise { + const auditRepository = event.manager.getRepository(AuditEntity); + const auditEntity = this.getAssetAudit(DatabaseAction.CREATE); + + await auditRepository.save(auditEntity); } - afterRemove(event: RemoveEvent): void | Promise { - console.log('AFTER ENTITY WITH ID ' + event.entityId + ' REMOVED: ', event.entity); + async afterRemove(event: RemoveEvent): Promise { + const auditRepository = event.manager.getRepository(AuditEntity); + const auditEntity = this.getAssetAudit(DatabaseAction.DELETE); + + await auditRepository.save(auditEntity); } async afterUpdate(event: UpdateEvent): Promise { - const audit = event.manager.getRepository(AuditEntity); + const auditRepository = event.manager.getRepository(AuditEntity); + const auditEntity = this.getAssetAudit(DatabaseAction.UPDATE); + + await auditRepository.save(auditEntity); + } + + private getAssetAudit(action: DatabaseAction): AuditEntity { const auditEntity = new AuditEntity(); - auditEntity.action = DatabaseAction.UPDATE; + auditEntity.action = action; auditEntity.entityType = EntityType.ASSET; + auditEntity.entityId = this.assetEntity.id; + auditEntity.ownerId = this.assetEntity.ownerId; + auditEntity.userId = this.assetEntity.ownerId; - console.log('AFTER ENTITY UPDATED: ', this.assetEntity.isFavorite); + return auditEntity; } }