Skip to content

Commit

Permalink
error if user tries to create a place with an in-use name
Browse files Browse the repository at this point in the history
  • Loading branch information
freddieptf committed Oct 29, 2024
1 parent 496066f commit 691eebc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/cht-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ export class ChtApi {
return { parent, sibling };
};

contactByType = async (placeType: string, property: string, value: string): Promise<{ name: String; parent: String }[]> => {
const url = `medic/_design/medic-client/_view/contacts_by_type_freetext`;
const params = {
include_docs: true,
key: JSON.stringify([placeType, property + ':' + value.toLowerCase()])
};
const resp = await this.axiosInstance.get(url, { params });
return resp.data.rows.map((row: any) => {
return {
name: row.doc.name,
parent: row.doc.parent?._id
};
});
};

getPlacesWithType = async (placeType: string)
: Promise<RemotePlace[]> => {
const url = `medic/_design/medic-client/_view/contacts_by_type_freetext`;
Expand Down
6 changes: 6 additions & 0 deletions src/services/upload.new.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Config } from '../config';
import { ChtApi, PlacePayload } from '../lib/cht-api';
import Place from './place';
import { Uploader } from './upload-manager';
Expand All @@ -14,6 +15,11 @@ export class UploadNewPlace implements Uploader {
};

handlePlacePayload = async (place: Place, payload: PlacePayload): Promise<string> => {
const contacts = await this.chtApi.contactByType(payload.contact_type, 'name', payload.name);
if (contacts.length > 0 && contacts.some(c => c.parent === payload.parent)) {
const contactType = Config.getContactType(payload.contact_type);
throw new Error(contactType.friendly + ' with name "' + payload.name + '" already exists');
}
return await this.chtApi.createPlace(payload);
};

Expand Down
11 changes: 11 additions & 0 deletions test/services/upload-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ describe('services/upload-manager.ts', () => {
expect(chtApi.createUser.callCount).to.eq(placeCount);
expect(places.find(p => !p.isCreated)).to.be.undefined;
});

it('err when creating place with duplicate name in parent place', async () => {
const { fakeFormData, contactType, sessionCache, chtApi } = await createMocks();
const place = await PlaceFactory.createOne(fakeFormData, contactType, sessionCache, chtApi);
chtApi.contactByType.resolves([{ name: '', parent: place.asChtPayload('test').parent }]);
const uploadManager = new UploadManager();
await uploadManager.doUpload([place], chtApi);
expect(chtApi.contactByType.calledOnce).to.be.true;
expect(place.isCreated).to.be.false;
});

it('required attributes can be inherited during replacement', async () => {
const { remotePlace, sessionCache, contactType, fakeFormData, chtApi } = await createMocks();
Expand Down Expand Up @@ -392,6 +402,7 @@ async function createMocks() {
updateContactParent: sinon.stub().resolves('created-contact-id'),
createUser: sinon.stub().resolves(),

contactByType: sinon.stub().resolves([]),
getParentAndSibling: sinon.stub().resolves({ parent: {}, sibling: {} }),
createContact: sinon.stub().resolves('replacement-contact-id'),
updatePlace: sinon.stub().resolves({
Expand Down

0 comments on commit 691eebc

Please sign in to comment.