-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make seed data * rename branches and skills * ignore console statement * change test user names
- Loading branch information
1 parent
763da8c
commit 42de004
Showing
3 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
import { PrismaClient, Role, PostingType, PostingStatus } from "@prisma/client"; | ||
import { addDays, setTime } from "../utilities/dateUtils"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
const users = [ | ||
{ | ||
firstName: "Anastasia", | ||
lastName: "Admin", | ||
authId: process.env.ADMIN_UID, | ||
role: Role.ADMIN, | ||
}, | ||
{ | ||
firstName: "Valorie", | ||
lastName: "Volunteer", | ||
authId: process.env.VOLUNTEER_UID, | ||
role: Role.VOLUNTEER, | ||
volunteer: { | ||
create: { | ||
pronouns: "they/them", | ||
hireDate: new Date(), | ||
dateOfBirth: new Date("August 19, 2000 23:15:30"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
firstName: "Edna", | ||
lastName: "Employee", | ||
authId: process.env.EMPLOYEE_UID, | ||
role: Role.EMPLOYEE, | ||
}, | ||
]; | ||
|
||
const branches = [ | ||
{ | ||
name: "Kitchen", | ||
}, | ||
{ | ||
name: "Arts", | ||
}, | ||
{ | ||
name: "Git Branch", | ||
}, | ||
]; | ||
|
||
const skills = [ | ||
{ | ||
name: "Danish", | ||
}, | ||
{ | ||
name: "CPR", | ||
}, | ||
{ | ||
name: "Breakdancing", | ||
}, | ||
]; | ||
|
||
const today = new Date(); | ||
const postings = [ | ||
{ | ||
title: "Danish Instructor", | ||
type: PostingType.INDIVIDUAL, | ||
status: PostingStatus.PUBLISHED, | ||
description: "Lead a Danish making class in Danish", | ||
startDate: addDays(today, 30), | ||
endDate: addDays(today, 50), | ||
autoClosingDate: addDays(today, 20), | ||
numVolunteers: 3, | ||
shifts: { | ||
create: [ | ||
{ | ||
startTime: setTime(addDays(today, 31), 13, 30), | ||
endTime: setTime(addDays(today, 31), 14, 30), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 33), 13, 30), | ||
endTime: setTime(addDays(today, 33), 14, 30), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 35), 13, 30), | ||
endTime: setTime(addDays(today, 35), 14, 30), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 35), 13, 30), | ||
endTime: setTime(addDays(today, 35), 14, 30), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 37), 13, 30), | ||
endTime: setTime(addDays(today, 37), 14, 30), | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
title: "Criossant Making", | ||
type: PostingType.INDIVIDUAL, | ||
status: PostingStatus.DRAFT, | ||
description: "Help with a criossant making class.", | ||
startDate: addDays(today, 20), | ||
endDate: addDays(today, 30), | ||
autoClosingDate: addDays(today, 15), | ||
numVolunteers: 2, | ||
shifts: { | ||
create: [ | ||
{ | ||
startTime: setTime(addDays(today, 21), 18, 0), | ||
endTime: setTime(addDays(today, 21), 19, 0), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 23), 18, 0), | ||
endTime: setTime(addDays(today, 23), 19, 0), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 25), 18, 0), | ||
endTime: setTime(addDays(today, 25), 19, 0), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 27), 18, 0), | ||
endTime: setTime(addDays(today, 27), 19, 0), | ||
}, | ||
{ | ||
startTime: setTime(addDays(today, 29), 18, 0), | ||
endTime: setTime(addDays(today, 29), 19, 0), | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
|
||
const main = async () => { | ||
const seedBranches = await prisma.$transaction( | ||
branches.map((branch) => | ||
prisma.branch.upsert({ | ||
where: { name: branch.name }, | ||
update: {}, | ||
create: { | ||
...branch, | ||
}, | ||
}), | ||
), | ||
); | ||
|
||
const employee = { | ||
create: { | ||
branch: { | ||
connect: { | ||
id: seedBranches[0].id, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const seedSkills = await prisma.$transaction( | ||
skills.map((skill) => | ||
prisma.skill.upsert({ | ||
where: { name: skill.name }, | ||
update: {}, | ||
create: { | ||
...skill, | ||
}, | ||
}), | ||
), | ||
); | ||
|
||
const seedUsers = await prisma.$transaction( | ||
users.map((user) => { | ||
if (!user.authId) { | ||
throw new Error( | ||
`Test user auth id for ${user.role} is not in environment variables.`, | ||
); | ||
} | ||
|
||
return prisma.user.upsert({ | ||
where: { authId: user.authId }, | ||
update: {}, | ||
create: { | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
authId: user.authId, | ||
role: user.role, | ||
volunteer: user.volunteer, | ||
employee: user.role === Role.EMPLOYEE ? employee : {}, | ||
}, | ||
include: { | ||
employee: { | ||
include: { | ||
postings: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}), | ||
); | ||
|
||
const seedEmployee = seedUsers.find((user) => user.role === "EMPLOYEE"); | ||
if (seedEmployee?.employee && !seedEmployee.employee.postings.length) { | ||
await prisma.$transaction( | ||
postings.map((posting) => | ||
prisma.posting.create({ | ||
data: { | ||
...posting, | ||
branch: { | ||
connect: { | ||
id: seedBranches[0].id, | ||
}, | ||
}, | ||
employees: { | ||
connect: [{ id: seedEmployee?.employee?.id }], | ||
}, | ||
skills: { | ||
connect: [{ id: seedSkills[0].id }, { id: seedSkills[1].id }], | ||
}, | ||
}, | ||
}), | ||
), | ||
); | ||
} | ||
}; | ||
|
||
main() | ||
.catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
process.exit(1); | ||
}) | ||
.finally(async () => { | ||
await prisma.$disconnect(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const addDays = (date: Date, daysToAdd: number): Date => { | ||
return new Date(new Date().setDate(date.getDate() + daysToAdd)); | ||
}; | ||
|
||
export const setTime = (date: Date, hours: number, minutes: number): Date => { | ||
return new Date(date.setHours(hours, minutes)); | ||
}; |