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: untranslate non-english preferences in db #662

Merged
merged 1 commit into from
Feb 12, 2024
Merged
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
122 changes: 122 additions & 0 deletions backend/core/src/migration/1707508859920-multiselectTranslationFix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class multiselectTranslationFix1707508859920 implements MigrationInterface {
name = "multiselectTranslationFix1707508859920"

// construct translation object for multiselect questions
listingPrefencesTranslations = {
en: [
{
text: "Historical BART Displacement ",
options: [
"Someone in my household already has a BART Construction Displacement preference certificate.",
"Someone in my household is applying for, or will apply for, a BART Construction Displacement preference certificate.",
"Nobody in my household has been displaced, or has family that has been displaced, due to BART construction.",
],
},
{
text: "Households with Children ",
options: [
"My household includes children under the age of 18.",
"My household does not include children",
],
},
{
text: "Displacement due to Eviction",
options: [
"I, or a member of my household, was displaced due to no-fault or nonpayment-related eviction within the past seven years in Berkeley.",
"No one in my household was displaced due to no-fault or nonpayment-related eviction within the past seven years in Berkeley.",
],
},
{
text: "Displacement due to Foreclosure",
options: [
"Someone in my household already has a Foreclosure Displacement preference certificate.",
"Someone in my household is applying for, or will apply for, a Foreclosure Displacement preference certificate.",
"Nobody in my household has been displaced due to foreclosure.",
],
},
{
text: "Residents or Former Residents of Redlined Neighborhoods",
options: [
"I or a member of my household live or lived in a formerly redlined neighborhood in Berkeley.",
"No one in my household lives or formerly lived in a redlined neighborhood.",
],
},
{
text: "Descendants of Residents of Redlined Neighborhoods",
options: [
"At least one of my parents or grandparents lives or lived in a formerly redlined neighborhood in Berkeley",
"I do not qualify for this preference",
],
},
{
text: "Homeless or at Risk of Homelessness",
options: [
"I, or someone in my household, live in Berkeley, and I have somewhere to stay, but it isn't permanent.",
"I, or someone in my household, am/is homeless and living in Berkeley, or am homeless and had a previous address in Berkeley.",
"I do not qualify for this preference.",
],
},
{
text: "Berkeley Housing Authority Preference",
options: [
"My household lives in the City of Berkeley or formerly lived in the City of Berkeley; or someone in my household works in the City of Berkeley or has been hired to work in the City of Berkeley",
"I or a member of my household is a veteran",
"I or someone in my household is 62 years or older and/or disabled",
"My household includes two or more people",
"I don't want to be considered for this preference",
],
},
],
}

public async up(queryRunner: QueryRunner): Promise<void> {
// get application id, language, program, preference where application language was not english
const applications = await queryRunner.query(`
SELECT
a.id,
a.language,
a.preferences
FROM applications a
WHERE (a.programs != '[]')
AND a.language != 'en'
AND a.listing_id = '123f4226-3f3d-4311-9e54-49e47ecfb068'
ORDER BY a.created_at desc
`)

const promiseArray: Promise<any>[] = []
for (const app of applications) {
promiseArray.push(this.untranslate(app, queryRunner))
}
await Promise.all(promiseArray)
}

public untranslateHelper(dataSet: any[]) {
return dataSet.map((preference) => {
const englishPreference = this.listingPrefencesTranslations.en.find(
(translation) => translation.text === preference.key
)
const options = preference.options.map((option, index) => {
const newOption = { ...option, key: englishPreference.options[index] }
return newOption
})
return { ...preference, options: options }
})
}

public untranslate(app: any, queryRunner: QueryRunner) {
const updatePreferences = this.untranslateHelper(app.preferences)

return queryRunner.query(
`
UPDATE applications
SET preferences = $1
WHERE id = $2
`,
[JSON.stringify(updatePreferences), app.id]
)
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
Loading