@hed-hog/finance 0.0.295 → 0.0.297

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.
@@ -123,13 +123,13 @@ export declare class FinanceDataController {
123
123
  codigo: string;
124
124
  nome: string;
125
125
  natureza: string;
126
- status: import(".prisma/client").$Enums.finance_category_status_enum;
126
+ status: import("@prisma/client").$Enums.finance_category_status_1c70701ad1_enum;
127
127
  }[];
128
128
  centrosCusto: {
129
129
  id: string;
130
130
  codigo: string;
131
131
  nome: string;
132
- status: import(".prisma/client").$Enums.cost_center_status_enum;
132
+ status: import("@prisma/client").$Enums.cost_center_status_9f38769d0c_enum;
133
133
  }[];
134
134
  aprovacoesPendentes: {
135
135
  id: string;
@@ -0,0 +1,11 @@
1
+ import { IntegrationDeveloperApiService } from '@hed-hog/core';
2
+ import { OnModuleInit } from '@nestjs/common';
3
+ import { FinanceService } from './finance.service';
4
+ export declare class FinanceIntegrationExampleSubscriber implements OnModuleInit {
5
+ private readonly integrationApi;
6
+ private readonly financeService;
7
+ private readonly logger;
8
+ constructor(integrationApi: IntegrationDeveloperApiService, financeService: FinanceService);
9
+ onModuleInit(): void;
10
+ }
11
+ //# sourceMappingURL=finance.integration-example.subscriber.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finance.integration-example.subscriber.d.ts","sourceRoot":"","sources":["../src/finance.integration-example.subscriber.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,8BAA8B,EAEjC,MAAM,eAAe,CAAC;AACvB,OAAO,EAAsB,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAmBnD,qBACa,mCAAoC,YAAW,YAAY;IAIpE,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,cAAc;IAJjC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwD;gBAG5D,cAAc,EAAE,8BAA8B,EAC9C,cAAc,EAAE,cAAc;IAGjD,YAAY,IAAI,IAAI;CAwFrB"}
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var FinanceIntegrationExampleSubscriber_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.FinanceIntegrationExampleSubscriber = void 0;
14
+ const core_1 = require("@hed-hog/core");
15
+ const common_1 = require("@nestjs/common");
16
+ const finance_service_1 = require("./finance.service");
17
+ let FinanceIntegrationExampleSubscriber = FinanceIntegrationExampleSubscriber_1 = class FinanceIntegrationExampleSubscriber {
18
+ constructor(integrationApi, financeService) {
19
+ this.integrationApi = integrationApi;
20
+ this.financeService = financeService;
21
+ this.logger = new common_1.Logger(FinanceIntegrationExampleSubscriber_1.name);
22
+ }
23
+ onModuleInit() {
24
+ this.integrationApi.subscribe({
25
+ eventName: 'operations.accounts_payable.requested',
26
+ consumerName: 'finance.accounts-payable-reference',
27
+ priority: 0,
28
+ handler: async (event) => {
29
+ var _a, _b, _c, _d;
30
+ const payload = (event.payload || {});
31
+ const sourceEntityType = String(payload.sourceEntityType || event.aggregateType || '').trim() ||
32
+ 'operations_payable_request';
33
+ const sourceEntityId = String(payload.sourceEntityId || event.aggregateId || '').trim() ||
34
+ event.aggregateId;
35
+ const existingLinks = await this.integrationApi.findLinksBySource({
36
+ module: event.sourceModule,
37
+ entityType: sourceEntityType,
38
+ entityId: sourceEntityId,
39
+ });
40
+ const alreadyLinked = existingLinks.some((link) => link.targetModule === 'finance' &&
41
+ link.targetEntityType === 'financial_title');
42
+ if (alreadyLinked) {
43
+ this.logger.debug(`Skipping duplicate payable integration for ${sourceEntityType}:${sourceEntityId}`);
44
+ return;
45
+ }
46
+ const payable = payload.payable || {};
47
+ const personId = Number(payable.personId);
48
+ const totalAmount = Number(payable.totalAmount);
49
+ const dueDate = String(payable.dueDate || '').trim();
50
+ const documentNumber = String(payable.documentNumber || '').trim();
51
+ const locale = String(payload.locale || '').trim() || 'en';
52
+ if (!Number.isInteger(personId) || personId <= 0) {
53
+ throw new Error('Invalid payable.personId for integration event.');
54
+ }
55
+ if (!Number.isFinite(totalAmount) || totalAmount <= 0) {
56
+ throw new Error('Invalid payable.totalAmount for integration event.');
57
+ }
58
+ if (!dueDate || Number.isNaN(new Date(dueDate).getTime())) {
59
+ throw new Error('Invalid payable.dueDate for integration event.');
60
+ }
61
+ if (!documentNumber) {
62
+ throw new Error('payable.documentNumber is required for integration event.');
63
+ }
64
+ const createdTitle = await this.financeService.createAccountsPayableTitle({
65
+ person_id: personId,
66
+ due_date: dueDate,
67
+ total_amount: totalAmount,
68
+ document_number: documentNumber,
69
+ description: (_a = payable.description) !== null && _a !== void 0 ? _a : undefined,
70
+ payment_channel: (_b = payable.paymentChannel) !== null && _b !== void 0 ? _b : undefined,
71
+ finance_category_id: (_c = payable.financeCategoryId) !== null && _c !== void 0 ? _c : undefined,
72
+ cost_center_id: (_d = payable.costCenterId) !== null && _d !== void 0 ? _d : undefined,
73
+ }, locale, Number(payload.requestedByUserId) || undefined);
74
+ const targetEntityId = String((createdTitle === null || createdTitle === void 0 ? void 0 : createdTitle.id) || '').trim();
75
+ if (!targetEntityId) {
76
+ throw new Error('Could not resolve target financial title id.');
77
+ }
78
+ await this.integrationApi.createLink({
79
+ sourceModule: event.sourceModule,
80
+ sourceEntityType,
81
+ sourceEntityId,
82
+ targetModule: 'finance',
83
+ targetEntityType: 'financial_title',
84
+ targetEntityId,
85
+ linkType: core_1.LinkType.REFERENCE,
86
+ metadata: {
87
+ eventName: event.eventName,
88
+ },
89
+ });
90
+ },
91
+ });
92
+ }
93
+ };
94
+ exports.FinanceIntegrationExampleSubscriber = FinanceIntegrationExampleSubscriber;
95
+ exports.FinanceIntegrationExampleSubscriber = FinanceIntegrationExampleSubscriber = FinanceIntegrationExampleSubscriber_1 = __decorate([
96
+ (0, common_1.Injectable)(),
97
+ __metadata("design:paramtypes", [core_1.IntegrationDeveloperApiService,
98
+ finance_service_1.FinanceService])
99
+ ], FinanceIntegrationExampleSubscriber);
100
+ //# sourceMappingURL=finance.integration-example.subscriber.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finance.integration-example.subscriber.js","sourceRoot":"","sources":["../src/finance.integration-example.subscriber.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,wCAGuB;AACvB,2CAAkE;AAClE,uDAAmD;AAoB5C,IAAM,mCAAmC,2CAAzC,MAAM,mCAAmC;IAG9C,YACmB,cAA8C,EAC9C,cAA8B;QAD9B,mBAAc,GAAd,cAAc,CAAgC;QAC9C,mBAAc,GAAd,cAAc,CAAgB;QAJhC,WAAM,GAAG,IAAI,eAAM,CAAC,qCAAmC,CAAC,IAAI,CAAC,CAAC;IAK5E,CAAC;IAEJ,YAAY;QACV,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC;YAC5B,SAAS,EAAE,uCAAuC;YAClD,YAAY,EAAE,oCAAoC;YAClD,QAAQ,EAAE,CAAC;YACX,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;;gBACvB,MAAM,OAAO,GAAG,CAAC,KAAK,CAAC,OAAO,IAAI,EAAE,CAAgC,CAAC;gBACrE,MAAM,gBAAgB,GACpB,MAAM,CAAC,OAAO,CAAC,gBAAgB,IAAI,KAAK,CAAC,aAAa,IAAI,EAAE,CAAC,CAAC,IA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AK,CAAC,8CAA8C,CAAC,CAAC;gBAClE,CAAC;gBAED,MAAM,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC;oBACnC,YAAY,EAAE,KAAK,CAAC,YAAY;oBAChC,gBAAgB;oBAChB,cAAc;oBACd,YAAY,EAAE,SAAS;oBACvB,gBAAgB,EAAE,iBAAiB;oBACnC,cAAc;oBACd,QAAQ,EAAE,eAAQ,CAAC,SAAS;oBAC5B,QAAQ,EAAE;wBACR,SAAS,EAAE,KAAK,CAAC,SAAS;qBAC3B;iBACF,CAAC,CAAC;YACL,CAAC;SACF,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAhGY,kFAAmC;8CAAnC,mCAAmC;IAD/C,IAAA,mBAAU,GAAE;qCAKwB,qCAA8B;QAC9B,gCAAc;GALtC,mCAAmC,CAgG/C"}
@@ -1 +1 @@
1
- {"version":3,"file":"finance.module.d.ts","sourceRoot":"","sources":["../src/finance.module.ts"],"names":[],"mappings":"AAmBA,qBAyBa,aAAa;CAAG"}
1
+ {"version":3,"file":"finance.module.d.ts","sourceRoot":"","sources":["../src/finance.module.ts"],"names":[],"mappings":"AAoBA,qBA0Ba,aAAa;CAAG"}
@@ -24,6 +24,7 @@ const finance_period_close_controller_1 = require("./finance-period-close.contro
24
24
  const finance_reports_controller_1 = require("./finance-reports.controller");
25
25
  const finance_statements_controller_1 = require("./finance-statements.controller");
26
26
  const finance_transfers_controller_1 = require("./finance-transfers.controller");
27
+ const finance_integration_example_subscriber_1 = require("./finance.integration-example.subscriber");
27
28
  const finance_service_1 = require("./finance.service");
28
29
  let FinanceModule = class FinanceModule {
29
30
  };
@@ -37,6 +38,7 @@ exports.FinanceModule = FinanceModule = __decorate([
37
38
  (0, common_1.forwardRef)(() => api_locale_1.LocaleModule),
38
39
  (0, common_1.forwardRef)(() => core_1.AiModule),
39
40
  (0, common_1.forwardRef)(() => core_1.FileModule),
41
+ (0, common_1.forwardRef)(() => core_1.IntegrationModule),
40
42
  ],
41
43
  controllers: [
42
44
  finance_audit_logs_controller_1.FinanceAuditLogsController,
@@ -51,7 +53,7 @@ exports.FinanceModule = FinanceModule = __decorate([
51
53
  finance_statements_controller_1.FinanceStatementsController,
52
54
  finance_transfers_controller_1.FinanceTransfersController,
53
55
  ],
54
- providers: [finance_service_1.FinanceService],
56
+ providers: [finance_service_1.FinanceService, finance_integration_example_subscriber_1.FinanceIntegrationExampleSubscriber],
55
57
  exports: [finance_service_1.FinanceService],
56
58
  })
57
59
  ], FinanceModule);
@@ -1 +1 @@
1
- {"version":3,"file":"finance.module.js","sourceRoot":"","sources":["../src/finance.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,oDAAmD;AACnD,4DAA2D;AAC3D,oDAAmD;AACnD,wCAAqD;AACrD,2CAAoD;AACpD,2CAA8C;AAC9C,mFAA6E;AAC7E,yFAAmF;AACnF,mFAA8E;AAC9E,qFAAgF;AAChF,uFAAiF;AACjF,uEAAkE;AAClE,uFAAkF;AAClF,uFAAiF;AACjF,6EAAwE;AACxE,mFAA8E;AAC9E,iFAA4E;AAC5E,uDAAmD;AA2B5C,IAAM,aAAa,GAAnB,MAAM,aAAa;CAAG,CAAA;AAAhB,sCAAa;wBAAb,aAAa;IAzBzB,IAAA,eAAM,EAAC;QACN,OAAO,EAAE;YACP,qBAAY,CAAC,OAAO,EAAE;YACtB,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,iCAAgB,CAAC;YAClC,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,yBAAY,CAAC;YAC9B,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,yBAAY,CAAC;YAC9B,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,eAAQ,CAAC;YAC1B,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,iBAAU,CAAC;SAC7B;QACD,WAAW,EAAE;YACX,0DAA0B;YAC1B,+CAAqB;YACrB,+DAA6B;YAC7B,qDAAwB;YACxB,gEAA6B;YAC7B,2DAA2B;YAC3B,6DAA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AC3B;QACD,SAAS,EAAE,CAAC,gCAAc,EAAE,4EAAmC,CAAC;QAChE,OAAO,EAAE,CAAC,gCAAc,CAAC;KAC1B,CAAC;GACW,aAAa,CAAG"}
@@ -197,13 +197,13 @@ export declare class FinanceService {
197
197
  codigo: string;
198
198
  nome: string;
199
199
  natureza: string;
200
- status: import(".prisma/client").$Enums.finance_category_status_enum;
200
+ status: import("@prisma/client").$Enums.finance_category_status_1c70701ad1_enum;
201
201
  }[];
202
202
  centrosCusto: {
203
203
  id: string;
204
204
  codigo: string;
205
205
  nome: string;
206
- status: import(".prisma/client").$Enums.cost_center_status_enum;
206
+ status: import("@prisma/client").$Enums.cost_center_status_9f38769d0c_enum;
207
207
  }[];
208
208
  aprovacoesPendentes: {
209
209
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hed-hog/finance",
3
- "version": "0.0.295",
3
+ "version": "0.0.297",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "dependencies": {
@@ -9,14 +9,14 @@
9
9
  "@nestjs/core": "^11",
10
10
  "@nestjs/jwt": "^11",
11
11
  "@nestjs/mapped-types": "*",
12
- "@hed-hog/api": "0.0.4",
13
- "@hed-hog/api-locale": "0.0.13",
14
- "@hed-hog/contact": "0.0.295",
15
- "@hed-hog/api-prisma": "0.0.5",
12
+ "@hed-hog/api": "0.0.6",
13
+ "@hed-hog/contact": "0.0.297",
14
+ "@hed-hog/tag": "0.0.297",
15
+ "@hed-hog/api-prisma": "0.0.6",
16
+ "@hed-hog/api-locale": "0.0.14",
17
+ "@hed-hog/core": "0.0.297",
16
18
  "@hed-hog/api-types": "0.0.1",
17
- "@hed-hog/api-pagination": "0.0.6",
18
- "@hed-hog/core": "0.0.295",
19
- "@hed-hog/tag": "0.0.295"
19
+ "@hed-hog/api-pagination": "0.0.7"
20
20
  },
21
21
  "exports": {
22
22
  ".": {
@@ -32,7 +32,7 @@
32
32
  ],
33
33
  "scripts": {
34
34
  "lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
35
- "prebuild": "pnpm exec ts-node ../../scripts/build-dependencies.ts libraries/finance",
35
+ "prebuild": "pnpm --dir ../.. exec ts-node ./scripts/build-dependencies.ts libraries/finance",
36
36
  "build": "tsc --project tsconfig.production.json",
37
37
  "patch": "pnpm exec ts-node ../../scripts/patch.ts libraries/finance",
38
38
  "prod": "pnpm run patch && pnpm run build && pnpm publish --access public --no-git-checks"
@@ -0,0 +1,122 @@
1
+ import {
2
+ IntegrationDeveloperApiService,
3
+ LinkType,
4
+ } from '@hed-hog/core';
5
+ import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
6
+ import { FinanceService } from './finance.service';
7
+
8
+ type AccountsPayableEventPayload = {
9
+ sourceEntityType?: string;
10
+ sourceEntityId?: string;
11
+ locale?: string;
12
+ requestedByUserId?: number;
13
+ payable?: {
14
+ personId?: number;
15
+ dueDate?: string;
16
+ totalAmount?: number;
17
+ documentNumber?: string;
18
+ description?: string | null;
19
+ paymentChannel?: string | null;
20
+ financeCategoryId?: number | null;
21
+ costCenterId?: number | null;
22
+ };
23
+ };
24
+
25
+ @Injectable()
26
+ export class FinanceIntegrationExampleSubscriber implements OnModuleInit {
27
+ private readonly logger = new Logger(FinanceIntegrationExampleSubscriber.name);
28
+
29
+ constructor(
30
+ private readonly integrationApi: IntegrationDeveloperApiService,
31
+ private readonly financeService: FinanceService,
32
+ ) {}
33
+
34
+ onModuleInit(): void {
35
+ this.integrationApi.subscribe({
36
+ eventName: 'operations.accounts_payable.requested',
37
+ consumerName: 'finance.accounts-payable-reference',
38
+ priority: 0,
39
+ handler: async (event) => {
40
+ const payload = (event.payload || {}) as AccountsPayableEventPayload;
41
+ const sourceEntityType =
42
+ String(payload.sourceEntityType || event.aggregateType || '').trim() ||
43
+ 'operations_payable_request';
44
+ const sourceEntityId =
45
+ String(payload.sourceEntityId || event.aggregateId || '').trim() ||
46
+ event.aggregateId;
47
+
48
+ const existingLinks = await this.integrationApi.findLinksBySource({
49
+ module: event.sourceModule,
50
+ entityType: sourceEntityType,
51
+ entityId: sourceEntityId,
52
+ });
53
+
54
+ const alreadyLinked = existingLinks.some(
55
+ (link) =>
56
+ link.targetModule === 'finance' &&
57
+ link.targetEntityType === 'financial_title',
58
+ );
59
+
60
+ if (alreadyLinked) {
61
+ this.logger.debug(
62
+ `Skipping duplicate payable integration for ${sourceEntityType}:${sourceEntityId}`,
63
+ );
64
+ return;
65
+ }
66
+
67
+ const payable = payload.payable || {};
68
+ const personId = Number(payable.personId);
69
+ const totalAmount = Number(payable.totalAmount);
70
+ const dueDate = String(payable.dueDate || '').trim();
71
+ const documentNumber = String(payable.documentNumber || '').trim();
72
+ const locale = String(payload.locale || '').trim() || 'en';
73
+
74
+ if (!Number.isInteger(personId) || personId <= 0) {
75
+ throw new Error('Invalid payable.personId for integration event.');
76
+ }
77
+ if (!Number.isFinite(totalAmount) || totalAmount <= 0) {
78
+ throw new Error('Invalid payable.totalAmount for integration event.');
79
+ }
80
+ if (!dueDate || Number.isNaN(new Date(dueDate).getTime())) {
81
+ throw new Error('Invalid payable.dueDate for integration event.');
82
+ }
83
+ if (!documentNumber) {
84
+ throw new Error('payable.documentNumber is required for integration event.');
85
+ }
86
+
87
+ const createdTitle = await this.financeService.createAccountsPayableTitle(
88
+ {
89
+ person_id: personId,
90
+ due_date: dueDate,
91
+ total_amount: totalAmount,
92
+ document_number: documentNumber,
93
+ description: payable.description ?? undefined,
94
+ payment_channel: payable.paymentChannel ?? undefined,
95
+ finance_category_id: payable.financeCategoryId ?? undefined,
96
+ cost_center_id: payable.costCenterId ?? undefined,
97
+ },
98
+ locale,
99
+ Number(payload.requestedByUserId) || undefined,
100
+ );
101
+
102
+ const targetEntityId = String(createdTitle?.id || '').trim();
103
+ if (!targetEntityId) {
104
+ throw new Error('Could not resolve target financial title id.');
105
+ }
106
+
107
+ await this.integrationApi.createLink({
108
+ sourceModule: event.sourceModule,
109
+ sourceEntityType,
110
+ sourceEntityId,
111
+ targetModule: 'finance',
112
+ targetEntityType: 'financial_title',
113
+ targetEntityId,
114
+ linkType: LinkType.REFERENCE,
115
+ metadata: {
116
+ eventName: event.eventName,
117
+ },
118
+ });
119
+ },
120
+ });
121
+ }
122
+ }
@@ -1,7 +1,7 @@
1
1
  import { LocaleModule } from '@hed-hog/api-locale';
2
2
  import { PaginationModule } from '@hed-hog/api-pagination';
3
3
  import { PrismaModule } from '@hed-hog/api-prisma';
4
- import { AiModule, FileModule } from '@hed-hog/core';
4
+ import { AiModule, FileModule, IntegrationModule } from '@hed-hog/core';
5
5
  import { forwardRef, Module } from '@nestjs/common';
6
6
  import { ConfigModule } from '@nestjs/config';
7
7
  import { FinanceAuditLogsController } from './finance-audit-logs.controller';
@@ -15,6 +15,7 @@ import { FinancePeriodCloseController } from './finance-period-close.controller'
15
15
  import { FinanceReportsController } from './finance-reports.controller';
16
16
  import { FinanceStatementsController } from './finance-statements.controller';
17
17
  import { FinanceTransfersController } from './finance-transfers.controller';
18
+ import { FinanceIntegrationExampleSubscriber } from './finance.integration-example.subscriber';
18
19
  import { FinanceService } from './finance.service';
19
20
 
20
21
  @Module({
@@ -25,6 +26,7 @@ import { FinanceService } from './finance.service';
25
26
  forwardRef(() => LocaleModule),
26
27
  forwardRef(() => AiModule),
27
28
  forwardRef(() => FileModule),
29
+ forwardRef(() => IntegrationModule),
28
30
  ],
29
31
  controllers: [
30
32
  FinanceAuditLogsController,
@@ -39,7 +41,7 @@ import { FinanceService } from './finance.service';
39
41
  FinanceStatementsController,
40
42
  FinanceTransfersController,
41
43
  ],
42
- providers: [FinanceService],
44
+ providers: [FinanceService, FinanceIntegrationExampleSubscriber],
43
45
  exports: [FinanceService],
44
46
  })
45
47
  export class FinanceModule {}