fix: make contentful data sync

This commit is contained in:
Sebastian Rindom committed 2021-10-25 18:48:56 +02:00
1 parent 81847456f1
commit 548f6c7138
2 files changed
+29 -8

No files matched your search

@@ -742,21 +742,35 @@ class ContentfulService extends BaseService {
const environment = await this.getContentfulEnvironment_() const environment = await this.getContentfulEnvironment_()
const productEntry = await environment.getEntry(productId) const productEntry = await environment.getEntry(productId)
const product = await this.productService_.retrieve(productId) const product = await this.productService_.retrieve(productId, {
select: [
"id",
"handle",
"title",
"subtitle",
"description",
"thumbnail",
],
})
const update = {} const update = {}
const title = const title =
productEntry.fields[this.getCustomField("title", "product")]["en-US"] productEntry.fields[this.getCustomField("title", "product")]?.["en-US"]
const subtitle = const subtitle =
productEntry.fields[this.getCustomField("subtitle", "product")]["en-US"] productEntry.fields[this.getCustomField("subtitle", "product")]?.[
const description =
productEntry.fields[this.getCustomField("description", "product")][
"en-US" "en-US"
] ]
const description =
productEntry.fields[this.getCustomField("description", "product")]?.[
"en-US"
]
const handle =
productEntry.fields[this.getCustomField("handle", "product")]?.["en-US"]
if (product.title !== title) { if (product.title !== title) {
update.title = title update.title = title
} }
@@ -769,6 +783,10 @@ class ContentfulService extends BaseService {
update.description = description update.description = description
} }
if (product.handle !== handle) {
update.handle = handle
}
// Get the thumbnail, if present // Get the thumbnail, if present
if (productEntry.fields.thumbnail) { if (productEntry.fields.thumbnail) {
const thumb = await environment.getAsset( const thumb = await environment.getAsset(
@@ -776,7 +794,7 @@ class ContentfulService extends BaseService {
) )
if (thumb.fields.file["en-US"].url) { if (thumb.fields.file["en-US"].url) {
if (!product.thumbnail.includes(thumb.fields.file["en-US"].url)) { if (!product.thumbnail?.includes(thumb.fields.file["en-US"].url)) {
update.thumbnail = thumb.fields.file["en-US"].url update.thumbnail = thumb.fields.file["en-US"].url
} }
} }
@@ -834,7 +852,6 @@ class ContentfulService extends BaseService {
output.push(transformed) output.push(transformed)
} }
console.log(output)
if (!isArray) { if (!isArray) {
return output[0] return output[0]
} }
@@ -55,6 +55,10 @@ export class ProductRepository extends Repository<Product> {
return [] return []
} }
if (relations.length === 0) {
return this.findByIds(entitiesIds, idsOrOptionsWithoutRelations)
}
const groupedRelations: { [toplevel: string]: string[] } = {} const groupedRelations: { [toplevel: string]: string[] } = {}
for (const rel of relations) { for (const rel of relations) {
const [topLevel] = rel.split(".") const [topLevel] = rel.split(".")