Skip to content

Commit

Permalink
Merge branch 'main' into 158-integration-of-user-management-tool-into…
Browse files Browse the repository at this point in the history
…-civ-project
  • Loading branch information
aloundoye authored Jun 14, 2024
2 parents 8d6dbcd + 136998e commit f0dd13e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ A simple user-facing web application using [CHT's API](https://docs.communityhea
* [Moving an Existing Place with the User Management Tool](https://www.loom.com/share/cd9e98aefedb490fae61775c86b9b6fe)
* [CHIS Kenya - CHA Changing a Phone Number for a CHP](https://www.loom.com/share/8a0f629161944567b6ca504ab27ec6cf)
* [How to Bulk Replace CHPs with CHIS User Management Tool](https://www.loom.com/share/33d9395a515741fa9620f7004578de24)
* [How to Resolve eCHIS-KE Logic Error](https://www.loom.com/share/80e130733d094e4a829bdfe063d8b1e6)

For Developers:
* [Running CHT User Management Tool with a Local CHT Instance](https://www.loom.com/share/645cf995a9c44a5ab843628538a019ff)
Expand Down
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.20",
"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
1 change: 1 addition & 0 deletions src/routes/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default async function sessionCache(fastify: FastifyInstance) {
};
});
const tmplData = {
session: req.chtSession,
contactTypes: placeData,
};
return resp.view('src/liquid/place/list.html', tmplData);
Expand Down
1 change: 1 addition & 0 deletions src/routes/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default async function events(fastify: FastifyInstance) {
const html = await fastify.view(
'src/liquid/components/place_item.html',
{
session: req.chtSession,
contactType: {
...place.type,
hierarchy: Config.getHierarchyWithReplacement(place.type, 'desc'),
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 f0dd13e

Please sign in to comment.