@bisondesk/documents-sdk 1.0.396 → 1.0.398

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": "@bisondesk/documents-sdk",
3
- "version": "1.0.396",
3
+ "version": "1.0.398",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -15,7 +15,9 @@
15
15
  "@bisondesk/commons-sdk": "*",
16
16
  "@bisondesk/core-sdk": "*",
17
17
  "decimal.js": "10.4.3",
18
- "joi": "17.4.0",
18
+ "joi": "17.4.0"
19
+ },
20
+ "peerDependencies": {
19
21
  "node-fetch": "3.3.2"
20
22
  },
21
23
  "devDependencies": {
@@ -3,7 +3,7 @@ import { XError } from '@bisondesk/commons-sdk/errors';
3
3
  import { cleanHeaders, getAdminAuth } from '@bisondesk/commons-sdk/fetch';
4
4
  import { DataRecord, ReferenceData } from '@bisondesk/core-sdk/types/utils';
5
5
  import fetch, { Response } from 'node-fetch';
6
- import { FinanceBooking, FinanceBookingPaymentUpdate } from '../types/booking.js';
6
+ import { FinanceBooking } from '../types/booking.js';
7
7
  import { AdministrativeDocument, FinanceDocument } from '../types/documents.js';
8
8
 
9
9
  export const getFinanceDocument = async (
@@ -206,38 +206,3 @@ export const upsertFinanceBooking = async (
206
206
  status: response.status,
207
207
  });
208
208
  };
209
-
210
- export const updateFinanceBookingPayment = async (
211
- tenantId: string,
212
- update: FinanceBookingPaymentUpdate
213
- ): Promise<FinanceBooking | undefined> => {
214
- const auth = await getAdminAuth();
215
- const response: Response = await fetch(
216
- `${process.env.CORE_API_ORIGIN}/api/documents/finance/booking/payment`,
217
- {
218
- method: 'PUT',
219
- headers: {
220
- 'Content-Type': 'application/json',
221
- Authorization: auth,
222
- [TENANT_ID_ADMIN_HEADER]: tenantId,
223
- },
224
- body: JSON.stringify(update),
225
- }
226
- );
227
-
228
- if (response.status === 204) {
229
- return;
230
- }
231
-
232
- if (response.status === 200) {
233
- return response.json() as any;
234
- }
235
-
236
- const body = await response.text();
237
- throw new XError('documents-sdk.finance-booking-payment-fail', {
238
- tenantId,
239
- update,
240
- response: body,
241
- status: response.status,
242
- });
243
- };
package/src/constants.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { AppRoles } from '@bisondesk/commons-sdk/constants';
1
+ import { AppRoles, SYSTEM_USER_ID } from '@bisondesk/commons-sdk/constants';
2
+ import { AttachmentValue } from '@bisondesk/commons-sdk/types';
3
+ import { getPdfPreviewSrc } from './utils/previews.js';
2
4
 
3
5
  export const DOCUMENT_PROCESSING_SETTINGS_ID = 'document-processing';
4
6
 
@@ -38,3 +40,14 @@ export const ENV_CONTRIBUTION_TYRES_LINE_CATEGORY_ID = 'env_contribution_tyres';
38
40
  export const STANDARD_VAT_CODE_ID = 'standard';
39
41
  export const SALE_EU_INTRA_COMMUNITY_VAT_CODE_ID = 'eu_ics_sale';
40
42
  export const SALE_EU_EXTRA_COMMUNITY_VAT_CODE_ID = 'eu_ecs_sale';
43
+
44
+ export const GENERATING_PLACEHOLDER_ATTACHMENT: AttachmentValue = {
45
+ id: 'generating',
46
+ contentType: 'application/pdf',
47
+ fileName: 'generating.pdf',
48
+ uploadedBy: SYSTEM_USER_ID,
49
+ uploadedAt: '2024-04-08T14:51:55.785Z',
50
+ size: 95749,
51
+ url: '/storage/public/generating.pdf',
52
+ previewSrc: getPdfPreviewSrc('/storage/public/generating.pdf'),
53
+ };
package/src/schemas.ts CHANGED
@@ -34,7 +34,7 @@ const associationsSchema = Joi.array().items(
34
34
  recordId: Joi.string().required(),
35
35
  title: Joi.string().required(),
36
36
  description: Joi.string().required(),
37
- tags: Joi.array().items(Joi.string()),
37
+ tags: Joi.array().items(Joi.string().empty('')),
38
38
  })
