@cardware/cardware-link 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,9 @@ export declare const refuseDeleteWhileLinked: (relationTo: CollectionSlug) => ({
17
17
  }) => Promise<void>;
18
18
  /**
19
19
  * Moves each link whose stored destination no longer matches its document's to the document's current one, on the
20
- * service and then in the stored copy. Answers the failures; a later attempt starts again from the stored copies.
20
+ * service and then in the stored copy. Answers the moves the service refused; a later attempt starts again from the
21
+ * stored copies. A stored copy that cannot be written is not such a refusal: that error is thrown, because in a
22
+ * transaction nothing written after it would be kept.
21
23
  */
22
24
  export declare const moveLinks: (req: Pick<PayloadRequest, 'payload'> & Partial<PayloadRequest>, resolved: Resolved, next: string, links: Pick<StoredLink, 'id' | 'key' | 'destination'>[]) => Promise<string[]>;
23
25
  /**
@@ -247,25 +247,28 @@ export const refuseDeleteWhileLinked = (relationTo) => async ({ id, req }) => {
247
247
  };
248
248
  /**
249
249
  * Moves each link whose stored destination no longer matches its document's to the document's current one, on the
250
- * service and then in the stored copy. Answers the failures; a later attempt starts again from the stored copies.
250
+ * service and then in the stored copy. Answers the moves the service refused; a later attempt starts again from the
251
+ * stored copies. A stored copy that cannot be written is not such a refusal: that error is thrown, because in a
252
+ * transaction nothing written after it would be kept.
251
253
  */
252
254
  export const moveLinks = async (req, resolved, next, links) => {
253
255
  const failed = [];
254
256
  for (const link of links.filter((each) => each.key && each.destination !== next)) {
255
257
  try {
256
258
  await resolved.service('Not moved', (client) => client.update(link.key, { destination: next }));
257
- await req.payload.db.updateOne({
258
- collection: LINKS,
259
- id: link.id,
260
- data: { destination: next },
261
- returning: false,
262
- req,
263
- });
264
259
  }
265
260
  catch (error) {
266
261
  failed.push(error instanceof Error ? error.message : String(error));
267
262
  req.payload.logger.error({ err: error, msg: `cardware.link: link ${link.key} not moved to ${next}` });
263
+ continue;
268
264
  }
265
+ await req.payload.db.updateOne({
266
+ collection: LINKS,
267
+ id: link.id,
268
+ data: { destination: next },
269
+ returning: false,
270
+ req,
271
+ });
269
272
  }
270
273
  return failed;
271
274
  };
@@ -294,7 +297,11 @@ export const followDestination = (resolved, collection) => async ({ doc, operati
294
297
  const next = destinationOf(collection, stored);
295
298
  const moving = links.filter((link) => link.key && link.destination !== next);
296
299
  const failed = await moveLinks(req, resolved, next, moving);
297
- if (failed.length > 0)
300
+ // Throwing undoes this save, and in a transaction it also undoes the stored copies of the moves the service
301
+ // took. Those links would then open an address that no copy here holds, and the sync, which compares the
302
+ // copies with the document, would not find them. So the save is refused only when the service moved nothing.
303
+ // Otherwise it stands, and the next save or sync moves the rest.
304
+ if (failed.length > 0 && failed.length === moving.length)
298
305
  throw new APIError(`${failed.length} of ${moving.length} tracked links to this still open the old address (${failed[0]}). Save again to retry; the next sync also retries.`, 502);
299
306
  return doc;
300
307
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cardware/cardware-link",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "The client for cardware.link, CardWare's link-tracking service, and its Payload plugin.",
5
5
  "license": "MIT",
6
6
  "type": "module",