@stamhoofd/models 2.137.4 → 2.138.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.
- package/dist/helpers/Handlebars.d.ts.map +1 -1
- package/dist/helpers/Handlebars.js +1 -1
- package/dist/helpers/Handlebars.js.map +1 -1
- package/dist/helpers/errorToSimpleErrors.d.ts +3 -0
- package/dist/helpers/errorToSimpleErrors.d.ts.map +1 -0
- package/dist/helpers/errorToSimpleErrors.js +17 -0
- package/dist/helpers/errorToSimpleErrors.js.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/migrations/1782902314-tokens-authenticated-at.sql +2 -0
- package/dist/migrations/1782902315-create-mfa-totp-table.sql +13 -0
- package/dist/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
- package/dist/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
- package/dist/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
- package/dist/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
- package/dist/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
- package/dist/migrations/1784122354-webauthn-credential-provider.sql +3 -0
- package/dist/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
- package/dist/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
- package/dist/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
- package/dist/migrations/1785354093-platform-uri.sql +1 -0
- package/dist/migrations/1785354094-platform-domain.sql +1 -0
- package/dist/migrations/1785354095-platform-uri-unique.sql +1 -0
- package/dist/migrations/1785354096-platform-domain-unique.sql +1 -0
- package/dist/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.d.ts +4 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.d.ts.map +1 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.js +10 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.js.map +1 -0
- package/dist/migrations/1785417301-users-last-active-at.sql +2 -0
- package/dist/models/Document.d.ts +0 -4
- package/dist/models/Document.d.ts.map +1 -1
- package/dist/models/Document.js +1 -75
- package/dist/models/Document.js.map +1 -1
- package/dist/models/DocumentTemplate.d.ts +0 -3
- package/dist/models/DocumentTemplate.d.ts.map +1 -1
- package/dist/models/DocumentTemplate.js +1 -63
- package/dist/models/DocumentTemplate.js.map +1 -1
- package/dist/models/Email.d.ts +1 -29
- package/dist/models/Email.d.ts.map +1 -1
- package/dist/models/Email.js +4 -823
- package/dist/models/Email.js.map +1 -1
- package/dist/models/EmailVerificationCode.d.ts +0 -5
- package/dist/models/EmailVerificationCode.d.ts.map +1 -1
- package/dist/models/EmailVerificationCode.js +0 -84
- package/dist/models/EmailVerificationCode.js.map +1 -1
- package/dist/models/Image.d.ts.map +1 -1
- package/dist/models/Image.js +11 -14
- package/dist/models/Image.js.map +1 -1
- package/dist/models/MFARecoveryCode.d.ts +17 -0
- package/dist/models/MFARecoveryCode.d.ts.map +1 -0
- package/dist/models/MFARecoveryCode.js +55 -0
- package/dist/models/MFARecoveryCode.js.map +1 -0
- package/dist/models/MFATOTP.d.ts +44 -0
- package/dist/models/MFATOTP.d.ts.map +1 -0
- package/dist/models/MFATOTP.js +116 -0
- package/dist/models/MFATOTP.js.map +1 -0
- package/dist/models/MFAToken.d.ts +49 -0
- package/dist/models/MFAToken.d.ts.map +1 -0
- package/dist/models/MFAToken.js +137 -0
- package/dist/models/MFAToken.js.map +1 -0
- package/dist/models/MemberPlatformMembership.d.ts +0 -8
- package/dist/models/MemberPlatformMembership.d.ts.map +1 -1
- package/dist/models/MemberPlatformMembership.js +1 -242
- package/dist/models/MemberPlatformMembership.js.map +1 -1
- package/dist/models/Order.d.ts +1 -32
- package/dist/models/Order.d.ts.map +1 -1
- package/dist/models/Order.js +1 -182
- package/dist/models/Order.js.map +1 -1
- package/dist/models/Organization.d.ts +1 -51
- package/dist/models/Organization.d.ts.map +1 -1
- package/dist/models/Organization.js +1 -530
- package/dist/models/Organization.js.map +1 -1
- package/dist/models/PasswordToken.d.ts +0 -8
- package/dist/models/PasswordToken.d.ts.map +1 -1
- package/dist/models/PasswordToken.js +0 -23
- package/dist/models/PasswordToken.js.map +1 -1
- package/dist/models/Platform.d.ts +28 -4
- package/dist/models/Platform.d.ts.map +1 -1
- package/dist/models/Platform.js +111 -50
- package/dist/models/Platform.js.map +1 -1
- package/dist/models/STPackage.d.ts +1 -6
- package/dist/models/STPackage.d.ts.map +1 -1
- package/dist/models/STPackage.js +1 -98
- package/dist/models/STPackage.js.map +1 -1
- package/dist/models/Token.d.ts +40 -1
- package/dist/models/Token.d.ts.map +1 -1
- package/dist/models/Token.js +68 -2
- package/dist/models/Token.js.map +1 -1
- package/dist/models/User.d.ts +25 -0
- package/dist/models/User.d.ts.map +1 -1
- package/dist/models/User.js +36 -0
- package/dist/models/User.js.map +1 -1
- package/dist/models/WebauthnChallenge.d.ts +27 -0
- package/dist/models/WebauthnChallenge.d.ts.map +1 -0
- package/dist/models/WebauthnChallenge.js +81 -0
- package/dist/models/WebauthnChallenge.js.map +1 -0
- package/dist/models/WebauthnCredential.d.ts +58 -0
- package/dist/models/WebauthnCredential.d.ts.map +1 -0
- package/dist/models/WebauthnCredential.js +144 -0
- package/dist/models/WebauthnCredential.js.map +1 -0
- package/dist/models/index.d.ts +5 -0
- package/dist/models/index.d.ts.map +1 -1
- package/dist/models/index.js +5 -0
- package/dist/models/index.js.map +1 -1
- package/package.json +3 -3
- package/src/helpers/Handlebars.ts +5 -9
- package/src/helpers/errorToSimpleErrors.ts +17 -0
- package/src/index.ts +0 -1
- package/src/migrations/1782902314-tokens-authenticated-at.sql +2 -0
- package/src/migrations/1782902315-create-mfa-totp-table.sql +13 -0
- package/src/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
- package/src/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
- package/src/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
- package/src/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
- package/src/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
- package/src/migrations/1784122354-webauthn-credential-provider.sql +3 -0
- package/src/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
- package/src/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
- package/src/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
- package/src/migrations/1785354093-platform-uri.sql +1 -0
- package/src/migrations/1785354094-platform-domain.sql +1 -0
- package/src/migrations/1785354095-platform-uri-unique.sql +1 -0
- package/src/migrations/1785354096-platform-domain-unique.sql +1 -0
- package/src/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
- package/src/migrations/1785354098-platform-tenant-backfill.ts +14 -0
- package/src/migrations/1785417301-users-last-active-at.sql +2 -0
- package/src/models/Document.ts +1 -84
- package/src/models/DocumentTemplate.ts +1 -71
- package/src/models/Email.ts +5 -941
- package/src/models/EmailVerificationCode.ts +0 -96
- package/src/models/Image.ts +11 -14
- package/src/models/MFARecoveryCode.ts +52 -0
- package/src/models/MFATOTP.ts +112 -0
- package/src/models/MFAToken.ts +138 -0
- package/src/models/MemberPlatformMembership.ts +1 -285
- package/src/models/Order.ts +1 -204
- package/src/models/Organization.ts +2 -637
- package/src/models/PasswordToken.ts +0 -29
- package/src/models/Platform.test.ts +167 -3
- package/src/models/Platform.ts +125 -50
- package/src/models/STPackage.ts +1 -110
- package/src/models/Token.ts +73 -2
- package/src/models/User.ts +37 -0
- package/src/models/WebauthnChallenge.ts +80 -0
- package/src/models/WebauthnCredential.ts +130 -0
- package/src/models/index.ts +6 -0
- package/dist/helpers/EmailBuilder.d.ts +0 -121
- package/dist/helpers/EmailBuilder.d.ts.map +0 -1
- package/dist/helpers/EmailBuilder.js +0 -659
- package/dist/helpers/EmailBuilder.js.map +0 -1
- package/src/helpers/EmailBuilder.test.ts +0 -291
- package/src/helpers/EmailBuilder.ts +0 -818
- package/src/models/Email.test.ts +0 -1222
- package/src/models/MemberPlatformMembership.test.ts +0 -70
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
import { column } from '@simonbackx/simple-database';
|
|
2
|
-
import {
|
|
3
|
-
import { QueryableModel, SQL, SQLWhereSign } from '@stamhoofd/sql';
|
|
4
|
-
import { PlatformMembershipTypeBehaviour } from '@stamhoofd/structures';
|
|
5
|
-
import { Formatter } from '@stamhoofd/utility';
|
|
2
|
+
import { QueryableModel } from '@stamhoofd/sql';
|
|
6
3
|
import { v4 as uuidv4 } from 'uuid';
|
|
7
4
|
import { BalanceItem } from './BalanceItem.js';
|
|
8
|
-
import type { Member } from './Member.js';
|
|
9
|
-
import { Organization } from './Organization.js';
|
|
10
|
-
import { Platform } from './Platform.js';
|
|
11
|
-
import { Registration } from './Registration.js';
|
|
12
|
-
import { RegistrationPeriod } from './RegistrationPeriod.js';
|
|
13
5
|
|
|
14
6
|
export class MemberPlatformMembership extends QueryableModel {
|
|
15
7
|
static table = 'member_platform_memberships';
|
|
@@ -123,282 +115,6 @@ export class MemberPlatformMembership extends QueryableModel {
|
|
|
123
115
|
throw new Error('Cannot delete a membership. Use the deletedAt column.');
|
|
124
116
|
}
|
|
125
117
|
|
|
126
|
-
/**
|
|
127
|
-
* Returns the last trial until date for this member. Null if this member does not have any trials
|
|
128
|
-
*/
|
|
129
|
-
async isElegibleForTrial(member: Member) {
|
|
130
|
-
const period = await RegistrationPeriod.getByID(this.periodId);
|
|
131
|
-
if (!period) {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (!period.previousPeriodId) {
|
|
136
|
-
// We have no previous period = no data = no trials
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const platform = await Platform.getSharedStruct();
|
|
141
|
-
const typeIds = platform.config.membershipTypes.filter(m => m.behaviour === PlatformMembershipTypeBehaviour.Period).map(m => m.id);
|
|
142
|
-
|
|
143
|
-
const membership = await MemberPlatformMembership.select()
|
|
144
|
-
.where('memberId', member.id)
|
|
145
|
-
.where('deletedAt', null)
|
|
146
|
-
.where('periodId', period.previousPeriodId)
|
|
147
|
-
.where('membershipTypeId', typeIds)
|
|
148
|
-
.first(false);
|
|
149
|
-
|
|
150
|
-
const hasBlockingMemberships = !!membership;
|
|
151
|
-
|
|
152
|
-
if (hasBlockingMemberships) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// The member needs to be registered for at least one registration that had a trial
|
|
157
|
-
const registrations = await Registration.select()
|
|
158
|
-
.where('memberId', member.id)
|
|
159
|
-
.where('periodId', period.id)
|
|
160
|
-
.where('organizationId', this.organizationId)
|
|
161
|
-
.where('deactivatedAt', null)
|
|
162
|
-
.where('registeredAt', '!=', null)
|
|
163
|
-
.where('trialUntil', '!=', null)
|
|
164
|
-
.fetch();
|
|
165
|
-
|
|
166
|
-
if (registrations.length === 0) {
|
|
167
|
-
return null;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const latestTrialUntil = new Date(Math.max(...registrations.map(r => r.trialUntil!.getTime())));
|
|
171
|
-
return latestTrialUntil;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
async correctDates(member: Member, registration?: Registration) {
|
|
175
|
-
if (this.locked) {
|
|
176
|
-
// price of locked membership cannot be changed
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const platform = await Platform.getSharedPrivateStruct();
|
|
181
|
-
const membershipType = platform.config.membershipTypes.find(m => m.id === this.membershipTypeId);
|
|
182
|
-
|
|
183
|
-
if (!membershipType) {
|
|
184
|
-
throw new SimpleError({
|
|
185
|
-
code: 'invalid_membership_type',
|
|
186
|
-
message: 'Unknown membership type',
|
|
187
|
-
human: $t(`%GM`),
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const periodConfig = membershipType.periods.get(this.periodId);
|
|
192
|
-
|
|
193
|
-
if (!periodConfig) {
|
|
194
|
-
throw new SimpleError({
|
|
195
|
-
code: 'period_unavailable',
|
|
196
|
-
message: 'Membership not available for this period',
|
|
197
|
-
human: $t(`%GN`),
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (membershipType.behaviour === PlatformMembershipTypeBehaviour.Days) {
|
|
202
|
-
// Make sure time is equal between start and end date
|
|
203
|
-
let startBrussels = Formatter.luxon(this.startDate);
|
|
204
|
-
let endBrussels = Formatter.luxon(this.endDate);
|
|
205
|
-
startBrussels = startBrussels.set({ hour: 0, minute: 0, second: 0, millisecond: 0 });
|
|
206
|
-
endBrussels = endBrussels.set({ hour: 23, minute: 59, second: 59, millisecond: 0 });
|
|
207
|
-
this.startDate = startBrussels.toJSDate();
|
|
208
|
-
this.endDate = endBrussels.toJSDate();
|
|
209
|
-
this.expireDate = null;
|
|
210
|
-
} else {
|
|
211
|
-
this.endDate = Formatter.luxon(periodConfig.endDate).set({ hour: 23, minute: 59, second: 59, millisecond: 0 }).toJSDate();
|
|
212
|
-
this.expireDate = periodConfig.expireDate ? Formatter.luxon(periodConfig.expireDate).set({ hour: 23, minute: 59, second: 59, millisecond: 0 }).toJSDate() : null;
|
|
213
|
-
|
|
214
|
-
// Alter start date
|
|
215
|
-
if (registration) {
|
|
216
|
-
const preferredStartDate = registration.startDate ?? registration.registeredAt ?? this.createdAt;
|
|
217
|
-
this.startDate = periodConfig.startDate;
|
|
218
|
-
|
|
219
|
-
if (preferredStartDate > periodConfig.startDate && preferredStartDate < periodConfig.endDate) {
|
|
220
|
-
let startBrussels = Formatter.luxon(preferredStartDate);
|
|
221
|
-
startBrussels = startBrussels.set({ hour: 0, minute: 0, second: 0, millisecond: 0 });
|
|
222
|
-
this.startDate = startBrussels.toJSDate();
|
|
223
|
-
}
|
|
224
|
-
} else {
|
|
225
|
-
// Keep the set date, but make sure it is at 0:00 in CET
|
|
226
|
-
let startBrussels = Formatter.luxon(this.startDate);
|
|
227
|
-
startBrussels = startBrussels.set({ hour: 0, minute: 0, second: 0, millisecond: 0 });
|
|
228
|
-
this.startDate = startBrussels.toJSDate();
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
let minimumStartDate = Formatter.luxon(periodConfig.startDate).set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toJSDate();
|
|
233
|
-
|
|
234
|
-
if (!this.existsInDatabase) {
|
|
235
|
-
if (membershipType.behaviour === PlatformMembershipTypeBehaviour.Days) {
|
|
236
|
-
// Cannot create a membership in the past
|
|
237
|
-
minimumStartDate = Formatter.luxon(new Date()).set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toJSDate();
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (this.startDate < minimumStartDate) {
|
|
242
|
-
this.startDate = minimumStartDate;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
if (this.endDate < this.startDate) {
|
|
246
|
-
this.endDate = Formatter.luxon(this.startDate).set({ hour: 23, minute: 59, second: 59, millisecond: 0 }).toJSDate();
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const maximumEndDate = periodConfig.getMaximumEndDate(this.startDate, membershipType.behaviour);
|
|
250
|
-
if (this.endDate > maximumEndDate) {
|
|
251
|
-
throw new SimpleError({
|
|
252
|
-
code: 'invalid_field',
|
|
253
|
-
field: 'endDate',
|
|
254
|
-
message: 'End date is after the maximum allowed end date',
|
|
255
|
-
human: $t('%15C', { date: Formatter.date(maximumEndDate) }),
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (periodConfig.trialDays) {
|
|
260
|
-
// Check whether you are elegible for a trial
|
|
261
|
-
const latestTrialDate = await this.isElegibleForTrial(member);
|
|
262
|
-
if (latestTrialDate !== null) {
|
|
263
|
-
// Allowed to set trial until, maximum periodConfig.trialDays after startDate
|
|
264
|
-
let trialUntil = Formatter.luxon(this.startDate).plus({ days: periodConfig.trialDays });
|
|
265
|
-
trialUntil = trialUntil.set({ hour: 23, minute: 59, second: 59, millisecond: 0 });
|
|
266
|
-
|
|
267
|
-
if (trialUntil.toJSDate() > latestTrialDate) {
|
|
268
|
-
// Use the latest trial date instead
|
|
269
|
-
trialUntil = Formatter.luxon(latestTrialDate);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// Max end date
|
|
273
|
-
if (trialUntil.toJSDate() > this.endDate) {
|
|
274
|
-
trialUntil = Formatter.luxon(this.endDate).set({ hour: 23, minute: 59, second: 59, millisecond: 0 });
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
this.trialUntil = trialUntil.toJSDate();
|
|
278
|
-
} else {
|
|
279
|
-
this.trialUntil = null;
|
|
280
|
-
}
|
|
281
|
-
} else {
|
|
282
|
-
// No trial
|
|
283
|
-
this.trialUntil = null;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
async calculatePrice(member: Member, registration?: Registration) {
|
|
288
|
-
if (this.locked) {
|
|
289
|
-
// price of locked membership cannot be changed
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
const platform = await Platform.getSharedPrivateStruct();
|
|
294
|
-
const membershipType = platform.config.membershipTypes.find(m => m.id === this.membershipTypeId);
|
|
295
|
-
|
|
296
|
-
if (!membershipType) {
|
|
297
|
-
throw new SimpleError({
|
|
298
|
-
code: 'invalid_membership_type',
|
|
299
|
-
message: 'Unknown membership type',
|
|
300
|
-
human: $t(`%GM`),
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const periodConfig = membershipType.periods.get(this.periodId);
|
|
305
|
-
|
|
306
|
-
if (!periodConfig) {
|
|
307
|
-
throw new SimpleError({
|
|
308
|
-
code: 'period_unavailable',
|
|
309
|
-
message: 'Membership not available for this period',
|
|
310
|
-
human: $t(`%GN`),
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const organization = await Organization.getByID(this.organizationId);
|
|
315
|
-
if (!organization) {
|
|
316
|
-
throw new SimpleError({
|
|
317
|
-
// todo
|
|
318
|
-
code: 'not_found',
|
|
319
|
-
message: 'Organization not found',
|
|
320
|
-
human: $t(`%GO`),
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const tagIds = organization.meta.tags;
|
|
325
|
-
const shouldApplyReducedPrice = member.details.shouldApplyReducedPrice;
|
|
326
|
-
|
|
327
|
-
// Correct dates so we get the correct price config
|
|
328
|
-
await this.correctDates(member, registration);
|
|
329
|
-
const priceConfig = periodConfig.getPriceConfigForDate(this.trialUntil ?? this.startDate);
|
|
330
|
-
const earliestPriceConfig = periodConfig.getPriceConfigForDate(new Date(1950, 0, 1));
|
|
331
|
-
|
|
332
|
-
let freeDays = 0;
|
|
333
|
-
|
|
334
|
-
const d = this.createdAt ?? new Date();
|
|
335
|
-
|
|
336
|
-
if (periodConfig.amountFree) {
|
|
337
|
-
// Check if this organization has rights to free memberships
|
|
338
|
-
const alreadyUsed = await MemberPlatformMembership.select()
|
|
339
|
-
.where('organizationId', this.organizationId)
|
|
340
|
-
.where('membershipTypeId', this.membershipTypeId)
|
|
341
|
-
.where('periodId', this.periodId)
|
|
342
|
-
.where('deletedAt', null)
|
|
343
|
-
.whereNot('id', this.id)
|
|
344
|
-
.where(
|
|
345
|
-
SQL.where('createdAt', SQLWhereSign.Less, d)
|
|
346
|
-
.or(
|
|
347
|
-
SQL.where('createdAt', SQLWhereSign.Equal, d)
|
|
348
|
-
.and('id', SQLWhereSign.Less, this.id),
|
|
349
|
-
),
|
|
350
|
-
)
|
|
351
|
-
.sum(SQL.column('maximumFreeAmount'));
|
|
352
|
-
|
|
353
|
-
if (alreadyUsed < periodConfig.amountFree) {
|
|
354
|
-
freeDays = periodConfig.amountFree - alreadyUsed;
|
|
355
|
-
console.log('Free membership created for ', this.id, periodConfig.amountFree, alreadyUsed);
|
|
356
|
-
} else {
|
|
357
|
-
console.log('No free membership created for', this.id, periodConfig.amountFree, alreadyUsed);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
if (membershipType.behaviour === PlatformMembershipTypeBehaviour.Days) {
|
|
362
|
-
const days = Math.round((this.endDate.getTime() - this.startDate.getTime()) / (1000 * 60 * 60 * 24));
|
|
363
|
-
this.maximumFreeAmount = days;
|
|
364
|
-
this.priceWithoutDiscount = earliestPriceConfig.calculatePrice(tagIds, false, days);
|
|
365
|
-
this.price = priceConfig.calculatePrice(tagIds, shouldApplyReducedPrice, Math.max(0, days - freeDays));
|
|
366
|
-
this.freeAmount = Math.min(days, freeDays);
|
|
367
|
-
} else {
|
|
368
|
-
this.priceWithoutDiscount = earliestPriceConfig.getBasePrice(tagIds, false);
|
|
369
|
-
this.price = priceConfig.getBasePrice(tagIds, shouldApplyReducedPrice);
|
|
370
|
-
this.maximumFreeAmount = this.price > 0 ? 1 : 0;
|
|
371
|
-
this.freeAmount = 0;
|
|
372
|
-
|
|
373
|
-
if (freeDays > 0) {
|
|
374
|
-
this.price = 0;
|
|
375
|
-
this.freeAmount = 1;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
// Never charge itself
|
|
380
|
-
const chargeVia = platform.membershipOrganizationId;
|
|
381
|
-
if (this.organizationId === chargeVia) {
|
|
382
|
-
this.price = 0;
|
|
383
|
-
this.priceWithoutDiscount = 0;
|
|
384
|
-
this.freeAmount = 0;
|
|
385
|
-
this.maximumFreeAmount = 0;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
if (this.balanceItemId) {
|
|
389
|
-
this.maximumFreeAmount = this.freeAmount;
|
|
390
|
-
|
|
391
|
-
// Also update the balance item
|
|
392
|
-
const balanceItem = await BalanceItem.getByID(this.balanceItemId);
|
|
393
|
-
if (balanceItem) {
|
|
394
|
-
balanceItem.unitPrice = this.price;
|
|
395
|
-
await balanceItem.save();
|
|
396
|
-
|
|
397
|
-
// await BalanceItem.updateOutstanding([balanceItem]);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
118
|
async doDelete() {
|
|
403
119
|
this.deletedAt = new Date();
|
|
404
120
|
await this.save();
|
package/src/models/Order.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import type { ManyToOneRelation } from '@simonbackx/simple-database';
|
|
2
2
|
import { column } from '@simonbackx/simple-database';
|
|
3
3
|
import { SimpleError } from '@simonbackx/simple-errors';
|
|
4
|
-
import { I18n } from '@stamhoofd/backend-i18n/I18n';
|
|
5
4
|
import { QueueHandler } from '@stamhoofd/queues';
|
|
6
5
|
import { Language } from '@stamhoofd/types/Language';
|
|
7
6
|
import { QueryableModel, SQL } from '@stamhoofd/sql';
|
|
8
7
|
import type { TransferSettings, WebshopTimeSlot } from '@stamhoofd/structures';
|
|
9
|
-
import { BalanceItemPaymentWithPayment, BalanceItemPaymentWithPrivatePayment, BalanceItemWithPayments, BalanceItemWithPrivatePayments,
|
|
8
|
+
import { BalanceItemPaymentWithPayment, BalanceItemPaymentWithPrivatePayment, BalanceItemWithPayments, BalanceItemWithPrivatePayments, OrderData, OrderStatus, Order as OrderStruct, PaymentMethod, PaymentStatus, Payment as PaymentStruct, PrivateOrder, PrivatePayment, ProductType, WebshopTicketType } from '@stamhoofd/structures';
|
|
10
9
|
import { Formatter } from '@stamhoofd/utility';
|
|
11
10
|
import { v4 as uuidv4 } from 'uuid';
|
|
12
11
|
|
|
13
|
-
import { sendEmailTemplate } from '../helpers/EmailBuilder.js';
|
|
14
|
-
import { WebshopCounter } from '../helpers/WebshopCounter.js';
|
|
15
12
|
import { BalanceItem } from './BalanceItem.js';
|
|
16
13
|
import { BalanceItemPayment } from './BalanceItemPayment.js';
|
|
17
14
|
import type { Organization } from './Organization.js';
|
|
@@ -736,66 +733,6 @@ export class Order extends QueryableModel {
|
|
|
736
733
|
await this.setRelation(Order.webshop, webshop).updateTickets();
|
|
737
734
|
}
|
|
738
735
|
|
|
739
|
-
/**
|
|
740
|
-
* Only call this once! Make sure you use the queues correctly
|
|
741
|
-
*/
|
|
742
|
-
async markPaid(this: Order, payment: Payment | null, organization: Organization, knownWebshop?: Webshop) {
|
|
743
|
-
if (!this.id) {
|
|
744
|
-
await this.save();
|
|
745
|
-
}
|
|
746
|
-
console.log('Marking order ' + this.id + ' as paid');
|
|
747
|
-
|
|
748
|
-
const webshop = (knownWebshop ?? (await Webshop.getByID(this.webshopId)))?.setRelation(Webshop.organization, organization);
|
|
749
|
-
if (!webshop) {
|
|
750
|
-
console.error('Missing webshop for order ' + this.id);
|
|
751
|
-
return;
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
if (this.status === OrderStatus.Deleted) {
|
|
755
|
-
await this.undoPaymentFailed(payment, organization);
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
const { tickets, didCreateTickets } = await this.setRelation(Order.webshop, webshop).updateTickets({ hasPaidPayment: true });
|
|
759
|
-
|
|
760
|
-
// Needs to happen before validation, because we can include the tickets in the validation that way
|
|
761
|
-
if (this.validAt === null) {
|
|
762
|
-
await this.setRelation(Order.webshop, webshop).markValid(payment, tickets);
|
|
763
|
-
} else {
|
|
764
|
-
this.markUpdated();
|
|
765
|
-
await this.save();
|
|
766
|
-
|
|
767
|
-
if (!this.data.shouldSendPaymentUpdates) {
|
|
768
|
-
console.log('Skip sending paid email for order ' + this.id);
|
|
769
|
-
return;
|
|
770
|
-
}
|
|
771
|
-
if (this.data.customer.email.length > 0) {
|
|
772
|
-
if (didCreateTickets) {
|
|
773
|
-
await this.setRelation(Order.webshop, webshop).sendTickets();
|
|
774
|
-
} else {
|
|
775
|
-
if (payment && payment.method === PaymentMethod.Transfer) {
|
|
776
|
-
await this.setRelation(Order.webshop, webshop).sendPaidMail();
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
async sendPaidMail(this: Order & { webshop: Webshop & { organization: Organization } }) {
|
|
784
|
-
// For a tickets webshop, where the order was marked as paid / non-paid, we should still send the tickets email
|
|
785
|
-
// - because the normal email is not editable
|
|
786
|
-
const hasTickets = this.webshop.meta.hasTickets;
|
|
787
|
-
|
|
788
|
-
await this.sendEmailTemplate({
|
|
789
|
-
type: hasTickets ? EmailTemplateType.TicketsReceivedTransfer : EmailTemplateType.OrderReceivedTransfer,
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
async sendTickets(this: Order & { webshop: Webshop & { organization: Organization } }) {
|
|
794
|
-
await this.sendEmailTemplate({
|
|
795
|
-
type: EmailTemplateType.TicketsReceivedTransfer,
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
|
|
799
736
|
getStructureWithoutPayment() {
|
|
800
737
|
return OrderStruct.create({
|
|
801
738
|
id: this.id,
|
|
@@ -901,47 +838,6 @@ export class Order extends QueryableModel {
|
|
|
901
838
|
return (await Order.getStructures([this]))[0];
|
|
902
839
|
}
|
|
903
840
|
|
|
904
|
-
async sendEmailTemplate(this: Order & { webshop: Webshop & { organization: Organization } }, data: {
|
|
905
|
-
type: EmailTemplateType;
|
|
906
|
-
to?: Recipient;
|
|
907
|
-
}) {
|
|
908
|
-
// Never send an email for archived webshops
|
|
909
|
-
if (this.webshop.meta.status === WebshopStatus.Archived) {
|
|
910
|
-
return;
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
// Render all $t's (recipient replacements, order tables, template...) in the language
|
|
914
|
-
// the customer used while placing the order, instead of the current request language.
|
|
915
|
-
const i18n = new I18n(this.consumerLanguage, this.webshop.organization.address.country);
|
|
916
|
-
|
|
917
|
-
await I18n.runWithLocale(i18n, async () => {
|
|
918
|
-
let recipient = (await this.getStructure()).getRecipient(
|
|
919
|
-
this.webshop.organization.getBaseStructure(),
|
|
920
|
-
WebshopPreview.create(this.webshop),
|
|
921
|
-
);
|
|
922
|
-
|
|
923
|
-
if (data.to) {
|
|
924
|
-
// Clear first and last name
|
|
925
|
-
recipient.firstName = null;
|
|
926
|
-
recipient.lastName = null;
|
|
927
|
-
recipient.language = this.webshop.meta.defaultLanguage;
|
|
928
|
-
recipient.replacements = recipient.replacements.filter(r => !['firstName', 'lastName'].includes(r.token));
|
|
929
|
-
data.to.merge(recipient);
|
|
930
|
-
recipient = data.to;
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
// Create e-mail builder
|
|
934
|
-
await sendEmailTemplate(this.webshop.organization, {
|
|
935
|
-
recipients: [recipient],
|
|
936
|
-
template: {
|
|
937
|
-
type: data.type,
|
|
938
|
-
webshop: this.webshop,
|
|
939
|
-
},
|
|
940
|
-
type: 'transactional',
|
|
941
|
-
});
|
|
942
|
-
});
|
|
943
|
-
}
|
|
944
|
-
|
|
945
841
|
/**
|
|
946
842
|
* Get the transfer settings for this order
|
|
947
843
|
* @param this
|
|
@@ -961,103 +857,4 @@ export class Order extends QueryableModel {
|
|
|
961
857
|
|
|
962
858
|
return transferSettings;
|
|
963
859
|
}
|
|
964
|
-
|
|
965
|
-
/**
|
|
966
|
-
* WARNING: this should always run inside a queue so it only runs once for the same orde
|
|
967
|
-
* Include any tickets that are generated and should be included in the e-mail
|
|
968
|
-
*/
|
|
969
|
-
async markValid(this: Order & { webshop: Webshop & { organization: Organization } }, payment: Payment | null, tickets: Ticket[]) {
|
|
970
|
-
const webshop = this.webshop;
|
|
971
|
-
const organization = webshop.organization;
|
|
972
|
-
|
|
973
|
-
console.log('Marking as valid: order ' + this.id);
|
|
974
|
-
const wasValid = this.validAt !== null;
|
|
975
|
-
|
|
976
|
-
if (wasValid) {
|
|
977
|
-
console.warn('Warning: already validated an order');
|
|
978
|
-
return;
|
|
979
|
-
}
|
|
980
|
-
this.validAt = new Date(); // will get flattened AFTER calculations
|
|
981
|
-
this.validAt.setMilliseconds(0);
|
|
982
|
-
this.number = await WebshopCounter.getNextNumber(this.webshop);
|
|
983
|
-
|
|
984
|
-
if (payment && !Order.payment.isLoaded(this)) {
|
|
985
|
-
this.setRelation(Order.payment, payment);
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
// Now we have a number, update the payment
|
|
989
|
-
if (payment && payment.method === PaymentMethod.Transfer) {
|
|
990
|
-
// Only now we can update the transfer description, since we need the order number as a reference
|
|
991
|
-
payment.transferSettings = this.getTransferSettings({ shouldThrowIfNoIban: true });
|
|
992
|
-
payment.generateDescription(organization, this.number.toString(), this.getTransferReplacements());
|
|
993
|
-
await payment.save();
|
|
994
|
-
}
|
|
995
|
-
|
|
996
|
-
await this.save();
|
|
997
|
-
|
|
998
|
-
if (this.data.customer.email.length > 0) {
|
|
999
|
-
if (tickets.length > 0) {
|
|
1000
|
-
// Also send a copy
|
|
1001
|
-
if (payment && payment.method === PaymentMethod.PointOfSale) {
|
|
1002
|
-
await this.sendEmailTemplate({
|
|
1003
|
-
type: EmailTemplateType.TicketsConfirmationPOS,
|
|
1004
|
-
});
|
|
1005
|
-
} else {
|
|
1006
|
-
await this.sendEmailTemplate({
|
|
1007
|
-
type: EmailTemplateType.TicketsConfirmation,
|
|
1008
|
-
});
|
|
1009
|
-
}
|
|
1010
|
-
} else {
|
|
1011
|
-
if (this.webshop.meta.ticketType === WebshopTicketType.None) {
|
|
1012
|
-
if (payment && payment.method === PaymentMethod.Transfer) {
|
|
1013
|
-
// Also send a copy
|
|
1014
|
-
await this.sendEmailTemplate({
|
|
1015
|
-
type: EmailTemplateType.OrderConfirmationTransfer,
|
|
1016
|
-
});
|
|
1017
|
-
} else if (payment && payment.method === PaymentMethod.PointOfSale) {
|
|
1018
|
-
await this.sendEmailTemplate({
|
|
1019
|
-
type: EmailTemplateType.OrderConfirmationPOS,
|
|
1020
|
-
});
|
|
1021
|
-
} else {
|
|
1022
|
-
// Also send a copy
|
|
1023
|
-
await this.sendEmailTemplate({
|
|
1024
|
-
type: EmailTemplateType.OrderConfirmationOnline,
|
|
1025
|
-
});
|
|
1026
|
-
}
|
|
1027
|
-
} else {
|
|
1028
|
-
if (payment && payment.method === PaymentMethod.Transfer) {
|
|
1029
|
-
await this.sendEmailTemplate({
|
|
1030
|
-
type: EmailTemplateType.TicketsConfirmationTransfer,
|
|
1031
|
-
});
|
|
1032
|
-
} else {
|
|
1033
|
-
console.error('Unexpected missing tickets for order where tickets are expected');
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
if (this.webshop.privateMeta.notificationEmails) {
|
|
1040
|
-
const webshop = this.webshop;
|
|
1041
|
-
const organization = webshop.organization;
|
|
1042
|
-
const i18n = organization.i18n;
|
|
1043
|
-
|
|
1044
|
-
const webshopDashboardUrl = 'https://' + (STAMHOOFD.domains.dashboard ?? 'stamhoofd.app') + '/' + i18n.locale + '/webshops/' + Formatter.slug(webshop.meta.name) + '/orders';
|
|
1045
|
-
|
|
1046
|
-
// Send an email to all these notification emails
|
|
1047
|
-
for (const email of this.webshop.privateMeta.notificationEmails) {
|
|
1048
|
-
await this.sendEmailTemplate({
|
|
1049
|
-
type: EmailTemplateType.OrderNotification,
|
|
1050
|
-
to: Recipient.create({
|
|
1051
|
-
email,
|
|
1052
|
-
replacements: [
|
|
1053
|
-
Replacement.create({
|
|
1054
|
-
token: 'orderUrl',
|
|
1055
|
-
value: webshopDashboardUrl,
|
|
1056
|
-
}),
|
|
1057
|
-
],
|
|
1058
|
-
}),
|
|
1059
|
-
});
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
860
|
}
|