{"version":3,"file":"content-type-builder.mjs","sources":["../../../../server/src/services/schema-builder/content-type-builder.ts"],"sourcesContent":["import path from 'path';\nimport _ from 'lodash';\n\nimport { strings, errors } from '@strapi/utils';\nimport type { Schema, Internal } from '@strapi/types';\nimport { isRelation, isConfigurable } from '../../utils/attributes';\nimport { typeKinds } from '../constants';\nimport createSchemaHandler from './schema-handler';\nimport { CreateContentTypeInput } from '../../controllers/validation/content-type';\nimport type { InternalRelationAttribute, InternalAttribute } from './types';\n\nconst { ApplicationError } = errors;\n\nconst reuseUnsetPreviousProperties = (\n  newAttribute: Schema.Attribute.AnyAttribute,\n  oldAttribute: Schema.Attribute.AnyAttribute\n) => {\n  _.defaults(\n    newAttribute,\n    _.omit(oldAttribute, [\n      'configurable',\n      'required',\n      'private',\n      'unique',\n      'pluginOptions',\n      'inversedBy',\n      'mappedBy',\n      'conditions', // Don't automatically preserve conditions\n    ])\n  );\n};\n\n/**\n * Maps each attribute name produced by this save's rename hops to the name it\n * had before the save, by folding the hops in order (`a -> tmp, tmp -> b`\n * gives `b -> a`).\n */\nconst getRenameOrigins = (renames: unknown): Map<string, string> => {\n  const originOf = new Map<string, string>();\n\n  if (!Array.isArray(renames)) {\n    return originOf;\n  }\n\n  for (const { oldName, newName } of renames as { oldName?: string; newName?: string }[]) {\n    if (!oldName || !newName || oldName === newName) {\n      continue;\n    }\n\n    const origin = originOf.get(oldName) ?? oldName;\n    originOf.delete(oldName);\n    originOf.set(newName, origin);\n  }\n\n  return originOf;\n};\n\nexport default function createComponentBuilder() {\n  return {\n    setRelation(\n      this: any,\n      { key, uid, attribute }: { key: string; uid: string; attribute: InternalRelationAttribute }\n    ) {\n      if (!_.has(attribute, 'target')) {\n        return;\n      }\n\n      const targetCT = this.contentTypes.get(attribute.target);\n\n      if (!targetCT) {\n        throw new ApplicationError(`Content type ${attribute.target} not found`);\n      }\n\n      const targetAttribute = targetCT.getAttribute(attribute.targetAttribute);\n\n      if (!attribute.targetAttribute) {\n        return;\n      }\n\n      // When generating the inverse relation, preserve existing conditions if they exist\n      // If the target attribute already exists and has conditions, preserve them\n      const targetAttributeData = targetAttribute || {};\n\n      // If the source doesn't have conditions but the target does, preserve target's conditions\n\n      targetCT.setAttribute(\n        attribute.targetAttribute,\n        generateRelation({ key, attribute, uid, targetAttribute: targetAttributeData })\n      );\n    },\n\n    unsetRelation(\n      this: any,\n      attribute: Schema.Attribute.Relation<Schema.Attribute.RelationKind.Any>\n    ) {\n      if (!('target' in attribute) || !attribute.target) {\n        return;\n      }\n\n      const targetCT = this.contentTypes.get(attribute.target);\n\n      const relationAttribute = attribute as InternalRelationAttribute;\n      const targetAttributeName = relationAttribute.inversedBy || relationAttribute.mappedBy;\n      const targetAttribute = targetCT.getAttribute(targetAttributeName);\n\n      if (!targetAttribute) return;\n\n      return targetCT.deleteAttribute(targetAttributeName);\n    },\n\n    createContentTypeAttributes(\n      this: any,\n      uid: string,\n      attributes: CreateContentTypeInput['attributes']\n    ) {\n      if (!this.contentTypes.has(uid)) {\n        throw new ApplicationError('contentType.notFound');\n      }\n\n      const contentType = this.contentTypes.get(uid);\n\n      // support self referencing content type relation\n      Object.keys(attributes).forEach((key) => {\n        const { target } = attributes[key];\n        if (target === '__self__') {\n          attributes[key].target = uid;\n        }\n      });\n\n      contentType.setAttributes(this.convertAttributes(attributes));\n\n      Object.keys(attributes).forEach((key) => {\n        const attribute = attributes[key] as InternalAttribute;\n\n        if (isRelation(attribute)) {\n          const relationAttribute = attribute as InternalRelationAttribute;\n          if (['manyToMany', 'oneToOne'].includes(relationAttribute.relation)) {\n            if (\n              relationAttribute.target === uid &&\n              relationAttribute.targetAttribute !== undefined\n            ) {\n              // self referencing relation\n              const targetAttribute = attributes[\n                relationAttribute.targetAttribute\n              ] as InternalRelationAttribute;\n\n              if (targetAttribute.dominant === undefined) {\n                relationAttribute.dominant = true;\n              } else {\n                relationAttribute.dominant = false;\n              }\n            } else {\n              relationAttribute.dominant = true;\n            }\n          }\n\n          this.setRelation({\n            key,\n            uid,\n            attribute: relationAttribute,\n          });\n        }\n      });\n\n      return contentType;\n    },\n\n    /**\n     * Creates a content type in memory to be written to files later on\n     */\n    createContentType(this: any, infos: CreateContentTypeInput) {\n      // TODO:: check for unique uid / singularName & pluralName & collectionName\n\n      if (infos.uid && infos.uid !== createContentTypeUID(infos)) {\n        throw new ApplicationError('contentType.invalidUID');\n      }\n\n      const uid = infos.uid ?? createContentTypeUID(infos);\n\n      if (this.contentTypes.has(uid)) {\n        throw new ApplicationError('contentType.alreadyExists');\n      }\n\n      const dir = infos.plugin\n        ? path.join(strapi.dirs.app.extensions, infos.plugin, 'content-types', infos.singularName)\n        : path.join(strapi.dirs.app.api, infos.singularName, 'content-types', infos.singularName);\n\n      const contentType = createSchemaHandler({\n        uid,\n        isNew: true,\n        modelName: infos.singularName,\n        dir,\n        filename: `schema.json`,\n      });\n\n      this.contentTypes.set(uid, contentType);\n\n      contentType\n        .set('kind', infos.kind || typeKinds.COLLECTION_TYPE)\n        .set(\n          'collectionName',\n          infos.collectionName || strings.nameToCollectionName(infos.pluralName)\n        )\n        .set('info', {\n          singularName: infos.singularName,\n          pluralName: infos.pluralName,\n          displayName: infos.displayName,\n          description: infos.description,\n        })\n        .set('options', {\n          ...infos.options,\n          draftAndPublish: infos.draftAndPublish ?? infos.options?.draftAndPublish,\n        })\n        .set('pluginOptions', infos.pluginOptions)\n        .set('config', infos.config);\n\n      this.createContentTypeAttributes(uid, infos.attributes);\n\n      return contentType;\n    },\n\n    editContentType(this: any, infos: any) {\n      const { uid } = infos;\n\n      if (!this.contentTypes.has(uid)) {\n        throw new ApplicationError('contentType.notFound');\n      }\n\n      const contentType = this.contentTypes.get(uid);\n\n      const oldAttributes = contentType.schema.attributes;\n\n      const newAttributes = _.omitBy(infos.attributes, (attr, key) => {\n        return _.has(oldAttributes, key) && !isConfigurable(oldAttributes[key]);\n      });\n\n      const newKeys = _.difference(Object.keys(newAttributes), Object.keys(oldAttributes));\n      const deletedKeys = _.difference(Object.keys(oldAttributes), Object.keys(newAttributes));\n      const remainingKeys = _.intersection(Object.keys(oldAttributes), Object.keys(newAttributes));\n      const renameOrigins = getRenameOrigins(infos.renames);\n\n      // remove old relations\n      deletedKeys.forEach((key) => {\n        const attribute = oldAttributes[key];\n\n        // if the old relation has a target attribute. we need to remove it in the target type\n        if (isConfigurable(attribute) && isRelation(attribute)) {\n          const relationAttribute = attribute as InternalRelationAttribute;\n          const targetAttributeName = relationAttribute.inversedBy || relationAttribute.mappedBy;\n\n          if (targetAttributeName !== null && targetAttributeName !== undefined) {\n            this.unsetRelation(attribute);\n          }\n        }\n      });\n\n      remainingKeys.forEach((key) => {\n        const oldAttribute = oldAttributes[key];\n        const newAttribute = newAttributes[key] as InternalAttribute;\n\n        if (!isRelation(oldAttribute) && isRelation(newAttribute)) {\n          return this.setRelation({\n            key,\n            uid,\n            attribute: newAttribute as InternalRelationAttribute,\n          });\n        }\n\n        if (isRelation(oldAttribute) && !isRelation(newAttribute)) {\n          return this.unsetRelation(oldAttribute);\n        }\n\n        if (isRelation(oldAttribute) && isRelation(newAttribute)) {\n          const relationAttribute = newAttribute as InternalRelationAttribute;\n          const oldRelationAttribute = oldAttribute as InternalRelationAttribute;\n          const oldTargetAttributeName =\n            oldRelationAttribute.inversedBy || oldRelationAttribute.mappedBy;\n\n          const sameRelation = oldAttribute.relation === relationAttribute.relation;\n          const targetAttributeHasChanged =\n            oldTargetAttributeName !== relationAttribute.targetAttribute;\n\n          if (!sameRelation || targetAttributeHasChanged) {\n            this.unsetRelation(oldAttribute);\n          }\n\n          // keep extra options that were set manually on oldAttribute\n          reuseUnsetPreviousProperties(relationAttribute, oldAttribute);\n\n          // Handle conditions explicitly - only preserve if present and not undefined in new attribute\n          const newAttributeFromInfos = newAttributes[key];\n          const hasNewConditions =\n            newAttributeFromInfos.conditions !== undefined &&\n            newAttributeFromInfos.conditions !== null;\n\n          if (oldAttribute.conditions) {\n            if (hasNewConditions) {\n              // Conditions are still present, keep them\n              relationAttribute.conditions = newAttributeFromInfos.conditions;\n            } else {\n              // Conditions were removed (undefined or null), ensure they're not preserved\n              delete relationAttribute.conditions;\n            }\n          } else if (hasNewConditions) {\n            // New conditions added\n            relationAttribute.conditions = newAttributeFromInfos.conditions;\n          }\n\n          if (oldRelationAttribute.inversedBy) {\n            relationAttribute.dominant = true;\n          } else if (oldRelationAttribute.mappedBy) {\n            relationAttribute.dominant = false;\n          }\n\n          return this.setRelation({\n            key,\n            uid,\n            attribute: relationAttribute,\n          });\n        }\n      });\n\n      // add new relations\n      newKeys.forEach((key) => {\n        const attribute = newAttributes[key] as InternalAttribute;\n\n        if (isRelation(attribute)) {\n          const relationAttribute = attribute as InternalRelationAttribute;\n          if (['manyToMany', 'oneToOne'].includes(relationAttribute.relation)) {\n            // A renamed relation keeps the ownership of the attribute it was\n            // renamed from (same rule as a remaining key), so the join table\n            // stays with its owner and the rename migration can carry it.\n            const origin = renameOrigins.get(key);\n            const originAttribute =\n              origin !== undefined && _.has(oldAttributes, origin)\n                ? (oldAttributes[origin] as InternalRelationAttribute)\n                : undefined;\n            const originOwnership =\n              originAttribute !== undefined &&\n              isRelation(originAttribute) &&\n              originAttribute.relation === relationAttribute.relation &&\n              originAttribute.target === relationAttribute.target\n                ? originAttribute\n                : undefined;\n\n            if (originOwnership?.inversedBy) {\n              relationAttribute.dominant = true;\n            } else if (originOwnership?.mappedBy) {\n              relationAttribute.dominant = false;\n            } else if (\n              relationAttribute.target === uid &&\n              relationAttribute.targetAttribute !== undefined\n            ) {\n              // self referencing relation\n              const targetAttribute = newAttributes[\n                relationAttribute.targetAttribute\n              ] as InternalRelationAttribute;\n\n              if (targetAttribute.dominant === undefined) {\n                relationAttribute.dominant = true;\n              } else {\n                relationAttribute.dominant = false;\n              }\n            } else {\n              relationAttribute.dominant = true;\n            }\n          }\n\n          this.setRelation({\n            key,\n            uid,\n            attribute: relationAttribute,\n          });\n        }\n      });\n\n      contentType\n        .set('kind', infos.kind || contentType.schema.kind)\n        .set(['info', 'displayName'], infos.displayName)\n        .set(['info', 'description'], infos.description)\n        .set('options', {\n          // Payloads may omit options keys the CTB does not edit (e.g. populateCreatorFields)\n          ...contentType.schema.options,\n          ...infos.options,\n          // A payload that does not set draftAndPublish keeps the stored value\n          draftAndPublish:\n            infos.draftAndPublish ??\n            infos.options?.draftAndPublish ??\n            contentType.schema.options?.draftAndPublish,\n        })\n        .set('pluginOptions', infos.pluginOptions)\n        .setAttributes(this.convertAttributes(newAttributes));\n\n      return contentType;\n    },\n\n    deleteContentType(this: any, uid: string) {\n      if (!this.contentTypes.has(uid)) {\n        throw new ApplicationError('contentType.notFound');\n      }\n\n      this.components.forEach((compo: any) => {\n        compo.removeContentType(uid);\n      });\n\n      this.contentTypes.forEach((ct: any) => {\n        ct.removeContentType(uid);\n      });\n\n      return this.contentTypes.get(uid).delete();\n    },\n  };\n}\n\n/**\n * Returns a uid from a content type infos\n *\n * @param {object} options options\n * @param {string} options.singularName content-type singularName\n * @returns {string} uid\n */\nconst createContentTypeUID = ({\n  plugin,\n  singularName,\n}: {\n  plugin?: string;\n  singularName: string;\n}): Internal.UID.ContentType =>\n  `${plugin ? `plugin` : `api`}::${plugin || singularName}.${singularName}`;\n\nconst generateRelation = ({\n  key,\n  attribute,\n  uid,\n  targetAttribute = {},\n}: {\n  key: string;\n  attribute: InternalRelationAttribute;\n  uid: string;\n  targetAttribute?: Partial<InternalRelationAttribute>;\n}) => {\n  const opts: any = {\n    type: 'relation',\n    target: uid,\n    required: targetAttribute.required || undefined,\n    private: targetAttribute.private || undefined,\n    pluginOptions: targetAttribute.pluginOptions || undefined,\n    // Preserve conditions from targetAttribute if they exist\n    // This allows each side of the relation to maintain its own conditions\n    ...(targetAttribute.conditions && { conditions: targetAttribute.conditions }),\n  };\n\n  switch (attribute.relation) {\n    case 'oneToOne': {\n      opts.relation = 'oneToOne';\n\n      if (attribute.dominant) {\n        opts.mappedBy = key;\n      } else {\n        opts.inversedBy = key;\n      }\n      break;\n    }\n    case 'oneToMany': {\n      opts.relation = 'manyToOne';\n      opts.inversedBy = key;\n      break;\n    }\n    case 'manyToOne': {\n      opts.relation = 'oneToMany';\n      opts.mappedBy = key;\n      break;\n    }\n    case 'manyToMany': {\n      opts.relation = 'manyToMany';\n\n      if (attribute.dominant) {\n        opts.mappedBy = key;\n      } else {\n        opts.inversedBy = key;\n      }\n\n      break;\n    }\n    default:\n  }\n\n  // we do this just to make sure we have the same key order when writing to files\n  const { type, relation, target, ...restOptions } = opts;\n\n  const result = {\n    type,\n    relation,\n    target,\n    ...restOptions,\n  };\n\n  return result;\n};\n"],"names":["ApplicationError","errors","reuseUnsetPreviousProperties","newAttribute","oldAttribute","_","defaults","omit","getRenameOrigins","renames","originOf","Map","Array","isArray","oldName","newName","origin","get","delete","set","createComponentBuilder","setRelation","key","uid","attribute","has","targetCT","contentTypes","target","targetAttribute","getAttribute","targetAttributeData","setAttribute","generateRelation","unsetRelation","relationAttribute","targetAttributeName","inversedBy","mappedBy","deleteAttribute","createContentTypeAttributes","attributes","contentType","Object","keys","forEach","setAttributes","convertAttributes","isRelation","includes","relation","undefined","dominant","createContentType","infos","createContentTypeUID","dir","plugin","path","join","strapi","dirs","app","extensions","singularName","api","createSchemaHandler","isNew","modelName","filename","kind","typeKinds","COLLECTION_TYPE","collectionName","strings","nameToCollectionName","pluralName","displayName","description","options","draftAndPublish","pluginOptions","config","editContentType","oldAttributes","schema","newAttributes","omitBy","attr","isConfigurable","newKeys","difference","deletedKeys","remainingKeys","intersection","renameOrigins","oldRelationAttribute","oldTargetAttributeName","sameRelation","targetAttributeHasChanged","newAttributeFromInfos","hasNewConditions","conditions","originAttribute","originOwnership","deleteContentType","components","compo","removeContentType","ct","opts","type","required","private","restOptions","result"],"mappings":";;;;;;;AAWA,MAAM,EAAEA,gBAAgB,EAAE,GAAGC,MAAAA;AAE7B,MAAMC,4BAAAA,GAA+B,CACnCC,YAAAA,EACAC,YAAAA,GAAAA;AAEAC,IAAAA,CAAAA,CAAEC,QAAQ,CACR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