Skip to content

Commit

Permalink
feat: update ami chart script (bloom-housing#4408) (#796)
Browse files Browse the repository at this point in the history
* feat: update ami chart script

* feat: update backend swagger

* feat: correct imports
  • Loading branch information
mcgarrye authored Nov 4, 2024
1 parent d0e639f commit 993a3a4
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/src/controllers/script-runner.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { AdminOrJurisdictionalAdminGuard } from '../guards/admin-or-jurisdiction
import { DataTransferDTO } from '../dtos/script-runner/data-transfer.dto';
import { BulkApplicationResendDTO } from '../dtos/script-runner/bulk-application-resend.dto';
import { AmiChartImportDTO } from '../dtos/script-runner/ami-chart-import.dto';
import { AmiChartUpdateImportDTO } from '../dtos/script-runner/ami-chart-update-import.dto';
import { CommunityTypeDTO } from '../dtos/script-runner/community-type.dto';
import { ApiKeyGuard } from '../guards/api-key.guard';

Expand Down Expand Up @@ -84,6 +85,23 @@ export class ScirptRunnerController {
);
}

@Put('amiChartUpdateImport')
@ApiOperation({
summary:
'A script that takes in a standardized string and outputs the input for the ami chart update endpoint',
operationId: 'amiChartUpdateImport',
})
@ApiOkResponse({ type: SuccessDTO })
async amiChartUpdateImport(
@Body() amiChartUpdateImportDTO: AmiChartUpdateImportDTO,
@Request() req: ExpressRequest,
): Promise<SuccessDTO> {
return await this.scriptRunnerService.amiChartUpdateImport(
req,
amiChartUpdateImportDTO,
);
}

