From 2d8c914e8a14149ad4f3e6d8f394e16afe8a8f08 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 31 Dec 2024 12:14:16 +0100 Subject: [PATCH] emit event correctie --- backend/src/controllers/clients.ts | 4 ++-- backend/src/controllers/config.ts | 4 ++-- backend/src/controllers/consultants.ts | 4 ++-- backend/src/controllers/invoices.ts | 9 +++++++-- backend/src/controllers/projects.ts | 6 +++--- backend/src/controllers/projectsMonth.ts | 8 ++++---- backend/src/controllers/user.ts | 8 ++++---- 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/backend/src/controllers/clients.ts b/backend/src/controllers/clients.ts index b3f32611..41f4cbc4 100644 --- a/backend/src/controllers/clients.ts +++ b/backend/src/controllers/clients.ts @@ -33,7 +33,7 @@ export const saveClient = async (req: ConfacRequest, res: Response) => { await saveAudit(req, 'client', originalClient, client); const clientResponse = {_id, ...client}; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityUpdated, entityType: CollectionNames.CLIENTS, entityId: _id, entity: clientResponse }); + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.CLIENTS, _id, clientResponse); return res.send(clientResponse); } @@ -42,6 +42,6 @@ export const saveClient = async (req: ConfacRequest, res: Response) => { client.audit = createAudit(req.user); const inserted = await req.db.collection(CollectionNames.CLIENTS).insertOne(client); const [createdClient] = inserted.ops; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.CLIENTS, entityId: createdClient._id, entity: createdClient }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.CLIENTS, createdClient._id, createdClient); return res.send(createdClient); }; diff --git a/backend/src/controllers/config.ts b/backend/src/controllers/config.ts index 7aad716d..5368712c 100644 --- a/backend/src/controllers/config.ts +++ b/backend/src/controllers/config.ts @@ -43,13 +43,13 @@ export const saveCompanyConfig = async (req: ConfacRequest, res: Response) => { await saveAudit(req, 'config', originalConfig, config); const responseConfig = {_id, ...config}; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityUpdated, entityType: CollectionNames.CONFIG, entityId: _id, entity: responseConfig }); + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.CONFIG, _id, responseConfig); return res.send(responseConfig); } const inserted = await req.db.collection(CollectionNames.CONFIG).insertOne(config); const responseConfig = inserted.ops[0]; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.CONFIG, entityId: responseConfig._id, entity: responseConfig }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.CONFIG, responseConfig._id, responseConfig); return res.send(responseConfig); }; diff --git a/backend/src/controllers/consultants.ts b/backend/src/controllers/consultants.ts index faae96b9..4136fc74 100644 --- a/backend/src/controllers/consultants.ts +++ b/backend/src/controllers/consultants.ts @@ -22,7 +22,7 @@ export const saveConsultant = async (req: ConfacRequest, res: Response) => { await saveAudit(req, 'consultant', originalConsultant, consultant); const responseConsultant = {_id, ...consultant}; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityUpdated, entityType: CollectionNames.CONSULTANTS, entityId: _id, entity: responseConsultant }); + emitEntityEvent(SocketEventTypes.EntityUpdated, CollectionNames.CONSULTANTS, _id, responseConsultant); return res.send(responseConsultant); } @@ -34,6 +34,6 @@ export const saveConsultant = async (req: ConfacRequest, res: Response) => { audit: createAudit(req.user), }); const [createdConsultant] = inserted.ops; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.CONSULTANTS, entityId: createdConsultant._id, entity: createdConsultant }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.CONSULTANTS, createdConsultant._id, createdConsultant); return res.send(createdConsultant); }; diff --git a/backend/src/controllers/invoices.ts b/backend/src/controllers/invoices.ts index 5b9a5b28..3f590ff2 100644 --- a/backend/src/controllers/invoices.ts +++ b/backend/src/controllers/invoices.ts @@ -119,7 +119,9 @@ export const createInvoiceController = async (req: ConfacRequest, res: Response) const projectMonthId = new ObjectID(invoice.projectMonth.projectMonthId); const {updatedInvoice, updatedProjectMonth} = await moveProjectMonthAttachmentsToInvoice(createdInvoice, projectMonthId, req.db); emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.INVOICES, updatedInvoice!._id, updatedInvoice); - emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.PROJECTS_MONTH, updatedProjectMonth!._id, updatedProjectMonth); + if (updatedProjectMonth) { + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.PROJECTS_MONTH, updatedProjectMonth!._id, updatedProjectMonth); + } return res.send(updatedInvoice); } @@ -223,10 +225,13 @@ export const deleteInvoiceController = async (req: ConfacRequest, res: Response) const projectMonthCollection = req.db.collection(CollectionNames.PROJECTS_MONTH); const attachments = invoice.attachments.filter(a => a.type !== 'pdf'); + const projectMonthId = new ObjectID(invoice.projectMonth.projectMonthId); const updateProjectMonthResult = await projectMonthCollection.findOneAndUpdate({ _id: projectMonthId }, { $set: { attachments } }); const updatedProjectMonth = updateProjectMonthResult.value; - emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.PROJECTS_MONTH, updatedProjectMonth!._id, updatedProjectMonth); + if (updatedProjectMonth) { + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.PROJECTS_MONTH, updatedProjectMonth._id, updatedProjectMonth); + } } await req.db.collection(CollectionNames.INVOICES).findOneAndDelete({ _id: new ObjectID(invoiceId) }); diff --git a/backend/src/controllers/projects.ts b/backend/src/controllers/projects.ts index 802e4e97..e2c1696e 100644 --- a/backend/src/controllers/projects.ts +++ b/backend/src/controllers/projects.ts @@ -46,7 +46,7 @@ export const saveProject = async (req: ConfacRequest, res: Response) => { const {value: originalProject} = await projectsColl.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: project}, {returnOriginal: true}); await saveAudit(req, 'project', originalProject, project); const responseProject = {_id, ...project}; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityUpdated, entityType: CollectionNames.PROJECTS, entityId: _id, entity: responseProject }); + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.PROJECTS, _id, responseProject); return res.send(responseProject); } @@ -55,7 +55,7 @@ export const saveProject = async (req: ConfacRequest, res: Response) => { audit: createAudit(req.user), }); const [createdProject] = inserted.ops; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.PROJECTS, entityId: createdProject._id, entity: createdProject }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.PROJECTS, createdProject._id, createdProject); return res.send(createdProject); }; @@ -63,6 +63,6 @@ export const saveProject = async (req: ConfacRequest, res: Response) => { export const deleteProject = async (req: ConfacRequest, res: Response) => { const id = req.body.id; await req.db.collection(CollectionNames.PROJECTS).findOneAndDelete({_id: new ObjectID(id)}); - emitEntityEvent({ req, eventType: SocketEventTypes.EntityDeleted, entityType: CollectionNames.PROJECTS, entityId: id, entity: null }); + emitEntityEvent(req, SocketEventTypes.EntityDeleted, CollectionNames.PROJECTS, id, null); return res.send(id); }; diff --git a/backend/src/controllers/projectsMonth.ts b/backend/src/controllers/projectsMonth.ts index 8054bdf6..81314639 100644 --- a/backend/src/controllers/projectsMonth.ts +++ b/backend/src/controllers/projectsMonth.ts @@ -56,7 +56,7 @@ export const createProjectsMonthController = async (req: ConfacRequest, res: Res return createdProjectMonth; })); - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.PROJECTS_MONTH, entityId: null, entity: createdProjectsMonth }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.PROJECTS_MONTH, null, createdProjectsMonth ); return res.send(createdProjectsMonth); }; @@ -72,7 +72,7 @@ export const patchProjectsMonthController = async (req: ConfacRequest, res: Resp const {value: originalProjectMonth} = await projMonthCollection.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: projectMonth}, {returnOriginal: true}); await saveAudit(req, 'projectMonth', originalProjectMonth, projectMonth); const projectMonthResponse = {_id, ...projectMonth}; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityUpdated, entityType: CollectionNames.PROJECTS_MONTH, entityId: projectMonthResponse._id, entity: projectMonthResponse }); + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.PROJECTS_MONTH, projectMonthResponse._id, projectMonthResponse); return res.send(projectMonthResponse); } @@ -81,7 +81,7 @@ export const patchProjectsMonthController = async (req: ConfacRequest, res: Resp audit: createAudit(req.user), }); const [createdProjectMonth] = inserted.ops; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.PROJECTS_MONTH, entityId: createdProjectMonth._id, entity: createdProjectMonth }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.PROJECTS_MONTH, createdProjectMonth._id, createdProjectMonth); return res.send(createdProjectMonth); }; @@ -91,6 +91,6 @@ export const deleteProjectsMonthController = async (req: ConfacRequest, res: Res const id = req.body.id; await req.db.collection(CollectionNames.PROJECTS_MONTH).findOneAndDelete({ _id: new ObjectID(id) }); await req.db.collection(CollectionNames.ATTACHMENTS_PROJECT_MONTH).findOneAndDelete({ _id: new ObjectID(id) }); - emitEntityEvent({ req, eventType: SocketEventTypes.EntityDeleted, entityType: CollectionNames.PROJECTS_MONTH, entityId: id, entity: null }); + emitEntityEvent(req, SocketEventTypes.EntityDeleted, CollectionNames.PROJECTS_MONTH, id, null); return res.send(id); }; diff --git a/backend/src/controllers/user.ts b/backend/src/controllers/user.ts index bbcbcf49..e8f3a1cd 100644 --- a/backend/src/controllers/user.ts +++ b/backend/src/controllers/user.ts @@ -113,14 +113,14 @@ export const saveUser = async (req: ConfacRequest, res: Response) => { const {value: originalUser} = await collection.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: user}, {returnOriginal: true}); await saveAudit(req, 'user', originalUser, user); const responseUser = {_id, ...user}; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityUpdated, entityType: CollectionNames.USERS, entityId: _id, entity: responseUser }); + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.USERS, _id, responseUser); return res.send(responseUser); } user.audit = createAudit(req.user); const inserted = await collection.insertOne(user); const [createdUser] = inserted.ops; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.USERS, entityId: createdUser._id, entity: createdUser }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.USERS, createdUser._id, createdUser); return res.send(createdUser); }; @@ -148,13 +148,13 @@ export const saveRole = async (req: ConfacRequest, res: Response) => { await saveAudit(req, 'role', originalRole, role); const responseRole = {_id, ...role}; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityUpdated, entityType: CollectionNames.ROLES, entityId: _id, entity: responseRole }); + emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.ROLES, _id, responseRole); return res.send(responseRole); } role.audit = createAudit(req.user); const inserted = await collection.insertOne(role); const [createdRole] = inserted.ops; - emitEntityEvent({ req, eventType: SocketEventTypes.EntityCreated, entityType: CollectionNames.ROLES, entityId: createdRole._id, entity: createdRole }); + emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.ROLES, createdRole._id, createdRole); return res.send(createdRole); };