refactor(claims): extract offer fulfillment logic to utility function

Created extractOfferFulfillment utility in libs/util.ts to handle both
array and single object cases for fulfills field. Updated ClaimView and
ConfirmGiftView to use the shared utility, eliminating code duplication
and improving maintainability.
This commit is contained in:
Jose Olarte III
2025-09-01 21:24:46 +08:00
parent 3e5e2cd0bb
commit 1eeb013638
3 changed files with 41 additions and 52 deletions

View File

@@ -159,6 +159,41 @@ export const isGiveAction = (
return isGiveClaimType(veriClaim.claimType); return isGiveClaimType(veriClaim.claimType);
}; };
export interface OfferFulfillment {
offerHandleId: string;
offerType: string;
}
/**
* Extract offer fulfillment information from the fulfills field
* Handles both array and single object cases
*/
export const extractOfferFulfillment = (fulfills: any): OfferFulfillment | null => {
if (!fulfills) {
return null;
}
// Handle both array and single object cases
let offerFulfill = null;
if (Array.isArray(fulfills)) {
// Find the Offer in the fulfills array
offerFulfill = fulfills.find((item) => item["@type"] === "Offer");
} else if (fulfills["@type"] === "Offer") {
// fulfills is a single Offer object
offerFulfill = fulfills;
}
if (offerFulfill) {
return {
offerHandleId: offerFulfill.identifier,
offerType: offerFulfill["@type"],
};
}
return null;
};
export const shortDid = (did: string) => { export const shortDid = (did: string) => {
if (did.startsWith("did:peer:")) { if (did.startsWith("did:peer:")) {
return ( return (

View File

@@ -733,32 +733,9 @@ export default class ClaimView extends Vue {
* Extract offer fulfillment information from the fulfills array * Extract offer fulfillment information from the fulfills array
*/ */
extractOfferFulfillment() { extractOfferFulfillment() {
if (!this.detailsForGive?.fullClaim?.fulfills) { this.detailsForGiveOfferFulfillment = libsUtil.extractOfferFulfillment(
this.detailsForGiveOfferFulfillment = null; this.detailsForGive?.fullClaim?.fulfills
return; );
}
const fulfills = this.detailsForGive.fullClaim.fulfills;
// Handle both array and single object cases
let offerFulfill = null;
if (Array.isArray(fulfills)) {
// Find the Offer in the fulfills array
offerFulfill = fulfills.find((item) => item["@type"] === "Offer");
} else if (fulfills["@type"] === "Offer") {
// fulfills is a single Offer object
offerFulfill = fulfills;
}
if (offerFulfill) {
this.detailsForGiveOfferFulfillment = {
offerHandleId: offerFulfill.identifier,
offerType: offerFulfill["@type"],
};
} else {
this.detailsForGiveOfferFulfillment = null;
}
} }
// ================================================= // =================================================

View File

@@ -718,32 +718,9 @@ export default class ConfirmGiftView extends Vue {
* Extract offer fulfillment information from the fulfills array * Extract offer fulfillment information from the fulfills array
*/ */
private extractOfferFulfillment() { private extractOfferFulfillment() {
if (!this.giveDetails?.fullClaim?.fulfills) { this.giveDetailsOfferFulfillment = libsUtil.extractOfferFulfillment(
this.giveDetailsOfferFulfillment = null; this.giveDetails?.fullClaim?.fulfills
return; );
}
const fulfills = this.giveDetails.fullClaim.fulfills;
// Handle both array and single object cases
let offerFulfill = null;
if (Array.isArray(fulfills)) {
// Find the Offer in the fulfills array
offerFulfill = fulfills.find((item) => item["@type"] === "Offer");
} else if (fulfills["@type"] === "Offer") {
// fulfills is a single Offer object
offerFulfill = fulfills;
}
if (offerFulfill) {
this.giveDetailsOfferFulfillment = {
offerHandleId: offerFulfill.identifier,
offerType: offerFulfill["@type"],
};
} else {
this.giveDetailsOfferFulfillment = null;
}
} }
/** /**