-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust finding of suitable applications
- Loading branch information
Showing
7 changed files
with
211 additions
and
15 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,159 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { Tally, TallyData, TallyField } from "@lib/types/tally"; | ||
import { keyUniqueEnforcer, now, options } from "./utils"; | ||
import db from "server/db"; | ||
|
||
enum FieldTypes { | ||
CHECKBOXES = "CHECKBOXES", | ||
MULTIPLE_CHOICE = "MULTIPLE_CHOICE", | ||
DROPDOWN = "DROPDOWN", | ||
} | ||
|
||
enum NormalFieldTypes { | ||
TEXTAREA = "TEXTAREA", | ||
INPUT_TEXT = "INPUT_TEXT", | ||
INPUT_NUMBER = "INPUT_NUMBER", | ||
INPUT_DATE = "INPUT_DATE", | ||
INPUT_EMAIL = "INPUT_EMAIL", | ||
INPUT_LINK = "INPUT_LINK", | ||
} | ||
|
||
type Application = { | ||
content: Tally; | ||
opportunity: number; | ||
}; | ||
|
||
const minNumberOfApplications = parseInt(options.minapplications, 10); | ||
const maxNumberOfApplications = parseInt(options.maxapplications, 10); | ||
|
||
export async function generateApplications(opportunityIds: number[]) { | ||
const applications: Application[] = opportunityIds.flatMap((id) => | ||
Array.from( | ||
{ | ||
length: faker.number.int({ | ||
min: minNumberOfApplications, | ||
max: maxNumberOfApplications, | ||
}), | ||
}, | ||
() => generateApplication(id), | ||
), | ||
); | ||
|
||
for (const application of applications) { | ||
await db.application.create({ | ||
data: { | ||
content: application.content, | ||
opportunity: { connect: { id: application.opportunity } }, | ||
}, | ||
}); | ||
} | ||
|
||
return applications; | ||
} | ||
|
||
function generateApplication(opportunityId: number): Application { | ||
return { | ||
content: generateTallyContent(), | ||
opportunity: opportunityId, | ||
}; | ||
} | ||
|
||
function generateTallyContent(): Tally { | ||
return { | ||
eventId: faker.string.uuid(), | ||
eventType: "FORM_RESPONSE", | ||
createdAt: now.toISOString(), | ||
data: generateTallyData(), | ||
}; | ||
} | ||
|
||
function generateTallyData(): TallyData { | ||
return { | ||
responseId: faker.string.nanoid(6), | ||
submissionId: faker.string.nanoid(6), | ||
respondentId: faker.string.nanoid(6), | ||
formId: faker.string.nanoid(6), | ||
formName: faker.science.chemicalElement().name, | ||
createdAt: now.toISOString(), | ||
fields: Array.from({ length: 2 }, () => generateTallyField()), | ||
}; | ||
} | ||
|
||
function generateTallyField(): TallyField { | ||
const fieldTypes: (string | FieldTypes)[] = Object.values(FieldTypes); | ||
const fieldType = faker.helpers.arrayElement(fieldTypes); | ||
|
||
const baseField = { | ||
label: faker.music.genre(), | ||
key: keyUniqueEnforcer.enforce(() => faker.string.nanoid(6)), | ||
}; | ||
|
||
return generateTallyFieldDetails(baseField, fieldType); | ||
} | ||
|
||
function generateTallyFieldDetails( | ||
baseField: { | ||
label: string; | ||
key: string; | ||
}, | ||
fieldType: string | FieldTypes, | ||
): TallyField { | ||
switch (fieldType) { | ||
case FieldTypes.CHECKBOXES: | ||
const checkbox_options = Array.from( | ||
{ length: faker.number.int({ min: 1, max: 4 }) }, | ||
() => ({ id: faker.string.uuid(), text: faker.music.songName() }), | ||
); | ||
return { | ||
...baseField, | ||
type: FieldTypes.CHECKBOXES, | ||
value: faker.helpers.arrayElements( | ||
checkbox_options.map((o) => o.id), | ||
{ min: 1, max: 4 }, | ||
), | ||
options: checkbox_options, | ||
}; | ||
|
||
case FieldTypes.DROPDOWN: | ||
const dropdown_options = Array.from( | ||
{ length: faker.number.int({ min: 1, max: 4 }) }, | ||
() => ({ id: faker.string.uuid(), text: faker.music.songName() }), | ||
); | ||
return { | ||
...baseField, | ||
type: FieldTypes.DROPDOWN, | ||
value: faker.helpers.arrayElements( | ||
dropdown_options.map((o) => o.id), | ||
1, | ||
), | ||
options: dropdown_options, | ||
}; | ||
|
||
case FieldTypes.MULTIPLE_CHOICE: | ||
const multiple_options = Array.from( | ||
{ length: faker.number.int({ min: 1, max: 4 }) }, | ||
() => ({ id: faker.string.uuid(), text: faker.music.songName() }), | ||
); | ||
return { | ||
...baseField, | ||
type: FieldTypes.MULTIPLE_CHOICE, | ||
value: faker.helpers.arrayElements( | ||
multiple_options.map((o) => o.id), | ||
1, | ||
), | ||
options: multiple_options, | ||
}; | ||
|
||
default: | ||
if ( | ||
Object.values(NormalFieldTypes).includes(fieldType as NormalFieldTypes) | ||
) { | ||
return { | ||
...baseField, | ||
type: fieldType as NormalFieldTypes, | ||
value: faker.lorem.sentence(), | ||
}; | ||
} | ||
throw new Error("Unknown field type"); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { TallyField, TallySchema } from "@lib/schemas/tally"; | ||
import { TallyData, TallyField, TallySchema } from "@lib/schemas/tally"; | ||
import { z } from "zod"; | ||
|
||
export type Tally = z.infer<typeof TallySchema>; | ||
export type TallyData = z.infer<typeof TallyData> | ||
export type TallyField = z.infer<typeof TallyField>; |