@extantape/payload-plugin-membership 0.5.1 → 0.5.2

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.
package/README.md CHANGED
@@ -83,6 +83,13 @@ In Stripe, add a webhook endpoint `https://<your site>/api/memberships/stripe-we
83
83
  Amounts are in cents. Server code can call `startCheckout`, `changeMembership`, `cancelMembership`,
84
84
  `portalLink`, `myMembership` and `getBackers` directly.
85
85
 
86
+ When two saves reach one record at the same moment, MongoDB keeps one and refuses the other. Over REST
87
+ the refused save answers 409 (`Another change to this record was being saved at the same moment, so
88
+ this one was not saved. Try again.`) and nothing of it is stored, so send it again. This covers
89
+ saves of a single record only: Payload's own REST routes by id for the plugin's four collections,
90
+ and the wall endpoint. A bulk PATCH (one with a `where`) that meets a conflict still answers 500,
91
+ with nothing of it stored. GraphQL and the Local API pass on the database's error as before.
92
+
86
93
  ## Options
87
94
 
88
95
  | Option | Default | |
@@ -185,7 +192,11 @@ A manager of a membership hides its wall entry in the admin form by clearing the
185
192
  saving. The box is open to them only while the entry is shown. Once it is hidden the box is
186
193
  read-only, and only the member can show it again, through the wall endpoint. The wall endpoint
187
194
  (`POST /api/memberships/:id/wall` with `show: false`), REST and the Local API hide an entry too. One
188
- check decides all of them, so the admin form allows what the wall endpoint allows. A save
195
+ check decides who may hide on all of them, so the admin form allows what the wall endpoint allows.
196
+ The wall endpoint also looks the membership up on the request's site first. With `scopeField`, a
197
+ membership with no site is on no site, so the wall endpoint answers 404 for it, while a manager of
198
+ every row (`canManage` answering `true`) can still hide it through the field: the admin form, REST
199
+ or the Local API. A save
189
200
  from a manager that sets `show` to `true` leaves the entry hidden and saves the other fields.
190
201
  Server code that writes with `overrideAccess` (checkout, Stripe events) is unchanged. A manager can
191
202
  set a category's `parent` only to a category in their scope. That is judged on the parent the row
package/dist/guard.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CollectionBeforeOperationHook, CollectionConfig, Field, PayloadRequest } from 'payload';
1
+ import type { CollectionAfterErrorHook, CollectionBeforeOperationHook, CollectionConfig, Field, PayloadRequest } from 'payload';
2
2
  import type { ResolvedMembership } from './types.js';
3
3
  export declare const BAD_KEY_MESSAGE = "Field names in a save can\u2019t start with a dollar sign or contain a dot. Send each field by its plain name.";
4
4
  /**
@@ -17,5 +17,24 @@ export declare function refuseBadKeys(data: unknown, fields: Field[] | null | un
17
17
  * restore a version that is itself inside their scope. A version that can't be read is refused.
18
18
  */
19
19
  export declare function writeGuard(o: ResolvedMembership): CollectionBeforeOperationHook;
20
- /** Puts the guard in front of the collection's `beforeOperation` hooks as they are after the host's overrides. */
20
+ /** The answer to a REST save the database refused because another save to the same record committed first. */
21
+ export declare const SAME_MOMENT_MESSAGE = "Another change to this record was being saved at the same moment, so this one was not saved. Try again.";
22
+ /**
23
+ * The database's own write conflict, and only that: MongoDB's code 112 or its code name. The
24
+ * message is never read, so another error that happens to mention a write conflict keeps its answer.
25
+ */
26
+ export declare function isDatabaseWriteConflict(error: unknown): boolean;
27
+ /**
28
+ * Two saves to one record at the same moment: the database lets one commit and aborts the other's
29
+ * transaction, so nothing of the other is stored. Over REST that one answers 409 with plain words
30
+ * and the caller sends it again. Every other error is left as it is. The save isn't retried here:
31
+ * a host's hooks may already have run for it. GraphQL and Local API callers get the database's
32
+ * error unchanged.
33
+ */
34
+ export declare const sameMomentConflict: CollectionAfterErrorHook;
35
+ /**
36
+ * Puts the guard in front of the collection's `beforeOperation` hooks as they are after the host's
37
+ * overrides, and the same-moment answer in front of its `afterError` hooks, so a host's own hook
38
+ * still sees the error and can answer differently.
39
+ */
21
40
  export declare function guarded(o: ResolvedMembership, c: CollectionConfig): CollectionConfig;
package/dist/guard.js CHANGED
@@ -149,7 +149,35 @@ export function writeGuard(o) {
149
149
  return args;
150
150
  };
151
151
  }
152
- /** Puts the guard in front of the collection's `beforeOperation` hooks as they are after the host's overrides. */
152
+ /** The answer to a REST save the database refused because another save to the same record committed first. */
153
+ export const SAME_MOMENT_MESSAGE = 'Another change to this record was being saved at the same moment, so this one was not saved. Try again.';
154
+ /**
155
+ * The database's own write conflict, and only that: MongoDB's code 112 or its code name. The
156
+ * message is never read, so another error that happens to mention a write conflict keeps its answer.
157
+ */
158
+ export function isDatabaseWriteConflict(error) {
159
+ const e = error;
160
+ if (!e || typeof e !== 'object')
161
+ return false;
162
+ return e.code === 112 || e.codeName === 'WriteConflict';
163
+ }
164
+ /**
165
+ * Two saves to one record at the same moment: the database lets one commit and aborts the other's
166
+ * transaction, so nothing of the other is stored. Over REST that one answers 409 with plain words
167
+ * and the caller sends it again. Every other error is left as it is. The save isn't retried here:
168
+ * a host's hooks may already have run for it. GraphQL and Local API callers get the database's
169
+ * error unchanged.
170
+ */
171
+ export const sameMomentConflict = ({ error }) => {
172
+ if (!isDatabaseWriteConflict(error))
173
+ return;
174
+ return { status: 409, response: { errors: [{ message: SAME_MOMENT_MESSAGE }] } };
175
+ };
176
+ /**
177
+ * Puts the guard in front of the collection's `beforeOperation` hooks as they are after the host's
178
+ * overrides, and the same-moment answer in front of its `afterError` hooks, so a host's own hook
179
+ * still sees the error and can answer differently.
180
+ */
153
181
  export function guarded(o, c) {
154
- return { ...c, hooks: { ...c.hooks, beforeOperation: [writeGuard(o), ...(c.hooks?.beforeOperation ?? [])] } };
182
+ return { ...c, hooks: { ...c.hooks, beforeOperation: [writeGuard(o), ...(c.hooks?.beforeOperation ?? [])], afterError: [sameMomentConflict, ...(c.hooks?.afterError ?? [])] } };
155
183
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extantape/payload-plugin-membership",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Memberships for Payload: Patreon-style tiers or pick-your-own-amount pledges to categories, billed as Stripe subscriptions, with a backers wall and hooks for perks.",
5
5
  "homepage": "https://github.com/ExtantApe/payload-plugin-membership#readme",
6
6
  "bugs": {