Skip to content

Commit

Permalink
Fix crash when replacing a user even if the place has no primary cont…
Browse files Browse the repository at this point in the history
…act (#174)

1.3.19
  • Loading branch information
kennsippell authored May 15, 2024
1 parent 1102b5a commit 58e7c62
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cht-user-management",
"version": "1.3.18",
"version": "1.3.19",
"main": "dist/index.js",
"dependencies": {
"@fastify/autoload": "^5.8.0",
Expand Down
6 changes: 4 additions & 2 deletions src/lib/cht-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ export class ChtApi {
delete payloadClone.contact;
delete payloadClone.parent;

const previousPrimaryContact = doc.contact._id;
const previousPrimaryContact = doc.contact?._id;
Object.assign(doc, payloadClone, { contact: { _id: contactId }});
doc.user_attribution ||= {};
doc.user_attribution.previousPrimaryContacts ||= [];
doc.user_attribution.previousPrimaryContacts.push(previousPrimaryContact);
if (previousPrimaryContact) {
doc.user_attribution.previousPrimaryContacts.push(previousPrimaryContact);
}

const putUrl = `medic/${payload._id}`;
console.log('axios.put', putUrl);
Expand Down
2 changes: 1 addition & 1 deletion src/services/upload.replacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class UploadReplacementWithDeletion implements Uploader {
}

const updatedPlaceDoc = await retryOnUpdateConflict<any>(() => this.chtApi.updatePlace(payload, contactId));
const previousPrimaryContact = updatedPlaceDoc.user_attribution.previousPrimaryContacts?.pop();
const previousPrimaryContact = updatedPlaceDoc.user_attribution?.previousPrimaryContacts?.pop();
if (previousPrimaryContact) {
await retryOnUpdateConflict<any>(() => this.chtApi.deleteDoc(previousPrimaryContact));
}
Expand Down
26 changes: 26 additions & 0 deletions test/services/upload-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,32 @@ describe('services/upload-manager.ts', () => {
});
expect(place.isCreated).to.be.true;
});

it('#173 - replacement when place has no primary contact', async () => {
const { remotePlace, sessionCache, contactType, fakeFormData, chtApi } = await createMocks();
const toReplace: RemotePlace = {
id: 'id-replace',
name: 'to-replace',
lineage: [remotePlace.id],
type: 'remote',
};

chtApi.updatePlace.resolves({ _id: 'updated-place-id' });
fakeFormData.hierarchy_replacement = toReplace.name;

chtApi.getPlacesWithType
.resolves([remotePlace])
.onSecondCall()
.resolves([toReplace]);

const place = await PlaceFactory.createOne(fakeFormData, contactType, sessionCache, chtApi);
expect(place.validationErrors).to.be.empty;

const uploadManager = new UploadManager();
await uploadManager.doUpload([place], chtApi);
expect(chtApi.deleteDoc.callCount).to.eq(0);
expect(place.isCreated).to.be.true;
});
});

async function createChu(remotePlace: RemotePlace, chu_name: string, sessionCache: any, chtApi: ChtApi) {
Expand Down

0 comments on commit 58e7c62

Please sign in to comment.