Skip to content

Commit

Permalink
Socket IO project & consultant basic
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHub committed Dec 30, 2024
1 parent 62b2108 commit cdf243a
Show file tree
Hide file tree
Showing 22 changed files with 595 additions and 32 deletions.
207 changes: 205 additions & 2 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"pug": "^2.0.4",
"regenerator-runtime": "^0.13.3",
"slugify": "^1.1.0",
"socket.io": "^4.8.1",
"tmp": "^0.2.1",
"ubl-builder": "github:pipesanta/ubl-builder",
"uuid": "^10.0.0"
Expand Down
8 changes: 6 additions & 2 deletions backend/src/controllers/consultants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {Request, Response} from 'express';
import slugify from 'slugify';
import {ObjectID} from 'mongodb';
import {IConsultant} from '../models/consultants';
import {CollectionNames, createAudit, updateAudit} from '../models/common';
import {CollectionNames, createAudit, SocketEventTypes, updateAudit} from '../models/common';
import {ConfacRequest} from '../models/technical';
import {saveAudit} from './utils/audit-logs';
import { emitEntityEvent } from './utils/entity-events';

export const getConsultants = async (req: Request, res: Response) => {
const consultants = await req.db.collection<IConsultant>(CollectionNames.CONSULTANTS).find().toArray();
Expand All @@ -20,7 +21,9 @@ export const saveConsultant = async (req: ConfacRequest, res: Response) => {
.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: consultant}, {returnOriginal: true});

await saveAudit(req, 'consultant', originalConsultant, consultant);
return res.send({_id, ...consultant});
const responseConsultant = {_id, ...consultant};
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.CONSULTANTS, responseConsultant, _id);
return res.send(responseConsultant);
}

const slug = slugify(`${consultant.firstName}-${consultant.name}`).toLowerCase();
Expand All @@ -31,5 +34,6 @@ export const saveConsultant = async (req: ConfacRequest, res: Response) => {
audit: createAudit(req.user),
});
const [createdConsultant] = inserted.ops;
emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.CONSULTANTS, createdConsultant, createdConsultant._id);
return res.send(createdConsultant);
};
11 changes: 8 additions & 3 deletions backend/src/controllers/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {Request, Response} from 'express';
import moment from 'moment';
import {ObjectID} from 'mongodb';
import {IProject} from '../models/projects';
import {CollectionNames, createAudit, updateAudit} from '../models/common';
import {CollectionNames, createAudit, SocketEventTypes, updateAudit} from '../models/common';
import {ConfacRequest} from '../models/technical';
import {saveAudit} from './utils/audit-logs';
import {emitEntityEvent} from './utils/entity-events';

/** No longer in use: this is now done in the frontend */
export const findActiveProjectsForSelectedMonth = (selectedMonth: string, projects: IProject[]) => projects.filter(project => {
Expand Down Expand Up @@ -44,20 +45,24 @@ export const saveProject = async (req: ConfacRequest, res: Response) => {
const projectsColl = req.db.collection<IProject>(CollectionNames.PROJECTS);
const {value: originalProject} = await projectsColl.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: project}, {returnOriginal: true});
await saveAudit(req, 'project', originalProject, project);
return res.send({_id, ...project});
const responseProject = {_id, ...project};
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.PROJECTS, responseProject, _id);
return res.send(responseProject);
}

const inserted = await req.db.collection<Omit<IProject, '_id'>>(CollectionNames.PROJECTS).insertOne({
...project,
audit: createAudit(req.user),
});
const [createdProject] = inserted.ops;
emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.PROJECTS, createdProject, createdProject._id);
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) });
await req.db.collection(CollectionNames.PROJECTS).findOneAndDelete({_id: new ObjectID(id)});
emitEntityEvent(req, SocketEventTypes.EntityDeleted, CollectionNames.PROJECTS, null, id);
return res.send(id);
};
Loading

0 comments on commit cdf243a

Please sign in to comment.