@dappermountain/payload-plugin-achievements 0.5.0 → 0.5.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.
@@ -1 +1 @@
1
- {"version":3,"file":"builtInRules.d.ts","sourceRoot":"","sources":["../../../src/services/achievement/builtInRules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2B,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AA4OlF,eAAO,MAAM,gBAAgB,EAAE,mBAAmB,EA6IjD,CAAA"}
1
+ {"version":3,"file":"builtInRules.d.ts","sourceRoot":"","sources":["../../../src/services/achievement/builtInRules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2B,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AA4PlF,eAAO,MAAM,gBAAgB,EAAE,mBAAmB,EAiJjD,CAAA"}
@@ -20,6 +20,7 @@ async function resolveMetricId(args) {
20
20
  depth: 0,
21
21
  limit: 1,
22
22
  overrideAccess: true,
23
+ req: args.req,
23
24
  where: { slug: { equals: metricSlug } },
24
25
  });
25
26
  return relationId(found.docs[0]);
@@ -34,6 +35,7 @@ async function resolveEventTypeId(args) {
34
35
  depth: 0,
35
36
  limit: 1,
36
37
  overrideAccess: true,
38
+ req: args.req,
37
39
  where: { id: { equals: eventTypeId } },
38
40
  });
39
41
  if (relationId(byId.docs[0]))
@@ -49,6 +51,7 @@ async function resolveEventTypeId(args) {
49
51
  depth: 0,
50
52
  limit: 1,
51
53
  overrideAccess: true,
54
+ req: args.req,
52
55
  where: { slug: { equals: slugCandidate } },
53
56
  });
54
57
  return relationId(found.docs[0]);
@@ -67,6 +70,7 @@ async function resolveActorTierMinimumRank(args) {
67
70
  id: fromRelation,
68
71
  depth: 0,
69
72
  overrideAccess: true,
73
+ req: args.req,
70
74
  select: { rank: true },
71
75
  }));
72
76
  if (doc && typeof doc.rank === 'number' && Number.isFinite(doc.rank))
@@ -83,6 +87,7 @@ async function resolveActorTierMinimumRank(args) {
83
87
  depth: 0,
84
88
  limit: 1,
85
89
  overrideAccess: true,
90
+ req: args.req,
86
91
  where: { slug: { equals: slug } },
87
92
  select: { rank: true },
88
93
  });
@@ -90,7 +95,11 @@ async function resolveActorTierMinimumRank(args) {
90
95
  return typeof rank === 'number' && Number.isFinite(rank) ? rank : null;
91
96
  }
92
97
  async function countMatchingEventLogs(args) {
93
- const eventTypeId = await resolveEventTypeId({ payload: args.payload, rule: args.rule });
98
+ const eventTypeId = await resolveEventTypeId({
99
+ payload: args.payload,
100
+ req: args.req,
101
+ rule: args.rule,
102
+ });
94
103
  if (!eventTypeId)
95
104
  return 0;
96
105
  const where = {
@@ -101,12 +110,14 @@ async function countMatchingEventLogs(args) {
101
110
  };
102
111
  const minimumRank = await resolveActorTierMinimumRank({
103
112
  payload: args.payload,
113
+ req: args.req,
104
114
  rule: args.rule,
105
115
  });
106
116
  if (minimumRank == null) {
107
117
  const result = await args.payload.count({
108
118
  collection: collectionOf('logs'),
109
119
  overrideAccess: true,
120
+ req: args.req,
110
121
  where,
111
122
  });
112
123
  return result.totalDocs;
@@ -116,6 +127,7 @@ async function countMatchingEventLogs(args) {
116
127
  depth: 0,
117
128
  overrideAccess: true,
118
129
  pagination: false,
130
+ req: args.req,
119
131
  select: { actorTiers: true },
120
132
  where,
121
133
  });
@@ -136,6 +148,7 @@ async function resolveAchievementRef(args) {
136
148
  depth: 0,
137
149
  limit: 1,
138
150
  overrideAccess: true,
151
+ req: args.req,
139
152
  where: args.scopeId
140
153
  ? {
141
154
  and: [{ slug: { equals: requiredSlug } }, { scope: { equals: args.scopeId } }],
@@ -154,6 +167,7 @@ async function resolveAchievementRef(args) {
154
167
  id: requiredId,
155
168
  depth: 0,
156
169
  overrideAccess: true,
170
+ req: args.req,
157
171
  }));
158
172
  requiredSlug = requiredSlug || String(doc?.slug ?? '');
159
173
  eligibilityRules = eligibilityRules ?? doc?.eligibilityRules;
@@ -174,6 +188,7 @@ async function isAchievementGranted(args) {
174
188
  depth: args.requiredSlug && !args.requiredId ? 1 : 0,
175
189
  limit: args.requiredId ? 1 : 50,
176
190
  overrideAccess: true,
191
+ req: args.req,
177
192
  where: { and: grantClauses },
178
193
  });
179
194
  if (args.requiredId)
@@ -199,6 +214,7 @@ export const builtInRuleTypes = [
199
214
  id: tierId,
200
215
  depth: 0,
201
216
  overrideAccess: true,
217
+ req,
202
218
  }));
