2024-04-03 17:13:35 +08:00
|
|
|
import { customAlphabet } from 'nanoid';
|
|
|
|
|
|
2024-02-06 16:16:10 +11:00
|
|
|
import { hashSync } from '@documenso/lib/server-only/auth/hash';
|
|
|
|
|
|
|
|
|
|
import { prisma } from '..';
|
|
|
|
|
|
|
|
|
|
type SeedUserOptions = {
|
|
|
|
|
name?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
password?: string;
|
|
|
|
|
verified?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-03 17:13:35 +08:00
|
|
|
const nanoid = customAlphabet('1234567890abcdef', 10);
|
|
|
|
|
|
2024-06-02 15:49:09 +10:00
|
|
|
export const seedTestEmail = () => `${nanoid()}@test.documenso.com`;
|
|
|
|
|
|
2024-02-06 16:16:10 +11:00
|
|
|
export const seedUser = async ({
|
2024-04-03 17:13:35 +08:00
|
|
|
name,
|
|
|
|
|
email,
|
2024-02-06 16:16:10 +11:00
|
|
|
password = 'password',
|
|
|
|
|
verified = true,
|
|
|
|
|
}: SeedUserOptions = {}) => {
|
2024-10-08 13:44:02 +03:00
|
|
|
let url = name;
|
|
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
|
url = nanoid();
|
|
|
|
|
} else {
|
2024-04-03 17:13:35 +08:00
|
|
|
name = nanoid();
|
2024-10-08 13:44:02 +03:00
|
|
|
url = name;
|
2024-04-03 17:13:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!email) {
|
|
|
|
|
email = `${nanoid()}@test.documenso.com`;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 16:16:10 +11:00
|
|
|
return await prisma.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
email,
|
|
|
|
|
password: hashSync(password),
|
|
|
|
|
emailVerified: verified ? new Date() : undefined,
|
2024-10-08 13:44:02 +03:00
|
|
|
url,
|
2024-02-06 16:16:10 +11:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const unseedUser = async (userId: number) => {
|
|
|
|
|
await prisma.user.delete({
|
|
|
|
|
where: {
|
|
|
|
|
id: userId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-02-22 22:58:51 +11:00
|
|
|
|
|
|
|
|
export const unseedUserByEmail = async (email: string) => {
|
|
|
|
|
await prisma.user.delete({
|
|
|
|
|
where: {
|
|
|
|
|
email,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const extractUserVerificationToken = async (email: string) => {
|
|
|
|
|
return await prisma.verificationToken.findFirstOrThrow({
|
|
|
|
|
where: {
|
|
|
|
|
identifier: 'confirmation-email',
|
|
|
|
|
user: {
|
|
|
|
|
email,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|