@stamhoofd/backend 2.83.1 → 2.83.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stamhoofd/backend",
3
- "version": "2.83.1",
3
+ "version": "2.83.2",
4
4
  "main": "./dist/index.js",
5
5
  "exports": {
6
6
  ".": {
@@ -38,14 +38,14 @@
38
38
  "@simonbackx/simple-encoding": "2.22.0",
39
39
  "@simonbackx/simple-endpoints": "1.19.1",
40
40
  "@simonbackx/simple-logging": "^1.0.1",
41
- "@stamhoofd/backend-i18n": "2.83.1",
42
- "@stamhoofd/backend-middleware": "2.83.1",
43
- "@stamhoofd/email": "2.83.1",
44
- "@stamhoofd/models": "2.83.1",
45
- "@stamhoofd/queues": "2.83.1",
46
- "@stamhoofd/sql": "2.83.1",
47
- "@stamhoofd/structures": "2.83.1",
48
- "@stamhoofd/utility": "2.83.1",
41
+ "@stamhoofd/backend-i18n": "2.83.2",
42
+ "@stamhoofd/backend-middleware": "2.83.2",
43
+ "@stamhoofd/email": "2.83.2",
44
+ "@stamhoofd/models": "2.83.2",
45
+ "@stamhoofd/queues": "2.83.2",
46
+ "@stamhoofd/sql": "2.83.2",
47
+ "@stamhoofd/structures": "2.83.2",
48
+ "@stamhoofd/utility": "2.83.2",
49
49
  "archiver": "^7.0.1",
50
50
  "aws-sdk": "^2.885.0",
51
51
  "axios": "1.6.8",
@@ -65,5 +65,5 @@
65
65
  "publishConfig": {
66
66
  "access": "public"
67
67
  },
68
- "gitHead": "97bf928ed008f1a992380b25385c86caecc43d42"
68
+ "gitHead": "b2f1dbec97a98ef6bc1f04ddeb28f7455078c3bb"
69
69
  }
@@ -214,7 +214,6 @@ export class PlatformMembershipService {
214
214
  membershipTypeId: { sign: 'IN', value: types },
215
215
  deletedAt: null,
216
216
  });
217
- const activeMembershipsUndeletable = activeMemberships.filter(m => !m.canDelete() || !m.generated);
218
217
 
219
218
  if (defaultMemberships.length === 0) {
220
219
  // Stop all active memberships that were added automatically
@@ -233,23 +232,6 @@ export class PlatformMembershipService {
233
232
  continue;
234
233
  }
235
234
 
236
- if (activeMembershipsUndeletable.length) {
237
- // Skip automatic additions
238
- for (const m of activeMembershipsUndeletable) {
239
- try {
240
- await m.calculatePrice(me);
241
- }
242
- catch (e) {
243
- // Ignore error: membership might not be available anymore
244
- if (!silent) {
245
- console.error('Failed to calculate price for undeletable membership', m.id, e);
246
- }
247
- }
248
- await m.save();
249
- }
250
- continue;
251
- }
252
-
253
235
  // Add the cheapest available membership
254
236
  const organizations = await Organization.getByIDs(...Formatter.uniqueArray(defaultMemberships.map(m => m.registration.organizationId)));
255
237
 
@@ -286,28 +268,58 @@ export class PlatformMembershipService {
286
268
  })[0];
287
269
 
288
270
  if (!cheapestMembership) {
271
+ // Technically not possible, but for type checking
289
272
  console.error('No membership found');
290
273
  continue;
291
274
  }
292
275
 
293
276
  // Check if already have the same membership
294
277
  // if that is the case, we'll keep that one and update the price + dates if the organization matches the cheapest/earliest membership
295
- let didFind = false;
278
+ let didFind: MemberPlatformMembership | null = null;
296
279
  for (const m of activeMemberships) {
297
280
  if (m.membershipTypeId === cheapestMembership.membership.id && m.organizationId === cheapestMembership.registration.organizationId) {
298
- // Update the price of this active membership (could have changed)
299
- try {
300
- await m.calculatePrice(me, cheapestMembership.registration);
281
+ if (!m.locked) {
282
+ // Update the price and dates of this active membership (could have changed)
283
+ try {
284
+ await m.calculatePrice(me, cheapestMembership.registration);
285
+ }
286
+ catch (e) {
287
+ // Ignore error: membership might not be available anymore
288
+ if (!silent) {
289
+ console.error('Failed to calculate price for active membership', m.id, e);
290
+ }
291
+ }
292
+ await m.save();
301
293
  }
302
- catch (e) {
303
- // Ignore error: membership might not be available anymore
294
+ didFind = m;
295
+ break;
296
+ }
297
+ }
298
+
299
+ // Delete all other generated memberships that are not the cheapest one
300
+ for (const m of activeMemberships) {
301
+ if (m.id !== didFind?.id) {
302
+ if (!m.locked && (m.generated || m.membershipTypeId === cheapestMembership.membership.id)) {
304
303
  if (!silent) {
305
- console.error('Failed to calculate price for active membership', m.id, e);
304
+ console.log('Removing membership because cheaper membership found or duplicate, for: ' + me.id + ' - membership ' + m.id);
305
+ }
306
+ await m.doDelete();
307
+ }
308
+ else {
309
+ // Update price
310
+ if (!m.locked) {
311
+ try {
312
+ await m.calculatePrice(me);
313
+ }
314
+ catch (e) {
315
+ // Ignore error: membership might not be available anymore
316
+ if (!silent) {
317
+ console.error('Failed to calculate price for undeletable membership', m.id, e);
318
+ }
319
+ }
320
+ await m.save();
306
321
  }
307
322
  }
308
- await m.save();
309
- didFind = true;
310
- break;
311
323
  }
312
324
  }
313
325
 
@@ -315,6 +327,8 @@ export class PlatformMembershipService {
315
327
  continue;
316
328
  }
317
329
 
330
+ // Otherwise make sure we create a new membership
331
+
318
332
  const periodConfig = cheapestMembership.membership.periods.get(period.id);
319
333
  if (!periodConfig) {
320
334
  console.error('Missing membership prices for membership type ' + cheapestMembership.membership.id + ' and period ' + period.id);
@@ -351,16 +365,6 @@ export class PlatformMembershipService {
351
365
 
352
366
  await membership.calculatePrice(me, cheapestMembership.registration);
353
367
  await membership.save();
354
-
355
- // This reasoning allows us to replace an existing membership with a cheaper one (not date based ones, but type based ones)
356
- for (const toDelete of activeMemberships) {
357
- if (toDelete.canDelete() && toDelete.generated) {
358
- if (!silent) {
359
- console.log('Removing membership because cheaper membership found for: ' + me.id + ' - membership ' + toDelete.id);
360
- }
361
- await toDelete.doDelete();
362
- }
363
- }
364
368
  }
365
369
  });
366
370
  });