@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.
Files changed (157) hide show
  1. package/dist/helpers/Handlebars.d.ts.map +1 -1
  2. package/dist/helpers/Handlebars.js +1 -1
  3. package/dist/helpers/Handlebars.js.map +1 -1
  4. package/dist/helpers/errorToSimpleErrors.d.ts +3 -0
  5. package/dist/helpers/errorToSimpleErrors.d.ts.map +1 -0
  6. package/dist/helpers/errorToSimpleErrors.js +17 -0
  7. package/dist/helpers/errorToSimpleErrors.js.map +1 -0
  8. package/dist/index.d.ts +0 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +0 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/migrations/1782902314-tokens-authenticated-at.sql +2 -0
  13. package/dist/migrations/1782902315-create-mfa-totp-table.sql +13 -0
  14. package/dist/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
  15. package/dist/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
  16. package/dist/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
  17. package/dist/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
  18. package/dist/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
  19. package/dist/migrations/1784122354-webauthn-credential-provider.sql +3 -0
  20. package/dist/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
  21. package/dist/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
  22. package/dist/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
  23. package/dist/migrations/1785354093-platform-uri.sql +1 -0
  24. package/dist/migrations/1785354094-platform-domain.sql +1 -0
  25. package/dist/migrations/1785354095-platform-uri-unique.sql +1 -0
  26. package/dist/migrations/1785354096-platform-domain-unique.sql +1 -0
  27. package/dist/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
  28. package/dist/migrations/1785354098-platform-tenant-backfill.d.ts +4 -0
  29. package/dist/migrations/1785354098-platform-tenant-backfill.d.ts.map +1 -0
  30. package/dist/migrations/1785354098-platform-tenant-backfill.js +10 -0
  31. package/dist/migrations/1785354098-platform-tenant-backfill.js.map +1 -0
  32. package/dist/migrations/1785417301-users-last-active-at.sql +2 -0
  33. package/dist/models/Document.d.ts +0 -4
  34. package/dist/models/Document.d.ts.map +1 -1
  35. package/dist/models/Document.js +1 -75
  36. package/dist/models/Document.js.map +1 -1
  37. package/dist/models/DocumentTemplate.d.ts +0 -3
  38. package/dist/models/DocumentTemplate.d.ts.map +1 -1
  39. package/dist/models/DocumentTemplate.js +1 -63
  40. package/dist/models/DocumentTemplate.js.map +1 -1
  41. package/dist/models/Email.d.ts +1 -29
  42. package/dist/models/Email.d.ts.map +1 -1
  43. package/dist/models/Email.js +4 -823
  44. package/dist/models/Email.js.map +1 -1
  45. package/dist/models/EmailVerificationCode.d.ts +0 -5
  46. package/dist/models/EmailVerificationCode.d.ts.map +1 -1
  47. package/dist/models/EmailVerificationCode.js +0 -84
  48. package/dist/models/EmailVerificationCode.js.map +1 -1
  49. package/dist/models/Image.d.ts.map +1 -1
  50. package/dist/models/Image.js +11 -14
  51. package/dist/models/Image.js.map +1 -1
  52. package/dist/models/MFARecoveryCode.d.ts +17 -0
  53. package/dist/models/MFARecoveryCode.d.ts.map +1 -0
  54. package/dist/models/MFARecoveryCode.js +55 -0
  55. package/dist/models/MFARecoveryCode.js.map +1 -0
  56. package/dist/models/MFATOTP.d.ts +44 -0
  57. package/dist/models/MFATOTP.d.ts.map +1 -0
  58. package/dist/models/MFATOTP.js +116 -0
  59. package/dist/models/MFATOTP.js.map +1 -0
  60. package/dist/models/MFAToken.d.ts +49 -0
  61. package/dist/models/MFAToken.d.ts.map +1 -0
  62. package/dist/models/MFAToken.js +137 -0
  63. package/dist/models/MFAToken.js.map +1 -0
  64. package/dist/models/MemberPlatformMembership.d.ts +0 -8
  65. package/dist/models/MemberPlatformMembership.d.ts.map +1 -1
  66. package/dist/models/MemberPlatformMembership.js +1 -242
  67. package/dist/models/MemberPlatformMembership.js.map +1 -1
  68. package/dist/models/Order.d.ts +1 -32
  69. package/dist/models/Order.d.ts.map +1 -1
  70. package/dist/models/Order.js +1 -182
  71. package/dist/models/Order.js.map +1 -1
  72. package/dist/models/Organization.d.ts +1 -51
  73. package/dist/models/Organization.d.ts.map +1 -1
  74. package/dist/models/Organization.js +1 -530
  75. package/dist/models/Organization.js.map +1 -1
  76. package/dist/models/PasswordToken.d.ts +0 -8
  77. package/dist/models/PasswordToken.d.ts.map +1 -1
  78. package/dist/models/PasswordToken.js +0 -23
  79. package/dist/models/PasswordToken.js.map +1 -1
  80. package/dist/models/Platform.d.ts +28 -4
  81. package/dist/models/Platform.d.ts.map +1 -1
  82. package/dist/models/Platform.js +111 -50
  83. package/dist/models/Platform.js.map +1 -1
  84. package/dist/models/STPackage.d.ts +1 -6
  85. package/dist/models/STPackage.d.ts.map +1 -1
  86. package/dist/models/STPackage.js +1 -98
  87. package/dist/models/STPackage.js.map +1 -1
  88. package/dist/models/Token.d.ts +40 -1
  89. package/dist/models/Token.d.ts.map +1 -1
  90. package/dist/models/Token.js +68 -2
  91. package/dist/models/Token.js.map +1 -1
  92. package/dist/models/User.d.ts +25 -0
  93. package/dist/models/User.d.ts.map +1 -1
  94. package/dist/models/User.js +36 -0
  95. package/dist/models/User.js.map +1 -1
  96. package/dist/models/WebauthnChallenge.d.ts +27 -0
  97. package/dist/models/WebauthnChallenge.d.ts.map +1 -0
  98. package/dist/models/WebauthnChallenge.js +81 -0
  99. package/dist/models/WebauthnChallenge.js.map +1 -0
  100. package/dist/models/WebauthnCredential.d.ts +58 -0
  101. package/dist/models/WebauthnCredential.d.ts.map +1 -0
  102. package/dist/models/WebauthnCredential.js +144 -0
  103. package/dist/models/WebauthnCredential.js.map +1 -0
  104. package/dist/models/index.d.ts +5 -0
  105. package/dist/models/index.d.ts.map +1 -1
  106. package/dist/models/index.js +5 -0
  107. package/dist/models/index.js.map +1 -1
  108. package/package.json +3 -3
  109. package/src/helpers/Handlebars.ts +5 -9
  110. package/src/helpers/errorToSimpleErrors.ts +17 -0
  111. package/src/index.ts +0 -1
  112. package/src/migrations/1782902314-tokens-authenticated-at.sql +2 -0
  113. package/src/migrations/1782902315-create-mfa-totp-table.sql +13 -0
  114. package/src/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
  115. package/src/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
  116. package/src/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
  117. package/src/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
  118. package/src/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
  119. package/src/migrations/1784122354-webauthn-credential-provider.sql +3 -0
  120. package/src/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
  121. package/src/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
  122. package/src/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
  123. package/src/migrations/1785354093-platform-uri.sql +1 -0
  124. package/src/migrations/1785354094-platform-domain.sql +1 -0
  125. package/src/migrations/1785354095-platform-uri-unique.sql +1 -0
  126. package/src/migrations/1785354096-platform-domain-unique.sql +1 -0
  127. package/src/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
  128. package/src/migrations/1785354098-platform-tenant-backfill.ts +14 -0
  129. package/src/migrations/1785417301-users-last-active-at.sql +2 -0
  130. package/src/models/Document.ts +1 -84
  131. package/src/models/DocumentTemplate.ts +1 -71
  132. package/src/models/Email.ts +5 -941
  133. package/src/models/EmailVerificationCode.ts +0 -96
  134. package/src/models/Image.ts +11 -14
  135. package/src/models/MFARecoveryCode.ts +52 -0
  136. package/src/models/MFATOTP.ts +112 -0
  137. package/src/models/MFAToken.ts +138 -0
  138. package/src/models/MemberPlatformMembership.ts +1 -285
  139. package/src/models/Order.ts +1 -204
  140. package/src/models/Organization.ts +2 -637
  141. package/src/models/PasswordToken.ts +0 -29
  142. package/src/models/Platform.test.ts +167 -3
  143. package/src/models/Platform.ts +125 -50
  144. package/src/models/STPackage.ts +1 -110
  145. package/src/models/Token.ts +73 -2
  146. package/src/models/User.ts +37 -0
  147. package/src/models/WebauthnChallenge.ts +80 -0
  148. package/src/models/WebauthnCredential.ts +130 -0
  149. package/src/models/index.ts +6 -0
  150. package/dist/helpers/EmailBuilder.d.ts +0 -121
  151. package/dist/helpers/EmailBuilder.d.ts.map +0 -1
  152. package/dist/helpers/EmailBuilder.js +0 -659
  153. package/dist/helpers/EmailBuilder.js.map +0 -1
  154. package/src/helpers/EmailBuilder.test.ts +0 -291
  155. package/src/helpers/EmailBuilder.ts +0 -818
  156. package/src/models/Email.test.ts +0 -1222
  157. package/src/models/MemberPlatformMembership.test.ts +0 -70
