Notification when user super admin

This commit is contained in:
Maxime Lalo 2023-07-27 15:18:11 +02:00
parent 98acafb1fd
commit f3251f0f03
4 changed files with 114 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { Appointment, Vote } from "le-coffre-resources/dist/SuperAdmin"; import { Appointment } from "le-coffre-resources/dist/SuperAdmin";
import BaseSuperAdmin from "../BaseSuperAdmin"; import BaseSuperAdmin from "../BaseSuperAdmin";
@ -13,6 +13,11 @@ export type IPostLiveVotesParams = {
appointment: Appointment; appointment: Appointment;
}; };
export type LiveVote = {
uid: string;
appointment: Appointment;
};
export default class LiveVotes extends BaseSuperAdmin { export default class LiveVotes extends BaseSuperAdmin {
private static instance: LiveVotes; private static instance: LiveVotes;
private readonly baseURl = this.namespaceUrl.concat("/live-votes"); private readonly baseURl = this.namespaceUrl.concat("/live-votes");
@ -32,10 +37,10 @@ export default class LiveVotes extends BaseSuperAdmin {
/** /**
* @description : Create a LiveVotes * @description : Create a LiveVotes
*/ */
public async post(body: IPostLiveVotesParams): Promise<Vote> { public async post(body: IPostLiveVotesParams): Promise<LiveVote> {
const url = new URL(this.baseURl); const url = new URL(this.baseURl);
try { try {
return await this.postRequest<Vote>(url, body); return await this.postRequest<LiveVote>(url, body);
} catch (err) { } catch (err) {
this.onError(err); this.onError(err);
return Promise.reject(err); return Promise.reject(err);

View File

@ -0,0 +1,44 @@
import { Vote } from "le-coffre-resources/dist/SuperAdmin";
import BaseSuperAdmin from "../BaseSuperAdmin";
// TODO Type get query params -> Where + inclue + orderby
export interface IGetVotessparams {
where?: {};
include?: {};
select?: {};
}
export type IDeleteVotesParams = {
uid: Vote["uid"];
};
export default class Votes extends BaseSuperAdmin {
private static instance: Votes;
private readonly baseURl = this.namespaceUrl.concat("/votes");
private constructor() {
super();
}
public static getInstance() {
if (!this.instance) {
return new this();
} else {
return this.instance;
}
}
/**
* @description : Create a Votes
*/
public async delete(body: IDeleteVotesParams): Promise<Vote> {
const url = new URL(this.baseURl + "/" + body.uid);
try {
return await this.deleteRequest<Vote>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
}

View File

@ -79,3 +79,7 @@
} }
} }
} }
.remove-my-vote {
margin-top: 16px;
}

View File

@ -1,12 +1,17 @@
import WarningIcon from "@Assets/images/warning.png"; import WarningIcon from "@Assets/images/warning.png";
import OfficeRoles from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles"; import OfficeRoles from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles";
import Roles from "@Front/Api/LeCoffreApi/Admin/Roles/Roles";
import LiveVotes from "@Front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes"; import LiveVotes from "@Front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes";
import Users from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users"; import Users from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users";
import Votes from "@Front/Api/LeCoffreApi/SuperAdmin/Votes/Votes";
import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button";
import SelectField, { IOption } from "@Front/Components/DesignSystem/Form/SelectField"; import SelectField, { IOption } from "@Front/Components/DesignSystem/Form/SelectField";
import Confirm from "@Front/Components/DesignSystem/Modal/Confirm"; import Confirm from "@Front/Components/DesignSystem/Modal/Confirm";
import Switch from "@Front/Components/DesignSystem/Switch"; import Switch from "@Front/Components/DesignSystem/Switch";
import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography";
import DefaultUserDashboard from "@Front/Components/LayoutTemplates/DefaultUserDashboard"; import DefaultUserDashboard from "@Front/Components/LayoutTemplates/DefaultUserDashboard";
import JwtService from "@Front/Services/JwtService/JwtService";
import Toasts from "@Front/Stores/Toasts";
import User, { Appointment, Vote } from "le-coffre-resources/dist/SuperAdmin"; import User, { Appointment, Vote } from "le-coffre-resources/dist/SuperAdmin";
import { EAppointmentStatus, EVote } from "le-coffre-resources/dist/SuperAdmin/Appointment"; import { EAppointmentStatus, EVote } from "le-coffre-resources/dist/SuperAdmin/Appointment";
import Image from "next/image"; import Image from "next/image";
@ -45,15 +50,17 @@ export default function UserInformations(props: IProps) {
role: true, role: true,
appointment: { appointment: {
include: { include: {
votes: true, votes: {
include: {
voter: true,
},
},
}, },
}, },
votes: true, votes: true,
}, },
}); });
if (!user) return; if (!user) return;
console.log(user);
const roles = await OfficeRoles.getInstance().get(); const roles = await OfficeRoles.getInstance().get();
if (!roles) return; if (!roles) return;
setAvailableRoles(roles.map((role) => ({ value: role.uid, label: role.name }))); setAvailableRoles(roles.map((role) => ({ value: role.uid, label: role.name })));
@ -88,9 +95,23 @@ export default function UserInformations(props: IProps) {
const handleAdminModalAccepted = useCallback(async () => { const handleAdminModalAccepted = useCallback(async () => {
if (!userSelected) return; if (!userSelected) return;
if (adminModalType === "add") { if (adminModalType === "add") {
// add super admin const adminRole = await Roles.getInstance().getOne({
where: {
name: "admin",
},
});
if (!adminRole) return;
await Users.getInstance().put(
userSelected?.uid as string,
User.hydrate<User>({
uid: userSelected?.uid as string,
office_role: undefined,
role: adminRole,
}),
);
} else { } else {
// remove super admin // retirer rôle admin
} }
setIsAdminModalOpened(false); setIsAdminModalOpened(false);
}, [userSelected, adminModalType]); }, [userSelected, adminModalType]);
@ -124,7 +145,13 @@ export default function UserInformations(props: IProps) {
}), }),
}); });
const votes = await LiveVotes.getInstance().post(vote); const liveVote = await LiveVotes.getInstance().post(vote);
if (liveVote.appointment.votes?.length === 3) {
Toasts.getInstance().open({
title: `Le titre de super-administrateur a été attribué à ${userSelected.contact?.first_name} ${userSelected.contact?.last_name} `,
});
}
await getUser(); await getUser();
setIsSuperAdminModalOpened(false); setIsSuperAdminModalOpened(false);
}, [userSelected, currentAppointment, superAdminModalType, getUser]); }, [userSelected, currentAppointment, superAdminModalType, getUser]);
@ -135,6 +162,22 @@ export default function UserInformations(props: IProps) {
setIsSuperAdminChecked(userSelected.role?.name === "super-admin"); setIsSuperAdminChecked(userSelected.role?.name === "super-admin");
setIsAdminChecked(userSelected.role?.name === "admin" && !userSelected.office_role); setIsAdminChecked(userSelected.role?.name === "admin" && !userSelected.office_role);
}, [userSelected]); }, [userSelected]);
const userHasVoted = useCallback(() => {
if (!currentAppointment) return false;
const user = JwtService.getInstance().decodeJwt();
return currentAppointment.votes?.find((vote) => vote.voter?.uid === user?.userId) !== undefined;
}, [currentAppointment]);
const deleteMyVote = useCallback(async () => {
if (!currentAppointment) return;
const user = JwtService.getInstance().decodeJwt();
const vote = currentAppointment.votes?.find((vote) => vote.voter?.uid === user?.userId);
if (!vote) return;
await Votes.getInstance().delete({ uid: vote.uid });
await getUser();
}, [currentAppointment, getUser]);
return ( return (
<DefaultUserDashboard mobileBackText={"Liste des utilisateurs"}> <DefaultUserDashboard mobileBackText={"Liste des utilisateurs"}>
<div className={classes["root"]}> <div className={classes["root"]}>
@ -207,14 +250,21 @@ export default function UserInformations(props: IProps) {
<div> <div>
<Typography typo={ITypo.CAPTION_14}> <Typography typo={ITypo.CAPTION_14}>
{currentAppointment.choice === EVote.NOMINATE {currentAppointment.choice === EVote.NOMINATE
? `Vous avez voté pour attribuer le titre de Super Admin. Il manque ${ ? `Un ou des collaborateurs souhaitent attribuer le titre de Super Admin à ce collaborateur. Il manque ${
3 - currentAppointment.votes?.length! 3 - currentAppointment.votes?.length!
} vote(s) pour que le collaborateur se voit attribuer le titre.` } vote(s) pour que le collaborateur se voit attribuer le titre.`
: `Vous avez voté pour retirer le titre de Super Admin. Il manque ${ : `Un ou des collaborateurs souhaitent retirer le titre de Super Admin à ce collaborateur. Il manque ${
3 - currentAppointment.votes?.length! 3 - currentAppointment.votes?.length!
} vote(s) pour que le collaborateur se voit retirer le titre.`} } vote(s) pour que le collaborateur se voit retirer le titre.`}
</Typography> </Typography>
</div> </div>
{userHasVoted() && (
<div className={classes["remove-my-vote"]}>
<Button variant={EButtonVariant.SECONDARY} onClick={deleteMyVote}>
Retirer mon vote
</Button>
</div>
)}
</div> </div>
</div> </div>
)} )}