fix: adds transform medusa ids (#385)

This commit is contained in:
Sebastian Rindom
2021-09-12 11:29:57 +02:00
committed by GitHub
parent 27150959ff
commit 0812acaf0d

View File

@@ -174,7 +174,7 @@ class ContentfulService extends BaseService {
"en-US": variantLinks,
},
[this.getCustomField("options", "product")]: {
"en-US": p.options,
"en-US": this.transformMedusaIds(p.options),
},
[this.getCustomField("medusaId", "product")]: {
"en-US": p.id,
@@ -285,10 +285,10 @@ class ContentfulService extends BaseService {
"en-US": v.sku,
},
[this.getCustomField("prices", "variant")]: {
"en-US": v.prices,
"en-US": this.transformMedusaIds(v.prices),
},
[this.getCustomField("options", "variant")]: {
"en-US": v.options,
"en-US": this.transformMedusaIds(v.options),
},
[this.getCustomField("medusaId", "variant")]: {
"en-US": v.id,
@@ -476,7 +476,7 @@ class ContentfulService extends BaseService {
"en-US": p.description,
},
[this.getCustomField("options", "product")]: {
"en-US": p.options,
"en-US": this.transformMedusaIds(p.options),
},
[this.getCustomField("variants", "product")]: {
"en-US": variantLinks,
@@ -538,9 +538,8 @@ class ContentfulService extends BaseService {
"en-US": p.collection.title,
}
productEntryFields[
this.getCustomField("collection", "product")
] = collection
productEntryFields[this.getCustomField("collection", "product")] =
collection
}
if (p.tags) {
@@ -624,10 +623,10 @@ class ContentfulService extends BaseService {
"en-US": v.sku,
},
[this.getCustomField("options", "variant")]: {
"en-US": v.options,
"en-US": this.transformMedusaIds(v.options),
},
[this.getCustomField("prices", "variant")]: {
"en-US": v.prices,
"en-US": this.transformMedusaIds(v.prices),
},
[this.getCustomField("medusaId", "variant")]: {
"en-US": v.id,
@@ -734,6 +733,28 @@ class ContentfulService extends BaseService {
}
}
transformMedusaIds(objOrArray) {
let input = objOrArray
let isArray = true
if (!Array.isArray(objOrArray)) {
input = [objOrArray]
isArray = false
}
let output = []
for (const obj of input) {
let transformed = Object.assign({}, objOrArray)
transformed.medusaId = output.id
output.push(transformed)
}
if (!isArray) {
return output[0]
}
return output
}
async getType(type) {
const environment = await this.getContentfulEnvironment_()
return environment.getContentType(type)