Skip to content

Commit

Permalink
Merge pull request #1659 from bcgov/bugfix/ALCS-1907-decision-indexin…
Browse files Browse the repository at this point in the history
…g-on-pr

Fix for PR decision indexing
  • Loading branch information
mhuseinov authored May 7, 2024
2 parents d6d77ca + 1f4c952 commit 644275d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
add_timezone_and_keep_date_part,
OatsToAlcsDecisionOutcomes,
AlcsPlanningReviewOutcomes,
to_alcs_format,
get_now_with_offset,
)
from db import inject_conn_pool
from psycopg2.extras import RealDictCursor, execute_batch
Expand Down Expand Up @@ -39,6 +41,8 @@ def process_planning_review_decisions(conn=None, batch_size=BATCH_UPLOAD_SIZE):
failed_inserts_count = 0
successful_inserts_count = 0
last_planning_decision_id = 0
last_file_number = 0
last_decision_date = ""

with open(
"planning_review/sql/decisions/planning_review_decisions_insert.sql",
Expand All @@ -47,10 +51,16 @@ def process_planning_review_decisions(conn=None, batch_size=BATCH_UPLOAD_SIZE):
) as sql_file:
query = sql_file.read()
while True:
query_condition = ""
if last_decision_date:
query_condition = f"WHERE pr.file_number::bigint >= {last_file_number} AND opd.decision_date >= {last_decision_date} AND opd.planning_decision_id > {last_planning_decision_id} ORDER BY pr.file_number::bigint, opd.decision_date, opd.planning_decision_id;"
else:
query_condition = f"WHERE pr.file_number::bigint >= {last_file_number} AND opd.planning_decision_id > {last_planning_decision_id} ORDER BY pr.file_number::bigint, opd.decision_date, opd.planning_decision_id;"

cursor.execute(
f"""
{query}
WHERE opd.planning_decision_id > {last_planning_decision_id} ORDER BY opd.planning_decision_id;
{query_condition}
"""
)

Expand All @@ -64,9 +74,10 @@ def process_planning_review_decisions(conn=None, batch_size=BATCH_UPLOAD_SIZE):
successful_inserts_count = successful_inserts_count + len(
inserted_data
)
last_planning_decision_id = dict(inserted_data[-1])[
"planning_decision_id"
]
last_record = dict(inserted_data[-1])
last_planning_decision_id = last_record["planning_decision_id"]
last_file_number = last_record["file_number"]
last_decision_date = last_record["decision_date"]

