Skip to content

Commit

Permalink
Fixed issues with sync encountering empty arrays (#610)
Browse files Browse the repository at this point in the history
* Added better null safety for cases where the sync data is an empty array

* Cleaning out console.logs. Adding null safety for the other requests.

* Re-adding guard to controller.

* Ran formatter
  • Loading branch information
dbayly-freshworks authored Jan 4, 2024
1 parent 223fa11 commit 1a91d87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
28 changes: 18 additions & 10 deletions apps/api/src/applicant/external-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ export class ExternalAPIService {
*/
async saveHa(manager: EntityManager): Promise<void> {
const data = await this.external_request.getHa();
if (Array.isArray(data)) {
if (data.length && Array.isArray(data)) {
const listHa = data.map(item => ({ ...item, title: item.name }));
const result = await manager.upsert(IENHaPcn, listHa, ['id']);
this.logger.log(`${result.raw.length}/${listHa.length} authorities updated`, 'ATS-SYNC');
this.logger.log(`${result?.raw?.length}/${listHa?.length} authorities updated`, 'ATS-SYNC');
} else {
this.logger.log(`No data for Health Authorities found, skipping.`);
}
}

Expand All @@ -102,7 +104,7 @@ export class ExternalAPIService {
*/
async saveUsers(manager: EntityManager): Promise<void> {
const data = await this.external_request.getStaff();
if (Array.isArray(data)) {
if (data?.length && Array.isArray(data)) {
const listUsers = data.map(item => {
return {
user_id: item.id.toLowerCase(),
Expand All @@ -112,7 +114,9 @@ export class ExternalAPIService {
});
const result = await manager.upsert(IENUsers, listUsers, ['email']);

this.logger.log(`${result.raw.length}/${listUsers.length} users updated`, 'ATS-SYNC');
this.logger.log(`${result?.raw?.length || 0}/${data.length} users updated`, 'ATS-SYNC');
} else {
this.logger.log('No user data found, skipping.');
}
}

Expand All @@ -122,9 +126,11 @@ export class ExternalAPIService {
*/
async saveReasons(manager: EntityManager): Promise<void> {
const data = await this.external_request.getReason();
if (Array.isArray(data)) {
if (data.length && Array.isArray(data)) {
const result = await manager.upsert(IENStatusReason, data, ['id']);
this.logger.log(`${result.raw.length}/${data.length} reasons updated`, 'ATS-SYNC');
this.logger.log(`${result?.raw?.length || 0}/${data.length} reasons updated`, 'ATS-SYNC');
} else {
this.logger.log('No Reasons found, skipping.');
}
}

Expand All @@ -144,7 +150,7 @@ export class ExternalAPIService {
};
});
const result = await manager.upsert(IENJobTitle, departments, ['id']);
this.logger.log(`${result.raw.length}/${departments.length} departments updated`, 'ATS-SYNC');
this.logger.log(`${result?.raw?.length || 0}/${data.length} departments updated`, 'ATS-SYNC');
}
}

Expand All @@ -155,13 +161,15 @@ export class ExternalAPIService {
async saveMilestones(manager: EntityManager): Promise<void> {
const data = await this.external_request.getMilestone();

if (Array.isArray(data)) {
if (data.length && Array.isArray(data)) {
const result = await manager.upsert(
IENApplicantStatus,
data.map(row => ({ ...row, status: row.name })), // name -> status
['id'],
);
this.logger.log(`${result.raw.length}/${data.length} milestones updated`, 'ATS-SYNC');
this.logger.log(`${result?.raw?.length || 0}/${data.length} milestones updated`, 'ATS-SYNC');
} else {
this.logger.log(`No milestones found, skipping.`);
}
}

Expand Down Expand Up @@ -421,7 +429,7 @@ export class ExternalAPIService {
.values(milestonesToBeInserted)
.orIgnore(true)
.execute();
this.logger.log(`milestones updated: ${result.raw.length}`, 'ATS-SYNC');
this.logger.log(`milestones updated: ${result?.raw?.length || 0}`, 'ATS-SYNC');
} catch (e) {
this.logger.error(e);
}
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/common/external-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ export class ExternalRequest {
});
}

async getHa() {
async getHa(): Promise<Array<{ id: string; name: string; abbreviation: string }>> {
return this.getData(`/health-authorities`);
}

async getStaff() {
async getStaff(): Promise<Array<{ id: string; name: string; email: string }>> {
const header = {
ApiKey: process.env.HMBC_ATS_AUTH_KEY,
};
return this.getData(`/staff`, header);
}

async getReason() {
async getReason(): Promise<Array<any>> {
return this.getData(`/withdrawal-reasons`);
}

Expand Down

0 comments on commit 1a91d87

Please sign in to comment.