ENCOA-289
This commit is contained in:
@@ -87,7 +87,15 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
||||
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
|
||||
const [type, setType] = useState<Type>("student");
|
||||
const [showHelp, setShowHelp] = useState(false);
|
||||
const [entity, setEntity] = useState((entities || [])[0]?.id || undefined)
|
||||
const [entity, setEntity] = useState<{id: string | null, label: string | null}| undefined>(() => {
|
||||
if (!entities?.length) {
|
||||
return undefined;
|
||||
}
|
||||
return {
|
||||
id: entities[0].id,
|
||||
label: entities[0].label
|
||||
};
|
||||
});
|
||||
|
||||
const { openFilePicker, filesContent, clear } = useFilePicker({
|
||||
accept: ".xlsx",
|
||||
@@ -291,11 +299,28 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
||||
if (!!crossRefEmails) {
|
||||
const existingEmails = new Set(crossRefEmails.map((x: any) => x.email));
|
||||
const dupes = infos.filter(info => existingEmails.has(info.email));
|
||||
const newUsersList = infos.filter(info => !existingEmails.has(info.email));
|
||||
const newUsersList = infos
|
||||
.filter(info => !existingEmails.has(info.email))
|
||||
.map(info => ({
|
||||
...info,
|
||||
entityLabels: [entity!.label!]
|
||||
}));
|
||||
setNewUsers(newUsersList);
|
||||
setDuplicatedUsers(dupes);
|
||||
|
||||
const {data: emailEntityMap} = await axios.post("/api/users/controller?op=getEntities", {
|
||||
emails: dupes.map((x) => x.email)
|
||||
});
|
||||
const withLabels = dupes.map((u) => ({
|
||||
...u,
|
||||
entityLabels: emailEntityMap.find((e: any) => e.email === u.email)?.entityLabels || []
|
||||
}))
|
||||
setDuplicatedUsers(withLabels);
|
||||
} else {
|
||||
setNewUsers(infos);
|
||||
const withLabel = infos.map(info => ({
|
||||
...info,
|
||||
entityLabels: [entity!.label!]
|
||||
}));
|
||||
setNewUsers(withLabel);
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error("Something went wrong, please try again later!");
|
||||
@@ -305,7 +330,7 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
||||
if (infos.length > 0) {
|
||||
crossReferenceEmails();
|
||||
}
|
||||
}, [infos]);
|
||||
}, [infos, entity]);
|
||||
|
||||
const makeUsers = async () => {
|
||||
const newUsersSentence = newUsers.length > 0 ? `create ${newUsers.length} user(s)` : undefined;
|
||||
@@ -459,7 +484,16 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
||||
<Select
|
||||
defaultValue={{ value: (entities || [])[0]?.id, label: (entities || [])[0]?.label }}
|
||||
options={entities.map((e) => ({ value: e.id, label: e.label }))}
|
||||
onChange={(e) => setEntity(e?.value || undefined)}
|
||||
onChange={(e) => {
|
||||
if (!e) {
|
||||
setEntity(undefined);
|
||||
return;
|
||||
}
|
||||
setEntity({
|
||||
id: e?.value,
|
||||
label: e?.label
|
||||
});
|
||||
}}
|
||||
isClearable={checkAccess(user, ["admin", "developer"])}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -38,6 +38,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
await assignToEntity(req.body);
|
||||
res.status(200).json({"ok": true});
|
||||
break;
|
||||
case 'getEntities':
|
||||
res.status(200).json(await getEntities(req.body.emails))
|
||||
break;
|
||||
default:
|
||||
res.status(400).json({ error: 'Invalid operation!' })
|
||||
}
|
||||
@@ -276,6 +279,36 @@ async function getIds(emails: string[]): Promise<Array<{ email: string; id: stri
|
||||
}));
|
||||
}
|
||||
|
||||
async function getEntities(emails: string[]): Promise<Array<{ email: string; entityLabels: string[] }>> {
|
||||
const users = await db.collection('users')
|
||||
.find({ email: { $in: emails } })
|
||||
.project({ email: 1, entities: 1, _id: 0 })
|
||||
.toArray();
|
||||
|
||||
const entityIds = [...new Set(
|
||||
users.flatMap(user =>
|
||||
(user.entities || []).map((entity: any) => entity.id)
|
||||
)
|
||||
)];
|
||||
|
||||
const entityRecords = await db.collection('entities')
|
||||
.find({ id: { $in: entityIds } })
|
||||
.project({ id: 1, label: 1, _id: 0 })
|
||||
.toArray();
|
||||
|
||||
const entityMap = new Map(
|
||||
entityRecords.map(entity => [entity.id, entity.label])
|
||||
);
|
||||
|
||||
return users.map(user => ({
|
||||
email: user.email,
|
||||
entityLabels: (user.entities || [])
|
||||
.map((entity: any) => entityMap.get(entity.id))
|
||||
.filter((label: string): label is string => !!label)
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function assignToEntity(body: any) {
|
||||
const { ids, entity } = body;
|
||||
|
||||
Reference in New Issue
Block a user