Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
69 lines
2.5 KiB
Plaintext
69 lines
2.5 KiB
Plaintext
export const transformRelationship = ({ field, locale, ref, relations, withinArrayOrBlockLocale })=>{
|
|
let result;
|
|
if (!('hasMany' in field) || field.hasMany === false) {
|
|
let relation = relations[0];
|
|
if (withinArrayOrBlockLocale) {
|
|
relation = relations.find((rel)=>rel.locale === withinArrayOrBlockLocale);
|
|
}
|
|
if (relation) {
|
|
// Handle hasOne Poly
|
|
if (Array.isArray(field.relationTo)) {
|
|
const matchedRelation = Object.entries(relation).find(([key, val])=>{
|
|
return val !== null && ![
|
|
'id',
|
|
'locale',
|
|
'order',
|
|
'parent',
|
|
'path'
|
|
].includes(key);
|
|
});
|
|
if (matchedRelation) {
|
|
const relationTo = matchedRelation[0].replace('ID', '');
|
|
result = {
|
|
relationTo,
|
|
value: matchedRelation[1]
|
|
};
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
const transformedRelations = [];
|
|
relations.forEach((relation)=>{
|
|
let matchedLocale = true;
|
|
if (withinArrayOrBlockLocale) {
|
|
matchedLocale = relation.locale === withinArrayOrBlockLocale;
|
|
}
|
|
// Handle hasMany
|
|
if (!Array.isArray(field.relationTo)) {
|
|
const relatedData = relation[`${field.relationTo}ID`];
|
|
if (relatedData && matchedLocale) {
|
|
transformedRelations.push(relatedData);
|
|
}
|
|
} else {
|
|
// Handle hasMany Poly
|
|
const matchedRelation = Object.entries(relation).find(([key, val])=>val !== null && ![
|
|
'id',
|
|
'locale',
|
|
'order',
|
|
'parent',
|
|
'path'
|
|
].includes(key) && matchedLocale);
|
|
if (matchedRelation) {
|
|
const relationTo = matchedRelation[0].replace('ID', '');
|
|
transformedRelations.push({
|
|
relationTo,
|
|
value: matchedRelation[1]
|
|
});
|
|
}
|
|
}
|
|
});
|
|
result = transformedRelations;
|
|
}
|
|
if (locale) {
|
|
ref[field.name][locale] = result;
|
|
} else {
|
|
ref[field.name] = result;
|
|
}
|
|
};
|
|
|
|
//# sourceMappingURL=relationship.js.map |