Files
outline/server/commands/userInviter.ts
Tom Moor 0139b91b5d chore: Replace lodash with es-toolkit (#12281)
* chore: Replace lodash with es-toolkit

Migrate all direct lodash imports to es-toolkit/compat for a smaller,
faster, lodash-compatible utility library. Transitive lodash usage from
other packages remains unchanged.

* fix: Restore isPlainObject semantics in CanCan policy

The lodash migration aliased `isObject` to `lodash/isPlainObject` and
the codemod incorrectly mapped the local name to es-toolkit's `isObject`,
which also returns true for arrays and functions. This caused condition
objects in policy definitions to be skipped, breaking authorization
checks across the codebase.

* fix: Restore unicode-aware length counting in validators

es-toolkit/compat's size() returns string.length, while lodash's _.size()
counts unicode code points. Switch to [...value].length to preserve the
previous behavior so multi-byte characters like emoji count as one.
2026-05-06 21:03:47 -04:00

127 lines
3.1 KiB
TypeScript

import { partition, uniqBy } from "es-toolkit/compat";
import { UserRole } from "@shared/types";
import InviteEmail from "@server/emails/templates/InviteEmail";
import env from "@server/env";
import Logger from "@server/logging/Logger";
import { User, Team } from "@server/models";
import { UserFlag } from "@server/models/User";
import type { APIContext } from "@server/types";
import { DomainNotAllowedError } from "@server/errors";
import { can } from "@server/policies";
export type Invite = {
name: string;
email: string;
role: UserRole;
};
type Props = {
invites: Invite[];
suppressEmail?: boolean;
};
export default async function userInviter(
ctx: APIContext,
{ invites, suppressEmail }: Props
): Promise<{
sent: Invite[];
unsent: Invite[];
users: User[];
}> {
const { user } = ctx.state.auth;
const team = await Team.findByPk(user.teamId, { rejectOnEmpty: true });
// filter out empties and obvious non-emails
const compactedInvites = invites.filter(
(invite) => !!invite.email.trim() && invite.email.match("@")
);
// normalize to lowercase and remove duplicates
const normalizedInvites = uniqBy(
compactedInvites.map((invite) => ({
...invite,
email: invite.email.toLowerCase(),
})),
"email"
);
// filter out any existing users in the system
const emails = normalizedInvites.map((invite) => invite.email);
if (!can(user, "update", team)) {
for (const email of emails) {
if (!(await team.isDomainAllowed(email))) {
throw DomainNotAllowedError();
}
}
}
const existingUsers = await User.findAll({
where: {
teamId: user.teamId,
email: emails,
},
});
const existingEmails = existingUsers.map(
(existingUser) => existingUser.email
);
const [existingInvites, filteredInvites] = partition(
normalizedInvites,
(invite) => existingEmails.includes(invite.email)
);
const users = [];
// send and record remaining invites
for (const invite of filteredInvites) {
const newUser = await User.createWithCtx(
ctx,
{
teamId: user.teamId,
name: invite.name,
email: invite.email,
role:
user.isAdmin && invite.role === UserRole.Admin
? UserRole.Admin
: user.isViewer || invite.role === UserRole.Viewer
? UserRole.Viewer
: UserRole.Member,
invitedById: user.id,
flags: suppressEmail
? undefined
: {
[UserFlag.InviteSent]: 1,
},
},
{
name: "invite",
}
);
users.push(newUser);
if (!suppressEmail) {
await new InviteEmail({
to: invite.email,
language: newUser.language,
name: invite.name,
actorName: user.name,
actorEmail: user.email,
teamName: team.name,
teamUrl: team.url,
}).schedule();
}
if (env.isDevelopment) {
Logger.info(
"email",
`Sign in immediately: ${
env.URL
}/auth/email.callback?token=${newUser.getEmailSigninToken(ctx)}`
);
}
}
return {
sent: filteredInvites,
unsent: existingInvites,
users,
};
}