Skip to content

Commit

Permalink
config
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHub committed Dec 30, 2024
1 parent c83438e commit fc98fde
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
11 changes: 8 additions & 3 deletions backend/src/controllers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {ObjectID} from 'mongodb';
import appConfig from '../config';
import {ICompanyConfig} from '../models/config';
import {getTemplatesPath} from './utils';
import {CollectionNames, updateAudit} from '../models/common';
import {CollectionNames, SocketEventTypes, updateAudit} from '../models/common';
import {ConfacRequest} from '../models/technical';
import {saveAudit} from './utils/audit-logs';
import { emitEntityEvent } from './utils/entity-events';

export const getCompanyConfig = async (req: Request, res: Response) => {
const companyConfig: ICompanyConfig | null = await req.db.collection(CollectionNames.CONFIG).findOne({key: 'conf'});
Expand Down Expand Up @@ -41,11 +42,15 @@ export const saveCompanyConfig = async (req: ConfacRequest, res: Response) => {
.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: config}, {returnOriginal: true});

await saveAudit(req, 'config', originalConfig, config);
return res.send({_id, ...config});
const responseConfig = {_id, ...config};
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.CONFIG, _id, responseConfig);
return res.send(responseConfig);
}

const inserted = await req.db.collection<ICompanyConfig>(CollectionNames.CONFIG).insertOne(config);
return res.send(inserted.ops[0]);
const responseConfig = inserted.ops[0];
emitEntityEvent(req, SocketEventTypes.EntityCreated, CollectionNames.CONFIG, responseConfig._id, responseConfig);
return res.send(responseConfig);
};


Expand Down
21 changes: 21 additions & 0 deletions frontend/src/actions/configActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import t from '../trans';
import {ConfigModel} from '../components/config/models/ConfigModel';
import {busyToggle, success} from './appActions';
import {authService} from '../components/users/authService';
import { Dispatch } from 'redux';
import { EntityEventPayload } from '../components/socketio/EntityEventPayload';
import { SocketEventTypes } from '../components/socketio/SocketEventTypes';
import { socketService } from '../components/socketio/SocketService';

export function updateConfig(newConfig: ConfigModel) {
return dispatch => {
dispatch(busyToggle());
return request.post(buildUrl('/config'))
.set('Content-Type', 'application/json')
.set('Authorization', authService.getBearer())
.set('x-socket-id', socketService.socketId)
.send(newConfig)
.then(res => {
dispatch({type: ACTION_TYPES.CONFIG_UPDATE, config: res.body});
Expand All @@ -22,3 +27,19 @@ export function updateConfig(newConfig: ConfigModel) {
.then(() => dispatch(busyToggle.off()));
};
}

export function handleconfigSocketEvents(eventType: string, eventPayload: EntityEventPayload){
return (dispatch: Dispatch) => {
dispatch(busyToggle());
switch(eventType){
case SocketEventTypes.EntityUpdated:
case SocketEventTypes.EntityCreated:
dispatch({
type: ACTION_TYPES.CONFIG_UPDATE,
config: eventPayload.entity,
}); break;
default: throw new Error(`${eventType} not supported for config.`);
}
dispatch(busyToggle.off());
}
}
3 changes: 3 additions & 0 deletions frontend/src/components/config/EditConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {Audit} from '../admin/audit/Audit';
import {Claim} from '../users/models/UserModel';
import {GenericAttachmentDropzone} from '../controls/attachments/GenericAttachmentDropzone';
import {ClaimGuard} from '../enhancers/EnhanceWithClaim';
import useEntityChangedToast from '../hooks/useEntityChangedToast';


const EditConfig = () => {
Expand All @@ -22,6 +23,8 @@ const EditConfig = () => {
const config = useSelector((state: ConfacState) => state.config);
const [state, setState] = useState<ConfigModel>(JSON.parse(JSON.stringify(config)));

useEntityChangedToast(config._id);

const termsAndConditions = config.attachments.find(x => x.type === 'TermsAndConditions');
return (
<Container className="edit-container">
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/socketio/SocketService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { Dispatch } from "redux";
import { io } from "socket.io-client";
import { handleClientSocketEvents, handleConsultantSocketEvents, handleProjectSocketEvents } from "../../actions";
import { handleClientSocketEvents, handleconfigSocketEvents, handleConsultantSocketEvents, handleProjectSocketEvents } from "../../actions";
import { SocketEventTypes } from "./SocketEventTypes";
import { EntityEventPayload } from "./EntityEventPayload";
import { t } from "../utils";
Expand Down Expand Up @@ -44,6 +44,7 @@ function createSocketService () {
case 'clients': dispatch(handleClientSocketEvents(eventType, eventPayload)); break;
case 'users': dispatch(handleUserSocketEvents(eventType, eventPayload)); break;
case 'roles': dispatch(handleRoleSocketEvents(eventType, eventPayload)); break;
case 'config': dispatch(handleconfigSocketEvents(eventType, eventPayload)); break;
default: throw new Error(`${eventPayload.entityType} event for entity type not supported.`);
};
});
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 @@ -683,7 +683,8 @@ export const trans = {
consultants: 'Consultant',
clients: 'Klant',
users: 'Gebruiker',
roles: 'Rol'
roles: 'Rol',
config: 'Configuratie'
},
operation: {
entityUpdated: '{entityType} werd aangepast door {user}',
Expand Down

0 comments on commit fc98fde

Please sign in to comment.