Skip to content

Commit 00da022

Browse files
authored
Merge pull request #312 from GTBitsOfGood/ezra/309-unapprove-distributions
Ezra/309 unapprove distributions
2 parents de70c31 + 587b6bd commit 00da022

3 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎src/components/DistributionTable.tsx‎

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useCallback, useRef, useState } from "react";
77
import { useApiClient } from "@/hooks/useApiClient";
88
import Portal from "./baseTable/Portal";
99
import toast from "react-hot-toast";
10-
import { CheckCircle, DotsThree } from "@phosphor-icons/react";
10+
import { CheckCircle, DotsThree, XCircle } from "@phosphor-icons/react";
1111
import DistributionsGeneralItemChipGroup from "./chips/DistributionsGeneralItemChipGroup";
1212
import { TableDistribution } from "@/types/api/distribution.types";
1313
import { useUser } from "@/components/context/UserContext";
@@ -168,10 +168,35 @@ function OptionsButton({
168168
error: "Failed to approve distribution.",
169169
});
170170

171-
await promise;
171+
try {
172+
await promise;
173+
fetchTableData();
174+
} finally {
175+
setIsDropdownOpen(false);
176+
}
172177

173-
setIsDropdownOpen(false);
174-
fetchTableData();
178+
}
179+
180+
async function unapproveDistribution() {
181+
const formData = new FormData();
182+
formData.append("pending", "true");
183+
184+
const promise = apiClient.patch(`/api/distributions/${distribution.id}`, {
185+
body: formData,
186+
});
187+
188+
toast.promise(promise, {
189+
loading: "Unapproving distribution...",
190+
success: "Distribution unapproved!",
191+
error: "Failed to unapprove distribution.",
192+
});
193+
194+
try {
195+
await promise;
196+
fetchTableData();
197+
} finally {
198+
setIsDropdownOpen(false);
199+
}
175200
}
176201

177202
return (
@@ -190,13 +215,24 @@ function OptionsButton({
190215
position="bottom-right"
191216
className="w-48 rounded-md bg-white shadow-lg ring-1 ring-black/5 py-1"
192217
>
193-
<button
194-
onClick={approveDistribution}
195-
className="flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
196-
>
197-
<CheckCircle size={16} className="mr-3 flex-shrink-0" />
198-
<p>Approve</p>
199-
</button>
218+
{distribution.pending ? (
219+
<button
220+
onClick={approveDistribution}
221+
className="flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
222+
>
223+
<CheckCircle size={16} className="mr-3 flex-shrink-0" />
224+
<p>Approve</p>
225+
</button>
226+
) : (
227+
<button
228+
onClick={unapproveDistribution}
229+
className="flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
230+
>
231+
<XCircle size={16} className="mr-3 flex-shrink-0" />
232+
<p>Unapprove</p>
233+
</button>
234+
235+
)}
200236
</Portal>
201237
</div>
202238
);

‎src/services/allocationService.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ export default class AllocationService {
317317
"Cannot transfer items from an approved distribution. Approved distributions are locked."
318318
);
319319
}
320+
if (allocation.signOffId !== null) {
321+
throw new ArgumentError(
322+
"Cannot transfer items that have been signed off."
323+
);
324+
}
320325
}
321326

322327
if (update.distributionId) {

‎src/services/distributionService.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,18 @@ export default class DistributionService {
664664
throw new NotFoundError("Distribution not found");
665665
}
666666

667+
if (data.pending === true && currentDistribution.pending === false) {
668+
const signedOffCount = await db.allocation.count({
669+
where: {distributionId, signOffId: { not: null} },
670+
});
671+
672+
if (signedOffCount > 0) {
673+
throw new ArgumentError(
674+
"Cannot unapprove a distribution that has signed-off allocations."
675+
);
676+
}
677+
}
678+
667679
const updatedDistribution = await db.distribution.update({
668680
where: { id: distributionId },
669681
data,

0 commit comments

Comments
 (0)