203
219
  requiredRank = tier?.rank;
204
220
  }
@@ -211,6 +227,7 @@ export const builtInRuleTypes = [
211
227
  depth: 0,
212
228
  limit: 1,
213
229
  overrideAccess: true,
230
+ req,
214
231
  where: scopeId
215
232
  ? { and: [{ slug: { equals: tierSlug } }, { scope: { equals: scopeId } }] }
216
233
  : { slug: { equals: tierSlug } },
@@ -235,10 +252,11 @@ export const builtInRuleTypes = [
235
252
  },
236
253
  {
237
254
  type: 'achievement-complete',
238
- async evaluate({ payload, rule, userId, scopeId }) {
239
- const ref = await resolveAchievementRef({ payload, rule, scopeId });
255
+ async evaluate({ payload, req, rule, userId, scopeId }) {
256
+ const ref = await resolveAchievementRef({ payload, req, rule, scopeId });
240
257
  return isAchievementGranted({
241
258
  payload,
259
+ req,
242
260
  userId,
243
261
  scopeId,
244
262
  requiredId: ref.id,
@@ -247,11 +265,12 @@ export const builtInRuleTypes = [
247
265
  },
248
266
  async progress(args) {
249
267
  const { payload, req, rule, userId, scopeId, ladderRank, progressVisited } = args;
250
- const ref = await resolveAchievementRef({ payload, rule, scopeId });
268
+ const ref = await resolveAchievementRef({ payload, req, rule, scopeId });
251
269
  if (!ref.id && !ref.slug)
252
270
  return 0;
253
271
  const granted = await isAchievementGranted({
254
272
  payload,
273
+ req,
255
274
  userId,
256
275
  scopeId,
257
276
  requiredId: ref.id,
@@ -284,7 +303,7 @@ export const builtInRuleTypes = [
284
303
  {
285
304
  type: 'metric-minimum',
286
305
  async evaluate({ payload, req, rule, userId, scopeId }) {
287
- const resolvedId = await resolveMetricId({ payload, rule });
306
+ const resolvedId = await resolveMetricId({ payload, req, rule });
288
307
  if (!resolvedId)
289
308
  return false;
290
309
  const minimum = Number(rule.minimum ?? 0);
@@ -298,7 +317,7 @@ export const builtInRuleTypes = [
298
317
  return value >= minimum;
299
318
  },
300
319
  async progress({ payload, req, rule, userId, scopeId }) {
301
- const resolvedId = await resolveMetricId({ payload, rule });
320
+ const resolvedId = await resolveMetricId({ payload, req, rule });
302
321
  if (!resolvedId)
303
322
  return 0;
304
323
  const minimum = Number(rule.minimum ?? 0);
@@ -316,16 +335,16 @@ export const builtInRuleTypes = [
316
335
  },
317
336
  {
318
337
  type: 'event-count',
319
- async evaluate({ payload, rule, userId, scopeId }) {
338
+ async evaluate({ payload, req, rule, userId, scopeId }) {
320
339
  const needed = Number(rule.count ?? 1);
321
- const total = await countMatchingEventLogs({ payload, rule, userId, scopeId });
340
+ const total = await countMatchingEventLogs({ payload, req, rule, userId, scopeId });
322
341
  return total >= needed;
323
342
  },
324
- async progress({ payload, rule, userId, scopeId }) {
343
+ async progress({ payload, req, rule, userId, scopeId }) {
325
344
  const needed = Number(rule.count ?? 1);
326
345
  if (!(needed > 0))
327
346
  return 1;
328
- const total = await countMatchingEventLogs({ payload, rule, userId, scopeId });
347
+ const total = await countMatchingEventLogs({ payload, req, rule, userId, scopeId });
329
348
  return clamp01(total / needed);
330
349
  },
331
350
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dappermountain/payload-plugin-achievements",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "A robust, Admin-configured achievements engine for Payload: composable unlock rules, derived rank progress, request/review grants, metrics, and scoped multi-tenant support.",
5
5
  "license": "MIT",
6
6
  "type": "module",