bullpen/backend/seeders/02-users-test.js

75 lines
3.2 KiB
JavaScript

const bcrypt = require("bcryptjs");
const process = require('process');
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, /*Sequelize*/) {
if (process.env.NODE_ENV !== 'test') return;
await queryInterface.bulkInsert('Authentications', [
{ email: 'player@example.com', password: bcrypt.hashSync('hash1234', 8), createdAt: new Date(), updatedAt: new Date() },
{ email: 'coach@example.com', password: bcrypt.hashSync('hash2345', 8), createdAt: new Date(), updatedAt: new Date() },
{ email: 'admin@example.com', password: bcrypt.hashSync('hash3456', 8), createdAt: new Date(), updatedAt: new Date() },
]);
const auths = await queryInterface.select(null, 'Authentications');
const playerAuthId = auths.filter((auth) => auth.email === 'player@example.com').map((auth) => auth.id).shift();
const coachAuthId = auths.filter((auth) => auth.email === 'coach@example.com').map((auth) => auth.id).shift();
const adminAuthId = auths.filter((auth) => auth.email === 'admin@example.com').map((auth) => auth.id).shift();
await queryInterface.bulkInsert('Users', [{
firstName: 'Alice',
lastName: 'Player',
dateOfBirth: '1990-01-01',
gender: 'female',
handedness: 'right',
authId: playerAuthId,
createdAt: new Date(),
updatedAt: new Date(),
}, {
firstName: 'Bob',
lastName: 'Coach',
dateOfBirth: '1985-05-05',
gender: 'male',
handedness: 'left',
authId: coachAuthId,
createdAt: new Date(),
updatedAt: new Date(),
}, {
firstName: 'Charlie',
lastName: 'Admin',
dateOfBirth: '1980-03-03',
gender: 'other',
handedness: 'both',
authId: adminAuthId,
createdAt: new Date(),
updatedAt: new Date(),
}]
);
const users = await queryInterface.select(null, 'Users');
const aliceId = users.filter((user) => user.firstName === 'Alice').map((user) => user.id).shift();
const bobId = users.filter((user) => user.firstName === 'Bob').map((user) => user.id).shift();
const charlieId = users.filter((user) => user.firstName === 'Charlie').map((user) => user.id).shift();
const roles = await queryInterface.select(null, 'Roles');
const playerId = roles.filter((role) => role.name === 'player').map((role) => role.id).shift();
const coachId = roles.filter((role) => role.name === 'coach').map((role) => role.id).shift();
const adminId = roles.filter((role) => role.name === 'admin').map((role) => role.id).shift();
await queryInterface.bulkInsert('UserRoles', [
{ userId: aliceId, roleId: playerId, createdAt: new Date(), updatedAt: new Date() },
{ userId: bobId, roleId: coachId, createdAt: new Date(), updatedAt: new Date() },
{ userId: charlieId, roleId: adminId, createdAt: new Date(), updatedAt: new Date() },
]);
},
async down (queryInterface, /*Sequelize*/) {
if (process.env.NODE_ENV !== 'test') return;
await queryInterface.dropTable('Authentications');
await queryInterface.dropTable('Users');
await queryInterface.dropTable('UserRoles');
}
};