@@ -1,13 +1,11 @@
1
1
  import { column } from '@simonbackx/simple-database';
2
- import { DocumentData, DocumentStatus, Document as DocumentStruct, Platform, Version } from '@stamhoofd/structures';
2
+ import { DocumentData, DocumentStatus, Document as DocumentStruct } from '@stamhoofd/structures';
3
3
  import { Formatter } from '@stamhoofd/utility';
4
4
  import { v4 as uuidv4 } from 'uuid';
5
5
 
6
6
  import { SimpleError } from '@simonbackx/simple-errors';
7
7
  import { QueryableModel } from '@stamhoofd/sql';
8
- import { render } from '../helpers/Handlebars.js';
9
8
  import type { Member, MemberWithRegistrations, RegistrationWithMember } from './Member.js';
10
- import type { Organization } from './Organization.js';
11
9
  import { Registration } from './Registration.js';
12
10
 
13
11
  export class Document extends QueryableModel {
@@ -83,64 +81,6 @@ export class Document extends QueryableModel {
83
81
  return DocumentStruct.create(this);
84
82
  }
85
83
 
86
- buildContext(organization: Organization) {
87
- // Convert the field answers in a simplified javascript object
88
- const data: Record<string, any> = {
89
- id: this.id,
90
- name: this.data.name,
91
- number: this.number,
92
- created_at: this.createdAt,
93
- organization: {
94
- name: organization.name,
95
- companyName: organization.meta.companies[0]?.name || organization.name,
96
- companyNumber: organization.meta.companies[0]?.companyNumber || null,
97
- address: organization.address,
98
- companyAddress: organization.meta.companies[0]?.address ?? organization.address,
99
- },
100
- };
101
- const platformLogo = Platform.shared.config.logoDocuments ?? Platform.shared.config.horizontalLogo ?? Platform.shared.config.squareLogo;
102
- const organizationLogo = organization.meta.horizontalLogo ?? organization.meta.squareLogo;
103
- const logo = organizationLogo || platformLogo;
104
-
105
- if (organizationLogo) {
106
- data['organization'] = {
107
- ...data['organization'],
108
- logo: organizationLogo.encode({ version: Version }) ?? null,
109
- };
110
- }
111
-
112
- if (platformLogo) {
113
- data['platform'] = {
114
- logo: platformLogo.encode({ version: Version }) ?? null,
115
- };
116
- }
117
-
118
- if (logo) {
119
- data['logo'] = logo.encode({ version: Version }) ?? null;
120
- }
121
-
122
- for (const field of this.data.fieldAnswers.values()) {
123
- const keys = field.settings.id.split('.');
124
- let current = data;
125
- const lastKey = keys.pop()!;
126
- if (!lastKey) {
127
- throw new Error('Invalid field id');
128
- }
129
- for (const key of keys) {
130
- if (!current[key]) {
131
- current[key] = {};
132
- }
133
- current = current[key];
134
-
135
- if (typeof current !== 'object') {
136
- throw new Error('Invalid field type');
137
- }
138
- }
139
- current[lastKey] = field.objectValue;
140
- }
141
- return data;
142
- }
143
-
144
84
  async updateData(): Promise<void> {
145
85
  if (this.isLocked) {
146
86
  console.log('Document is locked, skipping update');
@@ -267,27 +207,4 @@ export class Document extends QueryableModel {
267
207
  console.error(e);
268
208
  }
269
209
  }
270
-
271
- // Rander handlebars template
272
- async getRenderedHtml(organization: Organization): Promise<string | null> {
273
- const DocumentTemplate = (await import('./DocumentTemplate.js')).DocumentTemplate;
274
- const template = await DocumentTemplate.getByID(this.templateId);
275
- if (!template) {
276
- return null;
277
- }
278
-
279
- return await this.getRenderedHtmlForTemplate(organization, template.html);
280
- }
281
-
282
- // Rander handlebars template
283
- private async getRenderedHtmlForTemplate(organization: Organization, htmlTemplate: string): Promise<string | null> {
284
- try {
285
- const context = this.buildContext(organization);
286
- const renderedHtml = await render(htmlTemplate, context);
287
- return renderedHtml;
288
- } catch (e) {
289
- console.error('Failed to render document html', e);
290
- return null;
291
- }
292
- }
293
210
  }
@@ -1,5 +1,5 @@
1
1
  import { column } from '@simonbackx/simple-database';
2
- import { isSimpleError, isSimpleErrors, SimpleError } from '@simonbackx/simple-errors';
2
+ import { SimpleError } from '@simonbackx/simple-errors';
3
3
  import { QueueHandler } from '@stamhoofd/queues';
4
4
  import type { Parent, RecordAnswer } from '@stamhoofd/structures';
5
5
  import { BalanceItemStatus, DocumentData, DocumentPrivateSettings, DocumentSettings, DocumentStatus, DocumentTemplatePrivate, GroupType, NationalRegisterNumberOptOut, RecordAddressAnswer, RecordAnswerDecoder, RecordDateAnswer, RecordPriceAnswer, RecordSettings, RecordTextAnswer, RecordType } from '@stamhoofd/structures';
@@ -7,13 +7,11 @@ import { Formatter, Sorter } from '@stamhoofd/utility';
7
7
  import { v4 as uuidv4 } from 'uuid';
8
8
 
9
9
  import { QueryableModel } from '@stamhoofd/sql';
10
- import { render } from '../helpers/Handlebars.js';
11
10
  import { BalanceItem } from './BalanceItem.js';
12
11
  import { Document } from './Document.js';
13
12
  import { Group } from './Group.js';
14
13
  import type { RegistrationWithMember } from './Member.js';
15
14
  import { Member } from './Member.js';
16
- import type { Organization } from './Organization.js';
17
15
  import { Registration } from './Registration.js';
18
16
  import { User } from './User.js';
19
17
 
@@ -717,74 +715,6 @@ export class DocumentTemplate extends QueryableModel {
717
715
  return lastNumber + 1;
718
716
  }
719
717
 
720
- private async buildContext(organization: Organization) {
721
- // Convert the field answers in a simplified javascript object
722
- const documents = (await this.buildAll({ generateNumbers: true })).filter(d => d.status === DocumentStatus.Published && !!d.number).sort((a, b) => Sorter.byNumberValue(b.number ?? 0, a.number ?? 0));
723
-
724
- // Check numbers are strictly increasing
725
- let lastNumber = 0;
726
- for (const document of documents) {
727
- if (document.number !== lastNumber + 1) {
728
- document.number = lastNumber + 1;
729
- await document.save();
730
- }
731
- lastNumber = document.number;
732
- }
733
-
734
- const data = {
735
- id: this.id,
736
- created_at: this.createdAt,
737
- documents: documents.map(d => d.buildContext(organization)),
738
- organization: {
739
- name: organization.name,
740
- companyName: organization.meta.companies[0]?.name || organization.name,
741
- companyNumber: organization.meta.companies[0]?.companyNumber || null,
742
- address: organization.address,
743
- companyAddress: organization.meta.companies[0]?.address ?? organization.address,
744
- },
745
- };
746
-
747
- for (const field of this.settings.fieldAnswers.values()) {
748
- const keys = field.settings.id.split('.');
749
- let current = data;
750
- const lastKey = keys.pop()!;
751
- if (!lastKey) {
752
- throw new Error('Invalid field id');
753
- }
754
- for (const key of keys) {
755
- if (!current[key]) {
756
- current[key] = {};
757
- }
758
- current = current[key];
759
-
760
- if (typeof current !== 'object') {
761
- throw new Error('Invalid field type');
762
- }
763
- }
764
- current[lastKey] = field.objectValue;
765
- }
766
-
767
- return data;
768
- }
769
-
770
- async getRenderedXml(organization: Organization) {
771
- if (!this.privateSettings.templateDefinition.xmlExport) {
772
- return null;
773
- }
774
-
775
- try {
776
- const context = await this.buildContext(organization);
777
- const renderedHtml = await render(this.privateSettings.templateDefinition.xmlExport, context);
778
- return renderedHtml;
779
- } catch (e) {
780
- if (isSimpleError(e) || isSimpleErrors(e)) {
781
- throw e;
782
- }
783
- console.error('Failed to render document html', e);
784
- return null;
785
- }
786
- }
787
-
788
718
  areAnswersComplete(answers: Map<string, RecordAnswer>) {
789
719
  for (const field of this.privateSettings.templateDefinition.documentFieldCategories.flatMap(c => c.getAllRecords())) {
790
720
  const answer = answers.get(field.id);