Skip to content

Commit

Permalink
Prevent duplicate codes bump 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmenno committed Oct 28, 2024
1 parent ac46d38 commit 59b3b2a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion PWA/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ReliefBox",
"version": "0.11.0",
"version": "0.12.0",
"description": "A tool for managing the distribution of relief items during humanitarian emergencies.",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions PWA/public/Services/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export class Database {
return beneficiaries.filter((beneficiary) => beneficiary.distributionName == distributionName);
}
async addBeneficiary(beneficiary) {
if (await this.beneficiaryWithCode(beneficiary.code, beneficiary.distributionName)) {
throw "There's already a distribution with code " + beneficiary.code + " in distribution " + beneficiary.distributionName;
}
return this.addElement(ObjectStoreName.beneficiary, beneficiary);
}
async markBeneficiaryAsReceived(beneficiaryCode, distributionName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export class BeneficiaryDataUploadHandler extends ActiveSessionContainer {
const distribution = await database.distributionWithName(distributionName);
if (distribution) {
const beneficiaries = await new BeneficiaryDeserializationService().deserializeFormDataFromRequest(event.request, distributionName);
await beneficiaries.forEach(async (beneficiary) => await database.addBeneficiary(beneficiary));
if (!this.areAllCodesUnique(beneficiaries)) {
throw "All beneficiary codes should be unique";
}
for (const beneficiary of beneficiaries) {
await database.addBeneficiary(beneficiary);
}
this.activeSession.nameOfLastViewedDistribution = distributionName;
const beneficiaryInfoService = new BeneficiaryInfoService(this.activeSession.database);
return await ResponseTools.wrapInHTPLTemplateAndReplaceKeysWithValues(RouteEvents.distributionsHome, {
Expand All @@ -38,4 +43,14 @@ export class BeneficiaryDataUploadHandler extends ActiveSessionContainer {
return ResponseTools.wrapInHtmlTemplate(RouteEvents.uploadDataError);
}
}
areAllCodesUnique(beneficiaries) {
const codes = new Set();
for (const beneficiary of beneficiaries) {
if (codes.has(beneficiary.code)) {
return false;
}
codes.add(beneficiary.code);
}
return true;
}
}
2 changes: 1 addition & 1 deletion PWA/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<!-- <a class="navbar-item">-->
<img src="images/ReliefBox-horizontal-nobackground.png" width="220" height="30">
<!-- </a>-->
<div class="navbar-item">Alpha 0.11.0</div>
<div class="navbar-item">Alpha 0.12.0</div>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
Expand Down
2 changes: 1 addition & 1 deletion PWA/public/upload_error.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="column is-4 is-offset-4">
<div class="box">
<label class="label is-medium">The data that you uploaded is in the wrong format, please use the template.</label>
<label class="label is-medium">The data that you uploaded is in the wrong format, please use the template and make sure all codes are unique.</label>
<br>
<a href="/download_template" class="button is-block is-info is-medium is-fullwidth">Download data template</a>
<br>
Expand Down
4 changes: 4 additions & 0 deletions PWA/src/Services/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export class Database {
}

async addBeneficiary(beneficiary: Beneficiary): Promise<void> {
if(await this.beneficiaryWithCode(beneficiary.code, beneficiary.distributionName)) {
throw "There's already a distribution with code " + beneficiary.code + " in distribution " + beneficiary.distributionName
}

return this.addElement(ObjectStoreName.beneficiary, beneficiary);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BeneficiaryDeserializationService } from "../BeneficiaryDeserialization
import { ResponseTools } from "../ResponseTools.js";
import { BeneficiaryInfoService } from "../BeneficiaryInfoService.js";
import { ActiveSessionContainer } from "../ActiveSession.js";
import { Beneficiary } from "../../Models/Beneficiary.js";

export class BeneficiaryDataUploadHandler extends ActiveSessionContainer implements FetchEventHandler {
canHandleEvent(event: FetchEvent): boolean {
Expand All @@ -19,7 +20,12 @@ export class BeneficiaryDataUploadHandler extends ActiveSessionContainer impleme
const distribution = await database.distributionWithName(distributionName)
if (distribution) {
const beneficiaries = await new BeneficiaryDeserializationService().deserializeFormDataFromRequest(event.request, distributionName)
await beneficiaries.forEach(async (beneficiary) => await database.addBeneficiary(beneficiary))
if(!this.areAllCodesUnique(beneficiaries)) {
throw "All beneficiary codes should be unique"
}
for (const beneficiary of beneficiaries) {
await database.addBeneficiary(beneficiary);
}
this.activeSession.nameOfLastViewedDistribution = distributionName
const beneficiaryInfoService = new BeneficiaryInfoService(this.activeSession.database)

Expand All @@ -40,4 +46,17 @@ export class BeneficiaryDataUploadHandler extends ActiveSessionContainer impleme
return ResponseTools.wrapInHtmlTemplate(RouteEvents.uploadDataError)
}
}

private areAllCodesUnique(beneficiaries: Beneficiary[]): boolean {
const codes = new Set<string>()

for (const beneficiary of beneficiaries) {
if (codes.has(beneficiary.code)) {
return false
}
codes.add(beneficiary.code);
}

return true
}
}

0 comments on commit 59b3b2a

Please sign in to comment.