From 22b8aed12705bbdd48ff822b33281e2cb86d9ccb Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Thu, 17 Oct 2024 12:02:35 +0100 Subject: [PATCH] Continued with the entities for the batch users --- src/pages/(admin)/BatchCreateUser.tsx | 36 ++++++++++++++++++++------- src/pages/api/batch_users.ts | 7 +++++- src/pages/api/make_user.ts | 7 +++++- src/pages/settings.tsx | 2 +- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/pages/(admin)/BatchCreateUser.tsx b/src/pages/(admin)/BatchCreateUser.tsx index 8b97d6a2..2cad5c0e 100644 --- a/src/pages/(admin)/BatchCreateUser.tsx +++ b/src/pages/(admin)/BatchCreateUser.tsx @@ -17,6 +17,8 @@ import ReactDatePicker from "react-datepicker"; import clsx from "clsx"; import usePermissions from "@/hooks/usePermissions"; import countryCodes from "country-codes-list"; +import { EntityWithRoles } from "@/interfaces/entity"; +import Select from "@/components/Low/Select"; const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/); @@ -65,10 +67,11 @@ interface Props { user: User; users: User[]; permissions: PermissionType[]; + entities: EntityWithRoles[] onFinish: () => void; } -export default function BatchCreateUser({user, users, permissions, onFinish}: Props) { +export default function BatchCreateUser({user, users, entities = [], permissions, onFinish}: Props) { const [infos, setInfos] = useState< { email: string; @@ -89,6 +92,7 @@ export default function BatchCreateUser({user, users, permissions, onFinish}: Pr const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true); const [type, setType] = useState("student"); const [showHelp, setShowHelp] = useState(false); + const [entity, setEntity] = useState((entities || [])[0]?.id || undefined) const {openFilePicker, filesContent, clear} = useFilePicker({ accept: ".xlsx", @@ -122,6 +126,7 @@ export default function BatchCreateUser({user, users, permissions, onFinish}: Pr groupName: group, corporate, studentID, + entity, demographicInformation: { country: countryItem?.countryCode, passport_id: passport_id?.toString().trim() || undefined, @@ -206,15 +211,28 @@ export default function BatchCreateUser({user, users, permissions, onFinish}: Pr
-
- -
setShowHelp(true)}> - +
+
+
+ +
setShowHelp(true)}> + +
+
+ +
+
+ + )} -
diff --git a/src/pages/api/batch_users.ts b/src/pages/api/batch_users.ts index fdd2ec7b..fc7f6663 100644 --- a/src/pages/api/batch_users.ts +++ b/src/pages/api/batch_users.ts @@ -5,6 +5,8 @@ import { FirebaseScrypt } from 'firebase-scrypt'; import { firebaseAuthScryptParams } from "@/firebase"; import crypto from 'crypto'; import axios from "axios"; +import { getEntityWithRoles } from "@/utils/entities.be"; +import { findBy } from "@/utils"; export default withIronSessionApiRoute(handler, sessionOptions); @@ -47,7 +49,10 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const salt = crypto.randomBytes(16).toString('base64'); const hash = await scrypt.hash(user.passport_id, salt); - currentUser.entities = [{ id: currentUser.entity!, role: "90ce8f08-08c8-41e4-9848-f1500ddc3930" }] + const entity = await getEntityWithRoles(currentUser.entity!) + const defaultRole = findBy(entity?.roles || [], "isDefault", true) + + currentUser.entities = [{ id: entity?.id || "", role: defaultRole?.id || "" }] delete currentUser.entity currentUser.email = currentUser.email.toLowerCase(); diff --git a/src/pages/api/make_user.ts b/src/pages/api/make_user.ts index dd148874..bd05953a 100644 --- a/src/pages/api/make_user.ts +++ b/src/pages/api/make_user.ts @@ -10,6 +10,8 @@ import { getGroup, getGroups, getUserCorporate, getUserGroups, getUserNamedGroup import { uniq } from "lodash"; import { getSpecificUsers, getUser } from "@/utils/users.be"; import client from "@/lib/mongodb"; +import { getEntityWithRoles } from "@/utils/entities.be"; +import { findBy } from "@/utils"; const DEFAULT_DESIRED_LEVELS = { reading: 9, @@ -65,6 +67,9 @@ async function post(req: NextApiRequest, res: NextApiResponse) { .then(async (userCredentials) => { const userId = userCredentials.user.uid; + const entityWithRole = await getEntityWithRoles(entity) + const defaultRole = findBy(entityWithRole?.roles || [], "isDefault", true) + const user = { ...req.body, bio: "", @@ -78,7 +83,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { isFirstLogin: false, isVerified: true, registrationDate: new Date(), - entities: [{ id: entity, role: "90ce8f08-08c8-41e4-9848-f1500ddc3930" }], + entities: [{ id: entity, role: defaultRole?.id || "" }], subscriptionExpirationDate: expiryDate || null, ...((maker.type === "corporate" || maker.type === "mastercorporate") && type === "corporate" ? { diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx index 9bdb0307..db768563 100644 --- a/src/pages/settings.tsx +++ b/src/pages/settings.tsx @@ -74,7 +74,7 @@ export default function Admin({ user, entities, permissions }: Props) { setModalOpen(undefined)}> - setModalOpen(undefined)} /> + setModalOpen(undefined)} /> setModalOpen(undefined)}> setModalOpen(undefined)} />