( ...args: Parameters<GetLatestEntityByIdFunction<Properties>> )
| 190 | * fault |
| 191 | */ |
| 192 | export const getLatestEntityById = async < |
| 193 | Properties extends TypeIdsAndPropertiesForEntity = |
| 194 | TypeIdsAndPropertiesForEntity, |
| 195 | >( |
| 196 | ...args: Parameters<GetLatestEntityByIdFunction<Properties>> |
| 197 | ): ReturnType<GetLatestEntityByIdFunction<Properties>> => { |
| 198 | const [context, authentication, { entityId }] = args; |
| 199 | |
| 200 | const [webId, entityUuid, draftId] = splitEntityId(entityId); |
| 201 | |
| 202 | const allFilter: AllFilter["all"] = [ |
| 203 | { |
| 204 | equal: [{ path: ["uuid"] }, { parameter: entityUuid }], |
| 205 | }, |
| 206 | { |
| 207 | equal: [{ path: ["webId"] }, { parameter: webId }], |
| 208 | }, |
| 209 | { equal: [{ path: ["archived"] }, { parameter: false }] }, |
| 210 | ]; |
| 211 | |
| 212 | if (draftId) { |
| 213 | /** |
| 214 | * If the requested entityId includes a draftId, we know we're looking for a specific draft |
| 215 | */ |
| 216 | allFilter.push({ |
| 217 | equal: [{ path: ["draftId"] }, { parameter: draftId }], |
| 218 | }); |
| 219 | } else { |
| 220 | /** |
| 221 | * If the entityId does NOT contain a draftId, EXCLUDE any draft versions of the entity. |
| 222 | * Otherwise, a request for an entityId unqualified by a draftId might return a live version |
| 223 | * and one or more draft versions, which is currently an error in the function. |
| 224 | * |
| 225 | * We could alternatively handle this and prioritise a specific version, e.g. |
| 226 | * - the live version |
| 227 | * - the latest version by the time the edition was created, whether that is a draft or live |
| 228 | * ...whether the prioritisation is fixed behavior or varied by parameter. |
| 229 | */ |
| 230 | allFilter.push({ |
| 231 | not: { exists: { path: ["draftId"] } }, |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | const { |
| 236 | entities: [entity, ...unexpectedEntities], |
| 237 | } = await queryEntities<Properties>(context, authentication, { |
| 238 | filter: { |
| 239 | all: allFilter, |
| 240 | }, |
| 241 | temporalAxes: currentTimeInstantTemporalAxes, |
| 242 | includeDrafts: !!draftId, |
| 243 | includePermissions: false, |
| 244 | }); |
| 245 | |
| 246 | if (unexpectedEntities.length > 0) { |
| 247 | const errorMessage = `Latest entity with entityId ${entityId} returned more than one result with ids: ${unexpectedEntities |
| 248 | .map((unexpectedEntity) => unexpectedEntity.metadata.recordId.entityId) |
| 249 | .join(", ")}`; |
no test coverage detected