@Put('lotteryTranslations')
@ApiOperation({
summary: 'A script that adds lottery translations to the db',
Expand Down
16 changes: 16 additions & 0 deletions api/src/dtos/script-runner/ami-chart-update-import.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiProperty, OmitType } from '@nestjs/swagger';
import { Expose } from 'class-transformer';
import { IsDefined, IsString } from 'class-validator';
import { AmiChartImportDTO } from './ami-chart-import.dto';
import { ValidationsGroupsEnum } from '../../enums/shared/validation-groups-enum';

export class AmiChartUpdateImportDTO extends OmitType(AmiChartImportDTO, [
'jurisdictionId',
'name',
]) {
@Expose()
@IsString({ groups: [ValidationsGroupsEnum.default] })
@IsDefined({ groups: [ValidationsGroupsEnum.default] })
@ApiProperty()
amiId: string;
}
58 changes: 58 additions & 0 deletions api/src/services/script-runner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { Application } from '../dtos/applications/application.dto';
import { AmiChartImportDTO } from '../dtos/script-runner/ami-chart-import.dto';
import { AmiChartCreate } from '../dtos/ami-charts/ami-chart-create.dto';
import { AmiChartService } from './ami-chart.service';
import { AmiChartUpdate } from '../dtos/ami-charts/ami-chart-update.dto';
import { AmiChartUpdateImportDTO } from '../dtos/script-runner/ami-chart-update-import.dto';

/**
this is the service for running scripts
Expand Down Expand Up @@ -246,6 +248,62 @@ export class ScriptRunnerService {
return { success: true };
}

/**
*
* @param amiChartUpdateImportDTO this is a string in a very specific format like:
* percentOfAmiValue_1 householdSize_1_income_value householdSize_2_income_value \n percentOfAmiValue_2 householdSize_1_income_value householdSize_2_income_value
*
* Copying and pasting from google sheets will not match the format above. You will need to perform the following:
* 1) Find and delete all instances of "%"
* 2) Using the Regex option in the Find and Replace tool, replace /\t with " " and /\n with "\\n"
* See "How to format AMI data for script runner import" in Notion for a more detailed example
* @returns successDTO
* @description takes the incoming AMI Chart string and updates existing AMI Chart in the database
*/
async amiChartUpdateImport(
req: ExpressRequest,
amiChartUpdateImportDTO: AmiChartUpdateImportDTO,
): Promise<SuccessDTO> {
// script runner standard start up
const scriptName = `AMI Chart ${
amiChartUpdateImportDTO.amiId
} update ${new Date()}`;
const requestingUser = mapTo(User, req['user']);
await this.markScriptAsRunStart(scriptName, requestingUser);

const ami = await this.amiChartService.findOne(
amiChartUpdateImportDTO.amiId,
);

// parse incoming string into an amichart create dto
const updateDTO: AmiChartUpdate = {
id: amiChartUpdateImportDTO.amiId,
items: [],
name: ami.name,
};

const rows = amiChartUpdateImportDTO.values.split('\n');
rows.forEach((row: string) => {
const values = row.split(' ');
const percentage = values[0];
values.forEach((value: string, index: number) => {
if (index > 0) {
updateDTO.items.push({
percentOfAmi: Number(percentage),
householdSize: index,
income: Number(value),
});
}
});
});

await this.amiChartService.update(updateDTO);

// script runner standard spin down
await this.markScriptAsComplete(scriptName, requestingUser);
return { success: true };
}

private async addLotteryTranslationsHelper() {
const updateForLanguage = async (
language: LanguagesEnum,
Expand Down
184 changes: 184 additions & 0 deletions api/test/unit/services/script-runner.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,190 @@ describe('Testing script runner service', () => {
});
});

it('should update existing ami chart', 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.amiChart.findUnique = jest.fn().mockResolvedValue({
id: id,
name: 'example name',
});
prisma.amiChart.update = jest.fn().mockResolvedValue(null);

const name = 'example name';
const scriptName = `AMI Chart ${id} update ${new Date()}`;
const valueItem =
'15 18400 21000 23650 26250 28350 30450 32550 34650\n30 39150 44750 50350 55900 60400 64850 69350 73800\n50 65250 74600 83900 93200 100700 108150 115600 123050';
const res = await service.amiChartUpdateImport(
{
user: {
id,
} as unknown as User,
} as unknown as ExpressRequest,
{
amiId: id,
values: valueItem,
},
);
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.amiChart.update).toHaveBeenCalledWith({
data: {
id: undefined,
items: [
{
percentOfAmi: 15,
householdSize: 1,
income: 18400,
},
{
percentOfAmi: 15,
householdSize: 2,
income: 21000,
},
{
percentOfAmi: 15,
householdSize: 3,
income: 23650,
},
{
percentOfAmi: 15,
householdSize: 4,
income: 26250,
},
{
percentOfAmi: 15,
householdSize: 5,
income: 28350,
},
{
percentOfAmi: 15,
householdSize: 6,
income: 30450,
},
{
percentOfAmi: 15,
householdSize: 7,
income: 32550,
},
{
percentOfAmi: 15,
householdSize: 8,
income: 34650,
},
{
percentOfAmi: 30,
householdSize: 1,
income: 39150,
},
{
percentOfAmi: 30,
householdSize: 2,
income: 44750,
},
{
percentOfAmi: 30,
householdSize: 3,
income: 50350,
},
{
percentOfAmi: 30,
householdSize: 4,
income: 55900,
},
{
percentOfAmi: 30,
householdSize: 5,
income: 60400,
},
{
percentOfAmi: 30,
householdSize: 6,
income: 64850,
},
{
percentOfAmi: 30,
householdSize: 7,
income: 69350,
},
{
percentOfAmi: 30,
householdSize: 8,
income: 73800,
},
{
percentOfAmi: 50,
householdSize: 1,
income: 65250,
},
{
percentOfAmi: 50,
householdSize: 2,
income: 74600,
},
{
percentOfAmi: 50,
householdSize: 3,
income: 83900,
},
{
percentOfAmi: 50,
householdSize: 4,
income: 93200,
},
{
percentOfAmi: 50,
householdSize: 5,
income: 100700,
},
{
percentOfAmi: 50,
householdSize: 6,
income: 108150,
},
{
percentOfAmi: 50,
householdSize: 7,
income: 115600,
},
{
percentOfAmi: 50,
householdSize: 8,
income: 123050,
},
],
jurisdictions: undefined,
name: name,
},
include: {
jurisdictions: true,
},
where: {
id: id,
},
});
});

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

Expand Down
31 changes: 31 additions & 0 deletions shared-helpers/src/types/backend-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,28 @@ export class ScriptRunnerService {
axios(configs, resolve, reject)
})
}
/**
* A script that takes in a standardized string and outputs the input for the ami chart update endpoint
*/
amiChartUpdateImport(
params: {
/** requestBody */
body?: AmiChartUpdateImportDTO
} = {} as any,
options: IRequestOptions = {}
): Promise<SuccessDTO> {
return new Promise((resolve, reject) => {
let url = basePath + "/scriptRunner/amiChartUpdateImport"

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

let data = params.body

configs.data = data

axios(configs, resolve, reject)
})
}
/**
* A script that adds lottery translations to the db
*/
Expand Down Expand Up @@ -5892,6 +5914,15 @@ export interface AmiChartImportDTO {
jurisdictionId: string
}


export interface AmiChartUpdateImportDTO {
/** */
values: string

/** */
amiId: string
}

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

0 comments on commit 993a3a4

Please sign in to comment.