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

feat: correction script for sparks homes application preferences #795

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions api/src/controllers/script-runner.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,19 @@ export class ScirptRunnerController {
): Promise<SuccessDTO> {
return await this.scriptRunnerService.updateCodeExpirationTranslations(req);
}

@Put('correctApplicationPreferenceDataForSparksHomes')
@ApiOperation({
summary:
'A script that updates the preference keys for applications on Spark Homes',
operationId: 'correctApplicationPreferenceDataForSparksHomes',
})
@ApiOkResponse({ type: SuccessDTO })
async correctApplicationPreferenceDataForSparksHomes(
@Request() req: ExpressRequest,
): Promise<SuccessDTO> {
return await this.scriptRunnerService.correctApplicationPreferenceDataForSparksHomes(
req,
);
}
}
71 changes: 71 additions & 0 deletions api/src/services/script-runner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,77 @@ export class ScriptRunnerService {
return { success: true };
}

/**
* @param req incoming request object
* @returns successDTO
* @description updates the preference keys for applications on Spark Homes
*/
async correctApplicationPreferenceDataForSparksHomes(
req: ExpressRequest,
): Promise<SuccessDTO> {
// script runner standard start up
const requestingUser = mapTo(User, req['user']);
await this.markScriptAsRunStart(
'Correct application preference data for Sparks Homes',
requestingUser,
);

const applications = await this.prisma.applications.findMany({
select: {
id: true,
preferences: true,
},
where: { listingId: 'a055ce66-a074-4f3a-b67b-6776bec9926e' },
});

const options = [
{
original: 'Live in %{county} County Preference',
new: 'Live in the City of Hayward',
},
{
original: 'Work in %{county} County Preference',
new: 'Work in the City of Hayward',
},
];

console.log(`Updating ${applications.length} applications`);
for (const applicaiton of applications) {
const blob = applicaiton.preferences;

const preference = blob[0];

preference.options = preference.options.map((option) => {
if (option.key === options[0].original) {
return {
...option,
key: options[0].new,
};
} else if (option.key === options[1].original) {
return {
...option,
key: options[1].new,
};
}
});

await this.prisma.applications.update({
data: {
preferences: [preference] as unknown as Prisma.JsonArray,
},
where: {
id: applicaiton.id,
},
});
}

await this.markScriptAsComplete(
'Correct application preference data for Sparks Homes',
requestingUser,
);
return { success: true };
}

/**
this is simply an example
*/
Expand Down
65 changes: 65 additions & 0 deletions api/test/unit/services/script-runner.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,71 @@ describe('Testing script runner service', () => {
});
});

it('should correct application preference data for sparks home', async () => {
const id = randomUUID();
prisma.scriptRuns.findUnique = jest.fn().mockResolvedValue(null);
prisma.scriptRuns.create = jest.fn().mockResolvedValue(null);
prisma.scriptRuns.update = jest.fn().mockResolvedValue(null);
prisma.applications.findMany = jest.fn().mockResolvedValue([
{
id: id,
preferences: [
{
options: [
{ key: 'Live in %{county} County Preference' },
{ key: 'Work in %{county} County Preference' },
],
},
],
},
]);
prisma.applications.update = jest.fn().mockResolvedValue(null);

const scriptName = 'Correct application preference data for Sparks Homes';
const res = await service.correctApplicationPreferenceDataForSparksHomes({
user: {
id,
userRoles: { isAdmin: true },
} as unknown as User,
} as unknown as ExpressRequest);
expect(res.success).toEqual(true);
expect(prisma.scriptRuns.findUnique).toHaveBeenCalledWith({
where: {
scriptName,
},
});
expect(prisma.scriptRuns.create).toHaveBeenCalledWith({
data: {
scriptName,
triggeringUser: id,
},
});
expect(prisma.scriptRuns.update).toHaveBeenCalledWith({
data: {
didScriptRun: true,
triggeringUser: id,
},
where: {
scriptName,
},
});
expect(prisma.applications.update).toHaveBeenCalledWith({
data: {
preferences: [
{
options: [
{ key: 'Live in the City of Hayward' },
{ key: 'Work in the City of Hayward' },
],
},
],
},
where: {
id,
},
});
});

it('should transfer data', async () => {
prisma.listings.updateMany = jest.fn().mockResolvedValue({ count: 1 });

Expand Down
89 changes: 67 additions & 22 deletions shared-helpers/src/types/backend-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2167,28 +2167,6 @@ export class ScriptRunnerService {
axios(configs, resolve, reject)
})
}
/**
* A script that creates a new reserved community type
*/
createNewReservedCommunityType(
params: {
/** requestBody */
body?: IdDTO
} = {} as any,
options: IRequestOptions = {}
): Promise<SuccessDTO> {
return new Promise((resolve, reject) => {
let url = basePath + "/scriptRunner/createNewReservedCommunityType"

const configs: IRequestConfig = getConfigs("put", "application/json", url, options)

let data = params.body

configs.data = data

axios(configs, resolve, reject)
})
}
/**
* A script that takes in a standardized string and outputs the input for the ami chart create endpoint
*/
Expand Down Expand Up @@ -2256,6 +2234,62 @@ export class ScriptRunnerService {

configs.data = data

axios(configs, resolve, reject)
})
}
/**
* A script that creates a new reserved community type
*/
createNewReservedCommunityType(
params: {
/** requestBody */
body?: CommunityTypeDTO
} = {} as any,
options: IRequestOptions = {}
): Promise<SuccessDTO> {
return new Promise((resolve, reject) => {
let url = basePath + "/scriptRunner/createNewReservedCommunityType"

const configs: IRequestConfig = getConfigs("put", "application/json", url, options)

let data = params.body

configs.data = data

axios(configs, resolve, reject)
})
}
/**
* A script that updates single use code translations to show extended expiration time
*/
updateCodeExpirationTranslations(options: IRequestOptions = {}): Promise<SuccessDTO> {
return new Promise((resolve, reject) => {
let url = basePath + "/scriptRunner/updateCodeExpirationTranslations"

const configs: IRequestConfig = getConfigs("put", "application/json", url, options)

let data = null

configs.data = data

axios(configs, resolve, reject)
})
}
/**
* A script that updates the preference keys for applications on Spark Homes
*/
correctApplicationPreferenceDataForSparksHomes(
options: IRequestOptions = {}
): Promise<SuccessDTO> {
return new Promise((resolve, reject) => {
let url = basePath + "/scriptRunner/correctApplicationPreferenceDataForSparksHomes"

const configs: IRequestConfig = getConfigs("put", "application/json", url, options)

let data = null

configs.data = data

axios(configs, resolve, reject)
})
}
Expand Down Expand Up @@ -5857,6 +5891,17 @@ export interface AmiChartImportDTO {
jurisdictionId: string
}

export interface CommunityTypeDTO {
/** */
id: string

/** */
name: string

/** */
description?: string
}

export interface ApplicationCsvQueryParams {
/** */
id: string
Expand Down
Loading