Skip to content

Commit

Permalink
role
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHub committed Dec 30, 2024
1 parent cd0aeff commit c83438e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
10 changes: 7 additions & 3 deletions backend/src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ export const saveRole = async (req: ConfacRequest, res: Response) => {
role.audit = updateAudit(role.audit, req.user);
const {value: originalRole} = await collection.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: role}, {returnOriginal: true});
await saveAudit(req, 'role', originalRole, role);
return res.send({_id, ...role});

const responseRole = {_id, ...role};
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.ROLES, _id, responseRole);
return res.send(responseRole);
}

role.audit = createAudit(req.user);
const inserted = await collection.insertOne(role);
const [createdClient] = inserted.ops;
return res.send(createdClient);
const [createdRole] = inserted.ops;
emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.ROLES, createdRole._id, createdRole);
return res.send(createdRole);
};
16 changes: 16 additions & 0 deletions frontend/src/actions/userActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,19 @@ export function saveRole(role: RoleModel, callback?: (savedRole: RoleModel) => v
.then(() => dispatch(busyToggle.off()));
};
}

export function handleRoleSocketEvents(eventType: string, eventPayload: EntityEventPayload){
return (dispatch: Dispatch) => {
dispatch(busyToggle());
switch(eventType){
case SocketEventTypes.EntityUpdated:
case SocketEventTypes.EntityCreated:
dispatch({
type: ACTION_TYPES.ROLE_UPDATE,
role: eventPayload.entity,
}); break;
default: throw new Error(`${eventType} not supported for role.`);
}
dispatch(busyToggle.off());
}
}
3 changes: 2 additions & 1 deletion frontend/src/components/socketio/SocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SocketEventTypes } from "./SocketEventTypes";
import { EntityEventPayload } from "./EntityEventPayload";
import { t } from "../utils";
import { toast } from "react-toastify";
import { handleUserSocketEvents } from "../../actions/userActions";
import { handleRoleSocketEvents, handleUserSocketEvents } from "../../actions/userActions";

function createSocketService () {
// TODO nicolas read server url from frontend config !!!
Expand Down Expand Up @@ -43,6 +43,7 @@ function createSocketService () {
case 'consultants': dispatch(handleConsultantSocketEvents(eventType, eventPayload)); break;
case 'clients': dispatch(handleClientSocketEvents(eventType, eventPayload)); break;
case 'users': dispatch(handleUserSocketEvents(eventType, eventPayload)); break;
case 'roles': dispatch(handleRoleSocketEvents(eventType, eventPayload)); break;
default: throw new Error(`${eventPayload.entityType} event for entity type not supported.`);
};
});
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/users/EditRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {useDocumentTitle} from '../hooks/useDocumentTitle';
import {saveRole} from '../../actions/userActions';
import {Audit} from '../admin/audit/Audit';
import {useParams} from 'react-router-dom';
import useEntityChangedToast from '../hooks/useEntityChangedToast';


export const EditRole = () => {
Expand All @@ -23,6 +24,8 @@ export const EditRole = () => {
const model = useSelector((state: ConfacState) => state.user.roles.find(c => c.name === params.id));
const [role, setRole] = useState<RoleModel>(model || getNewRole());

useEntityChangedToast(role._id);

const docTitle = role._id ? 'roleEdit' : 'roleNew';
useDocumentTitle(docTitle, {name: role.name});

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/trans.nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ export const trans = {
projects: 'Project',
consultants: 'Consultant',
clients: 'Klant',
users: 'Gebruiker'
users: 'Gebruiker',
roles: 'Rol'
},
operation: {
entityUpdated: '{entityType} werd aangepast door {user}',
Expand Down

0 comments on commit c83438e

Please sign in to comment.