ENCOA-308
This commit is contained in:
@@ -22,6 +22,7 @@ import { IoInformationCircleOutline } from "react-icons/io5";
|
|||||||
import { FaFileDownload } from "react-icons/fa";
|
import { FaFileDownload } from "react-icons/fa";
|
||||||
import { HiOutlineDocumentText } from "react-icons/hi";
|
import { HiOutlineDocumentText } from "react-icons/hi";
|
||||||
import UserImportSummary, { ExcelUserDuplicatesMap } from "@/components/ImportSummaries/User";
|
import UserImportSummary, { ExcelUserDuplicatesMap } from "@/components/ImportSummaries/User";
|
||||||
|
import { v4 } from "uuid";
|
||||||
|
|
||||||
const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/);
|
const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/);
|
||||||
|
|
||||||
@@ -87,7 +88,7 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
|||||||
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
|
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
|
||||||
const [type, setType] = useState<Type>("student");
|
const [type, setType] = useState<Type>("student");
|
||||||
const [showHelp, setShowHelp] = useState(false);
|
const [showHelp, setShowHelp] = useState(false);
|
||||||
const [entity, setEntity] = useState<{id: string | null, label: string | null}| undefined>(() => {
|
const [entity, setEntity] = useState<{ id: string | null, label: string | null } | undefined>(() => {
|
||||||
if (!entities?.length) {
|
if (!entities?.length) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@@ -307,11 +308,11 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
|||||||
}));
|
}));
|
||||||
setNewUsers(newUsersList);
|
setNewUsers(newUsersList);
|
||||||
|
|
||||||
const {data: emailEntityMap} = await axios.post("/api/users/controller?op=getEntities", {
|
const { data: emailEntityMap } = await axios.post("/api/users/controller?op=getEntities", {
|
||||||
emails: dupes.map((x) => x.email)
|
emails: dupes.map((x) => x.email)
|
||||||
});
|
});
|
||||||
const withLabels = dupes.map((u) => ({
|
const withLabels = dupes.map((u) => ({
|
||||||
...u,
|
...u,
|
||||||
entityLabels: emailEntityMap.find((e: any) => e.email === u.email)?.entityLabels || []
|
entityLabels: emailEntityMap.find((e: any) => e.email === u.email)?.entityLabels || []
|
||||||
}))
|
}))
|
||||||
setDuplicatedUsers(withLabels);
|
setDuplicatedUsers(withLabels);
|
||||||
@@ -339,18 +340,17 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
|||||||
if (!confirm(`You are about to ${[newUsersSentence, existingUsersSentence].filter((x) => !!x).join(" and ")}, are you sure you want to continue?`))
|
if (!confirm(`You are about to ${[newUsersSentence, existingUsersSentence].filter((x) => !!x).join(" and ")}, are you sure you want to continue?`))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Promise.all(newUsers.map(async (u) => await axios.post(`/api/invites`, { to: u.id, entity, from: user.id })))
|
|
||||||
.then(() => toast.success(`Successfully invited ${newUsers.length} registered student(s)!`))
|
|
||||||
.finally(() => {
|
|
||||||
if (newUsers.length === 0) setIsLoading(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (newUsers.length > 0) {
|
if (newUsers.length > 0) {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await axios.post("/api/batch_users", { users: newUsers.map((user) => ({ ...user, type, expiryDate })) });
|
const withIds = newUsers.map((user) => ({ ...user, type, expiryDate, id: v4() }));
|
||||||
toast.success(`Successfully added ${newUsers.length} user(s)!`);
|
await axios.post("/api/batch_users", { users: withIds});
|
||||||
|
toast.success(`Successfully added ${withIds.length} user(s)!`);
|
||||||
|
Promise.all(withIds.map(async (u) => await axios.post(`/api/invites`, { to: u.id, entity: entity?.id, from: user.id })))
|
||||||
|
.then(() => toast.success(`Successfully invited ${withIds.length} registered student(s)!`))
|
||||||
|
.finally(() => {
|
||||||
|
if (withIds.length === 0) setIsLoading(false);
|
||||||
|
});
|
||||||
onFinish();
|
onFinish();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
@@ -486,14 +486,14 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
|
|||||||
options={entities.map((e) => ({ value: e.id, label: e.label }))}
|
options={entities.map((e) => ({ value: e.id, label: e.label }))}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
if (!e) {
|
if (!e) {
|
||||||
setEntity(undefined);
|
setEntity(undefined);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setEntity({
|
setEntity({
|
||||||
id: e?.value,
|
id: e?.value,
|
||||||
label: e?.label
|
label: e?.label
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
isClearable={checkAccess(user, ["admin", "developer"])}
|
isClearable={checkAccess(user, ["admin", "developer"])}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
passport_id?: string;
|
passport_id?: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
};
|
};
|
||||||
entity?: string
|
entity: { id: string, label: string }
|
||||||
entities: { id: string, role: string }[]
|
entities: { id: string, role: string }[]
|
||||||
passwordHash: string | undefined;
|
passwordHash: string | undefined;
|
||||||
passwordSalt: string | undefined;
|
passwordSalt: string | undefined;
|
||||||
@@ -49,11 +49,10 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const salt = crypto.randomBytes(16).toString('base64');
|
const salt = crypto.randomBytes(16).toString('base64');
|
||||||
const hash = await scrypt.hash(user.passport_id, salt);
|
const hash = await scrypt.hash(user.passport_id, salt);
|
||||||
|
|
||||||
const entity = await getEntityWithRoles(currentUser.entity!)
|
const entity = await getEntityWithRoles(currentUser.entity!.id)
|
||||||
const defaultRole = findBy(entity?.roles || [], "isDefault", true)
|
const defaultRole = findBy(entity?.roles || [], "isDefault", true)
|
||||||
|
|
||||||
currentUser.entities = [{ id: entity?.id || "", role: defaultRole?.id || "" }]
|
currentUser.entities = [{ id: entity?.id || "", role: defaultRole?.id || "" }]
|
||||||
delete currentUser.entity
|
|
||||||
|
|
||||||
currentUser.email = currentUser.email.toLowerCase();
|
currentUser.email = currentUser.email.toLowerCase();
|
||||||
currentUser.passwordHash = hash;
|
currentUser.passwordHash = hash;
|
||||||
|
|||||||
Reference in New Issue
Block a user