Skip to content

Commit

Permalink
users
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHub committed Dec 30, 2024
1 parent 7e6bbfa commit cd0aeff
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
12 changes: 8 additions & 4 deletions backend/src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {Request, Response} from 'express';
import {ObjectID} from 'mongodb';
import {OAuth2Client, TokenPayload} from 'google-auth-library';
import jwt from 'jsonwebtoken';
import {CollectionNames, IAudit, createAudit, updateAudit} from '../models/common';
import {CollectionNames, IAudit, SocketEventTypes, createAudit, updateAudit} from '../models/common';
import config from '../config';
import {IUser, IRole} from '../models/user';
import {ConfacRequest} from '../models/technical';
import {saveAudit} from './utils/audit-logs';
import { emitEntityEvent } from './utils/entity-events';

const AdminRole = 'admin';

Expand Down Expand Up @@ -111,13 +112,16 @@ export const saveUser = async (req: ConfacRequest, res: Response) => {
user.audit = updateAudit(user.audit, req.user);
const {value: originalUser} = await collection.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: user}, {returnOriginal: true});
await saveAudit(req, 'user', originalUser, user);
return res.send({_id, ...user});
const responseUser = {_id, ...user};
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.USERS, _id, responseUser);
return res.send(responseUser);
}

user.audit = createAudit(req.user);
const inserted = await collection.insertOne(user);
const [createdClient] = inserted.ops;
return res.send(createdClient);
const [createdUser] = inserted.ops;
emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.USERS, createdUser._id, createdUser);
return res.send(createdUser);
};


Expand Down
19 changes: 19 additions & 0 deletions frontend/src/actions/userActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {catchHandler} from './utils/fetch';
import {busyToggle, success} from './appActions';
import {ACTION_TYPES} from './utils/ActionTypes';
import { socketService } from '../components/socketio/SocketService';
import { SocketEventTypes } from '../components/socketio/SocketEventTypes';
import { Dispatch } from 'redux';
import { EntityEventPayload } from '../components/socketio/EntityEventPayload';


export function saveUser(user: UserModel, callback?: (savedUser: UserModel) => void, navigate?: any) {
Expand Down Expand Up @@ -36,6 +39,22 @@ export function saveUser(user: UserModel, callback?: (savedUser: UserModel) => v
};
}

export function handleUserSocketEvents(eventType: string, eventPayload: EntityEventPayload){
return (dispatch: Dispatch) => {
dispatch(busyToggle());
switch(eventType){
case SocketEventTypes.EntityUpdated:
case SocketEventTypes.EntityCreated:
dispatch({
type: ACTION_TYPES.USER_UPDATE,
user: eventPayload.entity,
}); break;
default: throw new Error(`${eventType} not supported for user.`);
}
dispatch(busyToggle.off());
}
}


export function saveRole(role: RoleModel, callback?: (savedRole: RoleModel) => void, navigate?: any) {
return dispatch => {
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/components/client/EditClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ const EditClient = () => {
const initClient = getClient(storeClient, config);
const [client, setClient] = useState<ClientModel>(initClient);
const clientWithSameKbo = useSelector((state: ConfacState) => state.clients.filter(x => x.btw === client.btw).find(x => x.slug !== params.id));

useEntityChangedToast(client._id);

const dispatch = useDispatch();
// useEffect(() => window.scrollTo(0, 0)); // TODO: each keystroke made it scroll to top :(
useDocumentTitle('clientEdit', {name: client.name});
useEntityChangedToast(client._id);


if (storeClient && !client._id) {
setClient(storeClient);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/project/EditProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const EditProject = () => {
const client = useSelector((state: ConfacState) => state.clients.find(x => x._id === project.client.clientId) || getNewClient());
const hasProjectMonths = useSelector((state: ConfacState) => state.projectsMonth.some(pm => pm.projectId === params.id));
const [needsSync, setNeedsSync] = useState<{consultant: boolean, client: boolean}>({consultant: false, client: false});

useEntityChangedToast(model?._id);

const docTitle = consultant._id ? 'projectEdit' : 'projectNew';
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/socketio/SocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SocketEventTypes } from "./SocketEventTypes";
import { EntityEventPayload } from "./EntityEventPayload";
import { t } from "../utils";
import { toast } from "react-toastify";
import { handleUserSocketEvents } from "../../actions/userActions";

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



Expand All @@ -23,9 +24,12 @@ export const EditUser = () => {
const params = useParams();
const model = useSelector((state: ConfacState) => state.user.users.find(c => c.alias === params.id));
const [user, setUser] = useState<UserModel>(model || getNewUser());


useEntityChangedToast(user._id);

const docTitle = user._id ? 'userEdit' : 'userNew';
useDocumentTitle(docTitle, {name: `${user.firstName} ${user.name}`});


if (model && !user._id) {
setUser(model);
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 @@ -681,7 +681,8 @@ export const trans = {
entities: {
projects: 'Project',
consultants: 'Consultant',
clients: 'Klant'
clients: 'Klant',
users: 'Gebruiker'
},
operation: {
entityUpdated: '{entityType} werd aangepast door {user}',
Expand Down

0 comments on commit cd0aeff

Please sign in to comment.