Skip to content

Commit

Permalink
emit event correctie
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHub committed Dec 31, 2024
1 parent 9c5f22b commit 2d8c914
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions backend/src/controllers/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
};
4 changes: 2 additions & 2 deletions backend/src/controllers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICompanyConfig>(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);
};

Expand Down
4 changes: 2 additions & 2 deletions backend/src/controllers/consultants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
};
9 changes: 7 additions & 2 deletions backend/src/controllers/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) });
Expand Down
6 changes: 3 additions & 3 deletions backend/src/controllers/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -55,14 +55,14 @@ 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);
};


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);
};
8 changes: 4 additions & 4 deletions backend/src/controllers/projectsMonth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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);
}

Expand All @@ -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);
};

Expand All @@ -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);
};
8 changes: 4 additions & 4 deletions backend/src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down Expand Up @@ -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);
};

0 comments on commit 2d8c914

Please sign in to comment.