39
39
  );
40
40
 
@@ -49,14 +49,14 @@ export const lineSchema = Joi.object({
49
49
  unitPriceExcl: Joi.number().cast('string').required(),
50
50
  vatAmount: Joi.number().cast('string').required(),
51
51
  }),
52
- invoiceLineId: Joi.string().uuid(),
52
+ invoiceLineId: Joi.string().empty('').uuid(),
53
53
  quantity: Joi.number().min(0).cast('string').required(),
54
54
  associations: associationsSchema,
55
55
  unitPriceExcl: Joi.number().cast('string').required(),
56
56
  vatAmount: Joi.number().cast('string').required(),
57
57
  vatCode: Joi.string().required(),
58
58
  vatPercentage: Joi.number().min(0).cast('string').required(),
59
- vatReason: Joi.string(),
59
+ vatReason: Joi.string().empty(''),
60
60
  });
61
61
 
62
62
  export const newFinanceDocV2Schema = Joi.object({
@@ -71,15 +71,15 @@ export const newFinanceDocV2Schema = Joi.object({
71
71
  branchId: Joi.string().required(),
72
72
  category: Joi.string().required(),
73
73
  organizationSummary: orgSummarySchema.required(),
74
- transferDescription: Joi.string(),
74
+ transferDescription: Joi.string().empty(''),
75
75
  invoiceId: Joi.string().uuid().empty(''),
76
76
  notes: Joi.string().empty(''),
77
77
  salesperson: Joi.object({
78
78
  name: Joi.string().required(),
79
79
  username: Joi.string().required(),
80
80
  }),
81
- paidAt: Joi.string(),
82
- tags: Joi.array().items(Joi.string()),
81
+ paidAt: Joi.string().empty(''),
82
+ tags: Joi.array().items(Joi.string().empty('')),
83
83
  total: Joi.object({
84
84
  amountExcl: Joi.number().min(0).cast('string').required(),
85
85
  amount: Joi.number().min(0).cast('string').required(),
@@ -103,11 +103,11 @@ export const newFinanceDocV2Schema = Joi.object({
103
103
 
104
104
  export const newAdministrativedDocumentSchema = Joi.object({
105
105
  id: Joi.string(),
106
- displayName: Joi.string(),
106
+ displayName: Joi.string().empty(''),
107
107
  branchId: Joi.string().required(),
108
108
  attachment: attachmentValueSchema.required(),
109
109
  category: Joi.string().empty(''),
110
- tags: Joi.array().items(Joi.string()),
110
+ tags: Joi.array().items(Joi.string().empty('')),
111
111
  associations: associationsSchema,
112
112
  organizationSummary: orgSummarySchema,
113
113
  });
@@ -242,7 +242,7 @@ export type NewDocumentDraft<T = NewFinanceDocumentV2 | NewAdministrativeDocumen
242
242
  export const isFinanceDocumentV2 = (
243
243
  arg: FinanceDocumentV2 | FinanceDocumentV1
244
244
  ): arg is FinanceDocumentV2 => {
245
- return (arg as FinanceDocumentV2).version === 2;
245
+ return (arg as FinanceDocumentV2).version == 2;
246
246
  };
247
247
 
248
248
  export const isFinanceDocumentV1 = (
@@ -298,6 +298,12 @@ export type DocumentGenerateRequestEvent = {
298
298
  request: FinanceDocumentGenerateRequest;
299
299
  };
300
300
 
301
+ export type FinanceDocumentGeneratePDFEvent = {
302
+ tenantId: string;
303
+ doc: FinanceDocumentV2;
304
+ request: FinanceDocumentGenerateRequest;
305
+ };
306
+
301
307
  export type FinanceDocumentSettings<M = {}> = {
302
308
  provider: string;
303
309
  meta: {
@@ -1,16 +1,38 @@
1
- import { Association, FinanceDocumentSubtype } from './documents.js';
1
+ import { Association, FinanceDocument, FinanceDocumentSubtype } from './documents.js';
2
2
 
3
- export type FinanceDocumentEvent = {
3
+ type BaseFinanceDocumentEvent = {
4
4
  id: string;
5
5
  actionAt: string;
6
- action: 'create' | 'update' | 'delete';
7
6
  userId: string;
8
7
  tenantId: string;
9
8
  type: 'Sale Finance' | 'Purchase Finance';
10
9
  subtype: 'Invoice' | 'Credit Note' | 'Proforma' | 'Purchase Order' | FinanceDocumentSubtype;
11
- version?: string;
10
+ version?: FinanceDocument['version'];
12
11
  };
13
12
 
13
+ export type FinanceDocumentCreateEvent = BaseFinanceDocumentEvent & {
14
+ action: 'create';
15
+ document: FinanceDocument;
16
+ previousDocument?: undefined;
17
+ };
18
+
19
+ export type FinanceDocumentUpdateEvent = BaseFinanceDocumentEvent & {
20
+ action: 'update';
21
+ document: FinanceDocument;
22
+ previousDocument: FinanceDocument;
23
+ };
24
+
25
+ export type FinanceDocumentDeleteEvent = BaseFinanceDocumentEvent & {
26
+ action: 'delete';
27
+ document?: undefined;
28
+ previousDocument: FinanceDocument;
29
+ };
30
+
31
+ export type FinanceDocumentEvent =
32
+ | FinanceDocumentCreateEvent
33
+ | FinanceDocumentUpdateEvent
34
+ | FinanceDocumentDeleteEvent;
35
+
14
36
  export type AdminDocumentEvent = {
15
37
  id: string;
16
38
  actionAt: string;
@@ -40,6 +40,12 @@ export type FinanceDocumentV1 = {
40
40
  username: string;
41
41
  };
42
42
 
43
+ total: {
44
+ amountExcl: string;
45
+ amount: string;
46
+ vatAmount: string;
47
+ };
48
+
43
49
  organizationSummary?: {
44
50
  id: string;
45
51
  code: string;
@@ -2,6 +2,7 @@ import { BusinessEntityIds } from '@bisondesk/commons-sdk/constants';
2
2
  import { roundMoney } from '@bisondesk/commons-sdk/money';
3
3
  import { Decimal } from 'decimal.js';
4
4
  import { uniqBy } from 'lodash-es';
5
+ import { GENERATING_PLACEHOLDER_ATTACHMENT } from '../constants.js';
5
6
  import {
6
7
  Association,
7
8
  DocumentDraft,
@@ -33,7 +34,15 @@ export const getAccountingDescription = (doc: FinanceDocumentV2): string => {
33
34
  ?.flatMap((line) => line.associations ?? [])
34
35
  .filter(Boolean) as Association[];
35
36
 
36
- const uniqueAssociations = uniqBy(associations, (a) => `${a.businessEntityId}_${a.recordId}`);
37
+ // In the UI the user can only select one vehicle associations
38
+ const equivalentEntityIds = {
39
+ [BusinessEntityIds.ExternalVehicleSale]: BusinessEntityIds.Vehicles,
40
+ };
41
+
42
+ const uniqueAssociations = uniqBy(
43
+ associations,
44
+ (a) => `${equivalentEntityIds[a.businessEntityId] ?? a.businessEntityId}_${a.recordId}`
45
+ );
37
46
 
38
47
  if (uniqueAssociations.length !== 1) {
39
48
  return doc.reference;
@@ -45,7 +54,8 @@ export const getAccountingDescription = (doc: FinanceDocumentV2): string => {
45
54
  BusinessEntityIds.Vehicles,
46
55
  BusinessEntityIds.Opportunities,
47
56
  BusinessEntityIds.ExternalVehicleSale,
48
- ].includes(firstAssociation.businessEntityId)
57
+ ].includes(firstAssociation.businessEntityId) &&
58
+ firstAssociation.tags != null
49
59
  ) {
50
60
  return `${firstAssociation.tags?.slice(0, 3).join(' ')} ${doc.reference}`.trim();
51
61
  }
@@ -160,3 +170,6 @@ export const getLineAmounts = (unitPriceExcl: string, quantity: string, vatRate:
160
170
  vatAmount: vatAmount.toString(),
161
171
  };
162
172
  };
173
+
174
+ export const isGeneratingAttachment = (doc: FinanceDocument) =>
175
+ doc.attachment.id === GENERATING_PLACEHOLDER_ATTACHMENT.id;