logger.debug(
f"Retrieved/updated items count: {len(inserted_data)}; total successfully inserted planning referral so far {successful_inserts_count}; last updated planning_decision_id: {last_planning_decision_id}"
Expand Down Expand Up @@ -106,7 +117,8 @@ def _insert_base_fields(conn, batch_size, cursor, rows):
outcome_code,
resolution_number,
resolution_year,
date
date,
created_at
)
VALUES (
%(review_uuid)s,
Expand All @@ -116,13 +128,16 @@ def _insert_base_fields(conn, batch_size, cursor, rows):
%(outcome_code)s,
%(resolution_number)s,
%(resolution_year)s,
%(date)s
%(date)s,
%(created_at)s
)
ON CONFLICT DO NOTHING;
"""


def _prepare_oats_planning_review_data(row_data_list):
mapped_data_list = []
insert_index = 0
for row in row_data_list:
mapped_data_list.append(
{
Expand All @@ -134,8 +149,10 @@ def _prepare_oats_planning_review_data(row_data_list):
"outcome_code": _map_outcome_code(row),
"resolution_number": row["resolution_number"],
"resolution_year": _map_resolution_year(row),
"created_at": to_alcs_format(get_now_with_offset(insert_index)),
}
)
insert_index += 1

return mapped_data_list

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ SELECT
opd.planning_acceptance_code,
pr."uuid",
opd.resolution_number,
opd.planning_decision_id
opd.planning_decision_id,
pr.file_number
FROM
oats.oats_planning_decisions opd
JOIN alcs.planning_review pr ON opd.planning_review_id::TEXT = pr.file_number
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { PlanningReviewDecisionService } from './planning-review-decision.servic
@UseGuards(RolesGuard)
export class PlanningReviewDecisionController {
constructor(
private plannigReviewDecisionService: PlanningReviewDecisionService,
private planningReviewDecisionService: PlanningReviewDecisionService,
@InjectMapper() private mapper: Mapper,
) {}

Expand All @@ -42,7 +42,7 @@ export class PlanningReviewDecisionController {
@Param('fileNumber') fileNumber,
): Promise<PlanningReviewDecisionDto[]> {
const decisions =
await this.plannigReviewDecisionService.getByFileNumber(fileNumber);
await this.planningReviewDecisionService.getByFileNumber(fileNumber);

return await this.mapper.mapArrayAsync(
decisions,
Expand All @@ -54,7 +54,7 @@ export class PlanningReviewDecisionController {
@Get('/codes')
@UserRoles(...ANY_AUTH_ROLE)
async getCodes() {
const codes = await this.plannigReviewDecisionService.fetchCodes();
const codes = await this.planningReviewDecisionService.fetchCodes();
return await this.mapper.mapArrayAsync(
codes.outcomes,
PlanningReviewDecisionOutcomeCode,
Expand All @@ -65,7 +65,7 @@ export class PlanningReviewDecisionController {
@Get('/:uuid')
@UserRoles(...ANY_AUTH_ROLE)
async get(@Param('uuid') uuid: string): Promise<PlanningReviewDecisionDto> {
const decision = await this.plannigReviewDecisionService.get(uuid);
const decision = await this.planningReviewDecisionService.get(uuid);

return this.mapper.mapAsync(
decision,
Expand All @@ -80,7 +80,7 @@ export class PlanningReviewDecisionController {
@Body() createDto: CreatePlanningReviewDecisionDto,
): Promise<PlanningReviewDecisionDto> {
const newDecision =
await this.plannigReviewDecisionService.create(createDto);
await this.planningReviewDecisionService.create(createDto);

return this.mapper.mapAsync(
newDecision,
Expand All @@ -95,7 +95,7 @@ export class PlanningReviewDecisionController {
@Param('uuid') uuid: string,
@Body() updateDto: UpdatePlanningReviewDecisionDto,
): Promise<PlanningReviewDecisionDto> {
const updatedDecision = await this.plannigReviewDecisionService.update(
const updatedDecision = await this.planningReviewDecisionService.update(
uuid,
updateDto,
);
Expand All @@ -110,7 +110,7 @@ export class PlanningReviewDecisionController {
@Delete('/:uuid')
@UserRoles(...ANY_AUTH_ROLE)
async delete(@Param('uuid') uuid: string) {
return await this.plannigReviewDecisionService.delete(uuid);
return await this.planningReviewDecisionService.delete(uuid);
}

@Post('/:uuid/file')
Expand All @@ -121,7 +121,7 @@ export class PlanningReviewDecisionController {
}

const file = req.body.file;
await this.plannigReviewDecisionService.attachDocument(
await this.planningReviewDecisionService.attachDocument(
decisionUuid,
file,
req.user.entity,
Expand All @@ -138,7 +138,7 @@ export class PlanningReviewDecisionController {
@Param('documentUuid') documentUuid: string,
@Body() body: { fileName: string },
) {
await this.plannigReviewDecisionService.updateDocument(
await this.planningReviewDecisionService.updateDocument(
documentUuid,
body.fileName,
);
Expand All @@ -154,7 +154,7 @@ export class PlanningReviewDecisionController {
@Param('fileUuid') documentUuid: string,
) {
const downloadUrl =
await this.plannigReviewDecisionService.getDownloadUrl(documentUuid);
await this.planningReviewDecisionService.getDownloadUrl(documentUuid);
return {
url: downloadUrl,
};
Expand All @@ -166,7 +166,7 @@ export class PlanningReviewDecisionController {
@Param('uuid') decisionUuid: string,
@Param('fileUuid') documentUuid: string,
) {
const downloadUrl = await this.plannigReviewDecisionService.getDownloadUrl(
const downloadUrl = await this.planningReviewDecisionService.getDownloadUrl(
documentUuid,
true,
);
Expand All @@ -181,7 +181,7 @@ export class PlanningReviewDecisionController {
@Param('uuid') decisionUuid: string,
@Param('fileUuid') documentUuid: string,
) {
await this.plannigReviewDecisionService.deleteDocument(documentUuid);
await this.planningReviewDecisionService.deleteDocument(documentUuid);
return {};
}

Expand All @@ -190,7 +190,7 @@ export class PlanningReviewDecisionController {
async getNextAvailableResolutionNumber(
@Param('resolutionYear') resolutionYear: number,
) {
return this.plannigReviewDecisionService.generateResolutionNumber(
return this.planningReviewDecisionService.generateResolutionNumber(
resolutionYear,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { MultipartFile } from '@fastify/multipart';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, IsNull, Repository } from 'typeorm';
import { IsNull, Repository } from 'typeorm';
import {
DOCUMENT_SOURCE,
DOCUMENT_SYSTEM,
Expand Down

0 comments on commit 644275d

Please sign in to comment.