Browse Source

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.
pull/167/head
Jose Olarte III 2 weeks ago
parent
commit
1eeb013638
  1. 35
      src/libs/util.ts
  2. 29
      src/views/ClaimView.vue
  3. 29
      src/views/ConfirmGiftView.vue

35
src/libs/util.ts

@ -159,6 +159,41 @@ export const isGiveAction = (
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) => {
if (did.startsWith("did:peer:")) {
return (

29
src/views/ClaimView.vue

@ -733,32 +733,9 @@ export default class ClaimView extends Vue {
* Extract offer fulfillment information from the fulfills array
*/
extractOfferFulfillment() {
if (!this.detailsForGive?.fullClaim?.fulfills) {
this.detailsForGiveOfferFulfillment = null;
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;
}
this.detailsForGiveOfferFulfillment = libsUtil.extractOfferFulfillment(
this.detailsForGive?.fullClaim?.fulfills
);
}
// =================================================

29
src/views/ConfirmGiftView.vue

@ -718,32 +718,9 @@ export default class ConfirmGiftView extends Vue {
* Extract offer fulfillment information from the fulfills array
*/
private extractOfferFulfillment() {
if (!this.giveDetails?.fullClaim?.fulfills) {
this.giveDetailsOfferFulfillment = null;
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;
}
this.giveDetailsOfferFulfillment = libsUtil.extractOfferFulfillment(
this.giveDetails?.fullClaim?.fulfills
);
}
/**

Loading…
Cancel
Save