93 lines
2.2 KiB
TypeScript
93 lines
2.2 KiB
TypeScript
import bcrypt from 'bcrypt';
|
|
import { Op, QueryTypes } from 'sequelize';
|
|
|
|
import config from '../../config.ts';
|
|
import type {
|
|
AdminUserSeedExistingIdRow,
|
|
AdminUserSeedRow,
|
|
SequelizeSeeder,
|
|
} from '../../types/index.ts';
|
|
|
|
const seedUserIds: readonly [string, string, string] = [
|
|
'193bf4b5-9f07-4bd5-9a43-e7e41f3e96af',
|
|
'af5a87be-8f9c-4630-902a-37a60b7005ba',
|
|
'5bc531ab-611f-41f3-9373-b7cc5d09c93d',
|
|
];
|
|
|
|
function createAdminUserRows(): AdminUserSeedRow[] {
|
|
const adminHash = bcrypt.hashSync(
|
|
config.admin_pass,
|
|
config.bcrypt.saltRounds,
|
|
);
|
|
const userHash = bcrypt.hashSync(config.user_pass, config.bcrypt.saltRounds);
|
|
const now = new Date();
|
|
|
|
return [
|
|
{
|
|
id: seedUserIds[0],
|
|
firstName: 'Admin',
|
|
email: config.admin_email,
|
|
emailVerified: true,
|
|
provider: config.providers.LOCAL,
|
|
password: adminHash,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
{
|
|
id: seedUserIds[1],
|
|
firstName: 'John',
|
|
email: 'john@doe.com',
|
|
emailVerified: true,
|
|
provider: config.providers.LOCAL,
|
|
password: userHash,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
{
|
|
id: seedUserIds[2],
|
|
firstName: 'Client',
|
|
email: 'client@hello.com',
|
|
emailVerified: true,
|
|
provider: config.providers.LOCAL,
|
|
password: userHash,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
];
|
|
}
|
|
|
|
const adminUserSeeder: SequelizeSeeder = {
|
|
async up(queryInterface) {
|
|
const existingRows =
|
|
await queryInterface.sequelize.query<AdminUserSeedExistingIdRow>(
|
|
'SELECT "id" FROM "users" WHERE "id" IN (:ids)',
|
|
{
|
|
replacements: { ids: seedUserIds },
|
|
type: QueryTypes.SELECT,
|
|
},
|
|
);
|
|
const existingIds = new Set(existingRows.map((row) => row.id));
|
|
const rowsToInsert = createAdminUserRows().filter(
|
|
(row) => !existingIds.has(row.id),
|
|
);
|
|
|
|
if (rowsToInsert.length > 0) {
|
|
await queryInterface.bulkInsert('users', rowsToInsert);
|
|
}
|
|
},
|
|
|
|
async down(queryInterface) {
|
|
await queryInterface.bulkDelete(
|
|
'users',
|
|
{
|
|
id: {
|
|
[Op.in]: seedUserIds,
|
|
},
|
|
},
|
|
{},
|
|
);
|
|
},
|
|
};
|
|
|
|
export default adminUserSeeder;
|