Skip to content

Commit

Permalink
fix: ensure the metadata.animationUrl is added to metadata.product.vi… (
Browse files Browse the repository at this point in the history
#647)

* fix: ensure the metadata.animationUrl is added to metadata.product.visuals_videos when not already there

* fix e2e test
  • Loading branch information
levalleux-ludo authored Jan 23, 2024
1 parent 9693947 commit 4739d77
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
29 changes: 26 additions & 3 deletions e2e/tests/productV1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ function mockProductV1Metadata(
productUuid: string = productV1.buildUuid(),
overrides: DeepPartial<productV1.ProductV1Metadata> = {} as DeepPartial<productV1.ProductV1Metadata>
): productV1.ProductV1Metadata {
const productV1ValidMinimalOfferClone = JSON.parse(
JSON.stringify(productV1ValidMinimalOffer)
);
return {
...productV1ValidMinimalOffer,
...productV1ValidMinimalOfferClone,
...overrides,
product: {
...productV1ValidMinimalOffer.product,
...productV1ValidMinimalOfferClone.product,
...overrides.product,
uuid: productUuid
},
uuid: productV1.buildUuid(),
type: MetadataType.PRODUCT_V1,
exchangePolicy: {
...productV1ValidMinimalOffer.exchangePolicy,
...productV1ValidMinimalOfferClone.exchangePolicy,
...overrides.exchangePolicy,
template
}
Expand Down Expand Up @@ -308,6 +311,7 @@ describe("ProductV1 e2e tests", () => {
);

metadata2 = mockProductV1Metadata("dummy", productUuid1, {
image: "ipfs://QmWJiZrvxs5Z8qXHZWqCvEbahQrfD7aca7x9iGPsa5iEBr",
product: {
visuals_images: [
{
Expand Down Expand Up @@ -1138,6 +1142,25 @@ describe("additional tests", () => {

// Do not create the offer with this invalid returnPeriod because the subgraph does not support it
});
test("check animationUrl is added to product.visuals_videos", async () => {
const { coreSDK, fundedWallet: sellerWallet } =
await initCoreSDKWithFundedWallet(seedWallet);

const template = "Hello World!!";
const metadata = mockProductV1Metadata(template);
metadata.animationUrl = "https://animation.url";
const { offerArgs } = await createOfferArgs(coreSDK, metadata);
resolveDateValidity(offerArgs);

const offer = await createOffer(coreSDK, sellerWallet, offerArgs);
expect(offer).toBeTruthy();

expect(offer.metadata?.type).toEqual(MetadataType.PRODUCT_V1);
expect(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
((offer?.metadata as any)?.product as any)?.visuals_videos?.length
).toEqual(1);
});
});

describe("core-sdk-check-exchange-policy", () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/subgraph/src/entities/metadata/product-v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function saveProductV1Metadata(
const variant = saveProductV1Variant(offerId, savedVariationIds);
const savedProductId = saveProductV1ProductOrOverrides(
productObj,
metadataObj,
savedProductV1SellerId,
false,
variant,
Expand All @@ -73,6 +74,7 @@ export function saveProductV1Metadata(
);
const savedProductOverridesId = saveProductV1ProductOrOverrides(
convertToObject(metadataObj.get("productOverrides")),
metadataObj,
savedProductV1SellerId,
true,
null,
Expand Down
25 changes: 24 additions & 1 deletion packages/subgraph/src/entities/metadata/product-v1/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ export function getMediaId(mediaUrl: string, mediaTag: string): string {

export function saveProductV1Medias(
medias: Array<TypedMap<string, JSONValue>>,
mediaType: string
mediaType: string,
additionalMediaUrl: string = ""
): string[] {
const savedMedias: string[] = [];
let addAdditionalMediaUrl = (additionalMediaUrl !== "")

for (let i = 0; i < medias.length; i++) {
const mediaObject = medias[i];
const mediaUrl = convertToString(mediaObject.get("url"));

if (mediaUrl.toLowerCase() == additionalMediaUrl.toLowerCase()) {
addAdditionalMediaUrl = false;
}
const mediaTag = convertToString(mediaObject.get("tag"));
const mediaHeight = convertToInt(mediaObject.get("height"));
const mediaWidth = convertToInt(mediaObject.get("width"));
Expand All @@ -39,5 +45,22 @@ export function saveProductV1Medias(
savedMedias.push(mediaId);
}

if (addAdditionalMediaUrl) {
const mediaTag = "";
const mediaId = getMediaId(additionalMediaUrl, mediaTag);

let media = ProductV1Media.load(mediaId);

if (!media) {
media = new ProductV1Media(mediaId);
media.url = additionalMediaUrl;
media.tag = mediaTag;
media.type = mediaType as string;
}
media.save();

savedMedias.push(mediaId);
}

return savedMedias;
}
8 changes: 6 additions & 2 deletions packages/subgraph/src/entities/metadata/product-v1/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function getSectionId(section: string): string {

export function saveProductV1ProductOrOverrides(
productOrOverrideObj: TypedMap<string, JSONValue> | null,
metadataObj: TypedMap<string, JSONValue>,
productV1SellerId: string | null,
isOverride: boolean,
variant: string | null,
Expand Down Expand Up @@ -130,11 +131,14 @@ export function saveProductV1ProductOrOverrides(
const visuals_images = convertToObjectArray(
productOrOverrideObj.get("visuals_images")
);
const savedImageIds = saveProductV1Medias(visuals_images, "IMAGE");
const metadataImage = convertToString(metadataObj.get("image"));
const savedImageIds = saveProductV1Medias(visuals_images, "IMAGE", metadataImage);
const visuals_videos = convertToObjectArray(
productOrOverrideObj.get("visuals_videos")
);
const savedVideoIds = saveProductV1Medias(visuals_videos, "VIDEO");
const animationUrl = convertToString(metadataObj.get("animationUrl"));
const savedVideoIds = saveProductV1Medias(visuals_videos, "VIDEO", animationUrl);


const packaging_packageQuantity = convertToString(
productOrOverrideObj.get("packaging_packageQuantity")
Expand Down

0 comments on commit 4739d77

Please sign in to comment.