@stamhoofd/backend 2.79.8 → 2.80.0

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.
@@ -0,0 +1,105 @@
1
+ import { Decoder } from '@simonbackx/simple-encoding';
2
+ import { DecodedRequest, Endpoint, Request, Response } from '@simonbackx/simple-endpoints';
3
+ import { SimpleError } from '@simonbackx/simple-errors';
4
+ import { ChargeMembersRequest, LimitedFilteredRequest, PermissionLevel } from '@stamhoofd/structures';
5
+
6
+ import { QueueHandler } from '@stamhoofd/queues';
7
+ import { Context } from '../../../helpers/Context';
8
+ import { fetchToAsyncIterator } from '../../../helpers/fetchToAsyncIterator';
9
+ import { MemberCharger } from '../../../helpers/MemberCharger';
10
+ import { GetMembersEndpoint } from '../../global/members/GetMembersEndpoint';
11
+
12
+ type Params = Record<string, never>;
13
+ type Query = LimitedFilteredRequest;
14
+ type Body = ChargeMembersRequest;
15
+ type ResponseBody = undefined;
16
+
17
+ export class ChargeMembersEndpoint extends Endpoint<Params, Query, Body, ResponseBody> {
18
+ queryDecoder = LimitedFilteredRequest as Decoder<LimitedFilteredRequest>;
19
+ bodyDecoder = ChargeMembersRequest as Decoder<ChargeMembersRequest>;
20
+
21
+ protected doesMatch(request: Request): [true, Params] | [false] {
22
+ if (request.method !== 'POST') {
23
+ return [false];
24
+ }
25
+
26
+ const params = Endpoint.parseParameters(request.url, '/admin/charge-members', {});
27
+
28
+ if (params) {
29
+ return [true, params as Params];
30
+ }
31
+ return [false];
32
+ }
33
+
34
+ private static throwIfInvalidBody(body: Body) {
35
+ if (!body.description?.trim()?.length) {
36
+ throw new SimpleError({
37
+ code: 'invalid_field',
38
+ message: 'Invalid description',
39
+ human: 'Beschrijving is verplicht',
40
+ field: 'description',
41
+ });
42
+ }
43
+
44
+ if (!body.price) {
45
+ throw new SimpleError({
46
+ code: 'invalid_field',
47
+ message: 'Invalid price',
48
+ human: 'Bedrag kan niet 0 zijn',
49
+ field: 'price',
50
+ });
51
+ }
52
+
53
+ if (body.amount === 0) {
54
+ throw new SimpleError({
55
+ code: 'invalid_field',
56
+ message: 'Invalid amount',
57
+ human: 'Aantal kan niet 0 zijn',
58
+ field: 'amount',
59
+ });
60
+ }
61
+ }
62
+
63
+ async handle(request: DecodedRequest<Params, Query, Body>) {
64
+ const organization = await Context.setOrganizationScope();
65
+ const body = request.body;
66
+
67
+ await Context.authenticate();
68
+
69
+ if (!await Context.auth.canManagePayments(organization.id)) {
70
+ throw Context.auth.error();
71
+ }
72
+
73
+ ChargeMembersEndpoint.throwIfInvalidBody(body);
74
+
75
+ const queueId = 'charge-members';
76
+
77
+ if (QueueHandler.isRunning(queueId)) {
78
+ throw new SimpleError({
79
+ code: 'charge_pending',
80
+ message: 'Charge members already pending',
81
+ human: 'Er is al een aanrekening bezig, even geduld.',
82
+ });
83
+ }
84
+
85
+ await QueueHandler.schedule(queueId, async () => {
86
+ const dataGenerator = fetchToAsyncIterator(request.query, {
87
+ fetch: request => GetMembersEndpoint.buildData(request, PermissionLevel.Write),
88
+ });
89
+
90
+ for await (const data of dataGenerator) {
91
+ await MemberCharger.chargeMany({
92
+ chargingOrganizationId: organization.id,
93
+ membersToCharge: data.members,
94
+ price: body.price,
95
+ amount: body.amount ?? 1,
96
+ description: body.description,
97
+ dueAt: body.dueAt,
98
+ createdAt: body.createdAt,
99
+ });
100
+ }
101
+ });
102
+
103
+ return new Response(undefined);
104
+ }
105
+ }
@@ -1,7 +1,7 @@
1
1
  import { Decoder } from '@simonbackx/simple-encoding';
2
2
  import { DecodedRequest, Endpoint, Request, Response } from '@simonbackx/simple-endpoints';
3
3
  import { SimpleError } from '@simonbackx/simple-errors';
4
- import { ChargeOrganizationsRequest, LimitedFilteredRequest, Organization as OrganizationStruct } from '@stamhoofd/structures';
4
+ import { ChargeOrganizationsRequest, LimitedFilteredRequest } from '@stamhoofd/structures';
5
5
 
6
6
  import { QueueHandler } from '@stamhoofd/queues';
7
7
  import { Context } from '../../../helpers/Context';
@@ -59,7 +59,7 @@ export class ChargeOrganizationsEndpoint extends Endpoint<Params, Query, Body, R
59
59
  });
60
60
  }
61
61
 
62
- if (body.organizationId === undefined) {
62
+ if (!body.organizationId) {
63
63
  throw new SimpleError({
64
64
  code: 'invalid_field',
65
65
  message: 'Invalid organization id',
@@ -94,19 +94,15 @@ export class ChargeOrganizationsEndpoint extends Endpoint<Params, Query, Body, R
94
94
  fetch: GetOrganizationsEndpoint.buildData,
95
95
  });
96
96
 
97
- const organizationId = body.organizationId;
98
- const chargeOrganizations = organizationId === null
99
- ? OrganizationCharger.chargeFromPlatform
100
- : (args: { organizationsToCharge: OrganizationStruct[]; price: number; amount?: number; description: string }) => OrganizationCharger.chargeMany({
101
- chargingOrganizationId: organizationId,
102
- ...args });
103
-
104
97
  for await (const data of dataGenerator) {
105
- await chargeOrganizations({
98
+ await OrganizationCharger.chargeMany({
99
+ chargingOrganizationId: body.organizationId,
106
100
  organizationsToCharge: data,
107
101
  price: body.price,
108
102
  amount: body.amount ?? 1,
109
103
  description: body.description,
104
+ dueAt: body.dueAt,
105
+ createdAt: body.createdAt,
110
106
  });
111
107
  }
112
108
  });