Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix seed #100

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 14 additions & 46 deletions apps/server/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

/*
Hard coded UUIDs for:
Hard coded UUIDs for:
- leadership team department
- chief of staff position
- chief financial officer position
- agg director position
- chief financial officer position
- agg director position
- chief learning engagement position
- chief signature field
- manager signature field
- director signature field
- chief signature field
- manager signature field
- director signature field
*/
const LEADERSHIP_TEAM_UUID = '3f08fe46-a243-4b33-84fa-6702a74f3a5d';
const CHIEF_OF_STAFF_UUID = '5a5b1c25-8bfe-4418-9ba6-b1420d1fedff';
Expand All @@ -21,7 +19,6 @@ const CHIEF_LEARNING_ENGAGEMENT_UUID = '693e8455-50e8-49bc-9d06-755eb24a5bcc';
const CHIEF_SIG_FIELD_UUID = '33b169ed-e98f-4f72-807d-c31c7cb4230d';
const MANAGER_SIG_FIELD_UUID = '1727a4d2-b22c-42de-b63d-5f553e964d75';
const DIR_SIG_FIELD_UUID = '6bd5c08b-f309-4226-8914-7fef4ba631c2';

// type definition for employee data used in upsertEmployee
type EmployeeData = {
id: string;
Expand All @@ -30,7 +27,6 @@ type EmployeeData = {
email: string;
positionId: string;
};

// update or insert employee to database based on the employee id
async function upsertEmployee(empData: EmployeeData) {
await prisma.employee.upsert({
Expand All @@ -47,20 +43,17 @@ async function upsertEmployee(empData: EmployeeData) {
},
});
}

// type definition for position data used in upsertPosition
type PositionData = {
id: string;
name: string;
departmentId: string;
signatureFields: { id: string }[];
};

// update or insert position into database based on position id
async function upsertPosition(data: PositionData) {
const { id, name, departmentId, signatureFields } = data;
const connections = signatureFields.map((sigField) => ({ id: sigField.id }));

return prisma.position.upsert({
where: { id },
update: {},
Expand All @@ -74,7 +67,6 @@ async function upsertPosition(data: PositionData) {
},
});
}

// type definition for mapping signature field names (ex: 'Director', 'Manager') to their data
type SignatureFieldMap = {
[key: string]: {
Expand All @@ -84,7 +76,6 @@ type SignatureFieldMap = {
formTemplateId: string;
};
};

// fetch or create signature field for the given form template based on form template id
async function fetchSignatureFields(
formTemplateId: string,
Expand All @@ -96,9 +87,7 @@ async function fetchSignatureFields(
Manager: MANAGER_SIG_FIELD_UUID,
Director: DIR_SIG_FIELD_UUID,
};

const signatureFieldsMap: SignatureFieldMap = {};

// fetch signaturefield, or create one if it doesn't already exist
for (const name of signatureFieldNames) {
const existingField = await prisma.signatureField.findFirst({
Expand All @@ -107,7 +96,6 @@ async function fetchSignatureFields(
formTemplateId: formTemplateId,
},
});

if (existingField) {
signatureFieldsMap[name] = existingField;
} else {
Expand All @@ -122,10 +110,8 @@ async function fetchSignatureFields(
signatureFieldsMap[name] = createdField;
}
}

return signatureFieldsMap;
}

// type definition for mapping signature field names (ex: 'Director', 'Manager') to their data
type FormInstanceData = {
id: string;
Expand All @@ -135,7 +121,6 @@ type FormInstanceData = {
formTemplateId: string;
signatures: any[];
};

// upsert new form instances
async function upsertFormInstance(formInstanceData: FormInstanceData) {
const { id, name, formDocLink, originatorId, formTemplateId, signatures } =
Expand All @@ -155,18 +140,16 @@ async function upsertFormInstance(formInstanceData: FormInstanceData) {
},
});
}

/* main seeding function to upsert the following data:
- 1 form template
- 1 department (leadership team)
/* main seeding function to upsert the following data:
- 1 form template
- 1 department (leadership team)
- 3 signature fields ('chief', 'director', 'manager')
- 4 positions (chief of staff, cheif financial officer, agg director, chief of learning)
- 4 employees, one for each of the above positions
*/

async function main() {
const testFormLink =
'https://s29.q4cdn.com/175625835/files/doc_downloads/test.pdf';
'Form_Template Test _547fba69-490e-40af-8f72-70eab3815e0e';
// form template
const formTemplate1Id = '1fbccd8a-b00c-472f-a94f-defa8e86e0cf';
await prisma.formTemplate.upsert({
Expand All @@ -178,7 +161,6 @@ async function main() {
formDocLink: testFormLink,
},
});

// leadership team department
const departmentLeadershipTeam = await prisma.department.upsert({
where: { id: LEADERSHIP_TEAM_UUID },
Expand All @@ -188,11 +170,9 @@ async function main() {
name: 'Leadership Team',
},
});

// signature fields
const signatureFieldsMap: SignatureFieldMap =
await fetchSignatureFields(formTemplate1Id);

// positions
const positions = [
{
Expand Down Expand Up @@ -220,47 +200,43 @@ async function main() {
signatureFields: [signatureFieldsMap['Manager']],
},
];

for (const positionData of positions) {
await upsertPosition(positionData);
}

// employees
const employees = [
{
id: '777c1974-3104-4744-ae31-7a9296e7784a',
firstName: 'Helen',
lastName: 'Miao',
email: '[email protected]',
email: '[email protected]',
positionId: CHIEF_OF_STAFF_UUID,
},
{
id: '339cf78e-d13f-4069-b1f7-dee0c64afb31',
firstName: 'Kai',
lastName: 'Zheng',
email: 'email2@gmail.com',
email: 'email2@kaiyangzhenggmail.onmicrosoft.com',
positionId: CHIEF_FIN_OFFICER_UUID,
},
{
id: 'c6de4017-cb1f-44f1-a707-0f38239e0bca',
firstName: 'Iris',
lastName: 'Zhang',
email: 'email3@gmail.com',
email: 'email3@kaiyangzhenggmail.onmicrosoft.com',
positionId: AGG_DIR_UUID,
},
{
id: 'b386ef53-d2d1-4bfd-a44c-55b1750a874e',
firstName: 'Anshul',
lastName: 'Shirude',
email: 'email4@gmail.com',
email: 'email4@kaiyangzhenggmail.onmicrosoft.com',
positionId: CHIEF_LEARNING_ENGAGEMENT_UUID,
},
];

for (const empData of employees) {
await upsertEmployee(empData);
}

// form instances
const formInstances = [
{
Expand Down Expand Up @@ -311,12 +287,10 @@ async function main() {
],
},
];

for (const formInstance of formInstances) {
await upsertFormInstance(formInstance);
}
}

// runs main seeding function
main()
.catch((e) => {
Expand All @@ -327,29 +301,23 @@ main()
.finally(async () => {
const formTemplates = await prisma.formTemplate.findMany();
console.log('Form Templates:', formTemplates);

const departments = await prisma.department.findMany();
console.log('Departments:', departments);

const allPositions = await prisma.position.findMany({
include: {
signatureFields: true,
},
});
console.log('Positions:', JSON.stringify(allPositions, null, 2));

const allEmployees = await prisma.employee.findMany({
include: {
position: true,
},
});
console.log('Employees:', allEmployees);

const allSignatureFields = await prisma.signatureField.findMany();
console.log('Signature Fields:', allSignatureFields);

const allFormInstances = await prisma.formInstance.findMany();
console.log('Form Instances:', allFormInstances);

await prisma.$disconnect();
});
Loading