( ...args: Parameters<CreateEntityFunction<Properties>> )
| 102 | * Create an entity. |
| 103 | */ |
| 104 | export const createEntity = async < |
| 105 | Properties extends TypeIdsAndPropertiesForEntity, |
| 106 | >( |
| 107 | ...args: Parameters<CreateEntityFunction<Properties>> |
| 108 | ): ReturnType<CreateEntityFunction<Properties>> => { |
| 109 | const [context, authentication, params] = args; |
| 110 | const { outgoingLinks, ...createParams } = params; |
| 111 | |
| 112 | const { graphApi, provenance } = context; |
| 113 | const { actorId } = authentication; |
| 114 | |
| 115 | let properties = params.properties; |
| 116 | |
| 117 | for (const beforeCreateHook of beforeCreateEntityHooks) { |
| 118 | if (createParams.entityTypeIds.includes(beforeCreateHook.entityTypeId)) { |
| 119 | const { properties: hookReturnedProperties } = |
| 120 | await beforeCreateHook.callback({ |
| 121 | context, |
| 122 | properties, |
| 123 | authentication, |
| 124 | }); |
| 125 | |
| 126 | properties = hookReturnedProperties; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | const entity = await HashEntity.create<Properties>( |
| 131 | graphApi, |
| 132 | { actorId }, |
| 133 | { |
| 134 | ...createParams, |
| 135 | properties, |
| 136 | provenance, |
| 137 | }, |
| 138 | ); |
| 139 | |
| 140 | for (const createOutgoingLinkParams of outgoingLinks ?? []) { |
| 141 | await createLinkEntity(context, authentication, { |
| 142 | ...createOutgoingLinkParams, |
| 143 | linkData: { |
| 144 | ...createOutgoingLinkParams.linkData, |
| 145 | leftEntityId: entity.metadata.recordId.entityId, |
| 146 | }, |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | for (const afterCreateHook of afterCreateEntityHooks) { |
| 151 | if (entity.metadata.entityTypeIds.includes(afterCreateHook.entityTypeId)) { |
| 152 | void afterCreateHook.callback({ |
| 153 | context, |
| 154 | entity, |
| 155 | authentication, |
| 156 | }); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return entity; |
| 161 | }; |
no test coverage detected