Skip to content

Commit

Permalink
Make sure beneficiaries that belong to a deleted distribution are als…
Browse files Browse the repository at this point in the history
…o deleted bump 0.19.0
  • Loading branch information
rcmenno committed Nov 11, 2024
1 parent 9ea4aee commit a14369c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 29 deletions.
39 changes: 35 additions & 4 deletions PWA/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,29 @@ Given distribution page is shown
Then "No beneficiary data found" message is shown
and "Add beneficiary button" is visible

and beneficiaries have been added
Then "beneficiaries served: (n / n)" is shown
and "Add beneficiary button" is hidden 🐞
and <numberOfBeneficiaries> beneficiaries have been added
Then "beneficiaries served: (0 / <numberOfBeneficiaries>)" is shown
and "Add beneficiary button" is hidden

When distribution is started or resumed
using typing or scanner
When scanning code for eligle beneficary
Then green box appears showing beneficiary details
When marking as receipient
and going back to distribution page
Then "beneficiaries served: (1 / <numberOfBeneficiaries>)" is shown
When continuing distribution
and going back to distribution page
Then "beneficiaries served: (0 / <numberOfBeneficiaries>)" is shown
When going back to main menu
Then "beneficiaries served: (0 / <numberOfBeneficiaries>)" is shown

When scanning code for ineligible beneficiary
Then red box appears showing beneficiary details
When pressing Mark as recepient

When scanning unknown code
using camera

Given no distributions are known (yet)
When tapping home button
Expand All @@ -114,5 +134,16 @@ Given no distributions are known (yet)
Given one or more distributions are known
When tapping home button
Then distribution page of last viewed distribution is shown


#### High demanding scenarios

Scenario: Correct beneficiaries served message
Given a distribution has been created and beneficiaries have been added
When each beneficiary has individually been mark

Scenario: correct cleanup of previous distribution
Given a distribution is added
And beneficiaries are added to that distribution
And distribution is deleted
When new distribution with same name as previous distribution is added
Then it new distribution has no beneficiaries
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.18.0",
"version": "0.19.0",
"description": "A tool for managing the distribution of relief items during humanitarian emergencies.",
"main": "index.js",
"scripts": {
Expand Down
30 changes: 21 additions & 9 deletions PWA/public/Services/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export class Database {
return this.addElement(ObjectStoreName.distribution, distribution);
}
async deleteDistributionWithName(name) {
return this.removeElement(ObjectStoreName.distribution, await this.keyForDistributionWithName(name));
await this.removeElement(ObjectStoreName.distribution, await this.keyForDistributionWithName(name));
return this.deleteElementIf(ObjectStoreName.beneficiary, (beneficiary) => {
return beneficiary.distributionName == name;
});
}
async beneficiariesForDistributionNamed(distributionName) {
const beneficiaries = await this.readBeneficiaries();
Expand Down Expand Up @@ -137,7 +140,7 @@ export class Database {
return key === "all" ? store.clear() : store.delete(key);
});
}
async updateElementIf(storeName, requirementCheck, updateFunction) {
async performCursorActionIf(storeName, requirementCheck, action) {
const objectStore = await this.objectStore(storeName, "readwrite");
// Open a cursor to iterate over the store
const cursorRequest = objectStore.openCursor();
Expand All @@ -151,15 +154,13 @@ export class Database {
const currentValue = cursor.value;
// Check if the item meets the requirement
if (requirementCheck(currentValue)) {
// Apply the update function
const updatedValue = updateFunction(currentValue);
// Update the record at the current cursor position
const updateRequest = cursor.update(updatedValue);
updateRequest.onerror = (event) => {
console.error('Error updating the object with cursor', event);
throw Error('Error updating the object with cursor');
const cursorRequest = action(cursor);
cursorRequest.onerror = (event) => {
console.error('Error performing action with cursor', event);
throw Error('Error performing action with cursor');
};
updateRequest.onsuccess = () => {
cursorRequest.onsuccess = () => {
console.log('Record updated successfully');
};
}
Expand All @@ -168,6 +169,17 @@ export class Database {
}
};
}
async updateElementIf(storeName, requirementCheck, updateFunction) {
this.performCursorActionIf(storeName, requirementCheck, (cursor) => {
const updatedValue = updateFunction(cursor.value);
return cursor.update(updatedValue);
});
}
async deleteElementIf(storeName, requirementCheck) {
this.performCursorActionIf(storeName, requirementCheck, (cursor) => {
return cursor.delete();
});
}
performRequestForObjectStoreNamed(storeName, transactionMode, makeRequest) {
const open = this.openDatabaseRequest();
return new Promise((resolve, reject) => {
Expand Down
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.18.0</div>
<div class="navbar-item">Alpha 0.19.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
50 changes: 36 additions & 14 deletions PWA/src/Services/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ export class Database {
}

async deleteDistributionWithName(name: string): Promise<void> {
return this.removeElement(
await this.removeElement(
ObjectStoreName.distribution,
await this.keyForDistributionWithName(name)
) as Promise<void>;
)

return this.deleteElementIf(
ObjectStoreName.beneficiary,
(beneficiary: Beneficiary) => {
return beneficiary.distributionName == name
})
}

async beneficiariesForDistributionNamed(
Expand Down Expand Up @@ -207,12 +213,12 @@ export class Database {
);
}

private async updateElementIf<T>(
private async performCursorActionIf<T>(
storeName: ObjectStoreName,
requirementCheck: (item: T) => boolean,
updateFunction: (item: T) => T
action: (cursor: IDBCursorWithValue) => IDBRequest
): Promise<void> {
const objectStore = await this.objectStore(storeName, "readwrite")
const objectStore = await this.objectStore(storeName, "readwrite")

// Open a cursor to iterate over the store
const cursorRequest = objectStore.openCursor();
Expand All @@ -230,18 +236,15 @@ export class Database {

// Check if the item meets the requirement
if (requirementCheck(currentValue)) {
// Apply the update function
const updatedValue = updateFunction(currentValue);

// Update the record at the current cursor position
const updateRequest = cursor.update(updatedValue);
const cursorRequest = action(cursor)

updateRequest.onerror = (event) => {
console.error('Error updating the object with cursor', event);
throw Error('Error updating the object with cursor')
cursorRequest.onerror = (event) => {
console.error('Error performing action with cursor', event);
throw Error('Error performing action with cursor')
};

updateRequest.onsuccess = () => {
cursorRequest.onsuccess = () => {
console.log('Record updated successfully');
};
}
Expand All @@ -251,8 +254,27 @@ export class Database {
}
};
}


private async updateElementIf<T>(
storeName: ObjectStoreName,
requirementCheck: (item: T) => boolean,
updateFunction: (item: T) => T
): Promise<void> {
this.performCursorActionIf(storeName, requirementCheck, (cursor) => {
const updatedValue = updateFunction(cursor.value);
return cursor.update(updatedValue);
})
}

private async deleteElementIf<T>(
storeName: ObjectStoreName,
requirementCheck: (item: T) => boolean
): Promise<void> {
this.performCursorActionIf(storeName, requirementCheck, (cursor) => {
return cursor.delete();
})
}

private performRequestForObjectStoreNamed<T>(
storeName: string,
transactionMode: IDBTransactionMode,
Expand Down

0 comments on commit a14369c

Please sign in to comment.