@vulog/aima-billing 1.1.74 → 1.1.75

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/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Client } from '@vulog/aima-client';
2
+ import { UUID } from 'crypto';
2
3
 
3
4
  type ChargeProductInfo = {
4
5
  productId: string;
@@ -14,7 +15,31 @@ type ChargeProductInfo = {
14
15
  type Invoice = {
15
16
  [key: string]: any;
16
17
  };
18
+ type Credit = {
19
+ initialAmount: number;
20
+ validityStartDate: string;
21
+ validityEndDate: string;
22
+ notes: string;
23
+ discountCategory: 'CREDITS' | 'PERCENTAGE';
24
+ oneTimeUsage: boolean;
25
+ id: UUID;
26
+ availableAmount: number;
27
+ usedAmount: number;
28
+ originId: UUID;
29
+ entityId: UUID;
30
+ creditAlreadyUsed: boolean;
31
+ updateDate: string;
32
+ type: 'LOCAL' | 'GLOBAL' | 'PERIODIC';
33
+ usage: 'TRIP' | 'REGISTRATION' | 'PRODUCTS' | 'ALL';
34
+ [key: string]: any;
35
+ };
17
36
 
18
37
  declare const chargeProduct: (client: Client, info: ChargeProductInfo) => Promise<Invoice>;
19
38
 
20
- export { type ChargeProductInfo, type Invoice, chargeProduct };
39
+ declare const getUserCreditsByEntityId: (client: Client, id: string) => Promise<Credit>;
40
+
41
+ interface Payload extends Omit<Credit, 'id' | 'availableAmount' | 'usedAmount' | 'originId' | 'creditAlreadyUsed' | 'updateDate'> {
42
+ }
43
+ declare const addCredits: (client: Client, payload: Payload) => Promise<Credit>;
44
+
45
+ export { type ChargeProductInfo, type Credit, type Invoice, addCredits, chargeProduct, getUserCreditsByEntityId };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Client } from '@vulog/aima-client';
2
+ import { UUID } from 'crypto';
2
3
 
3
4
  type ChargeProductInfo = {
4
5
  productId: string;
@@ -14,7 +15,31 @@ type ChargeProductInfo = {
14
15
  type Invoice = {
15
16
  [key: string]: any;
16
17
  };
18
+ type Credit = {
19
+ initialAmount: number;
20
+ validityStartDate: string;
21
+ validityEndDate: string;
22
+ notes: string;
23
+ discountCategory: 'CREDITS' | 'PERCENTAGE';
24
+ oneTimeUsage: boolean;
25
+ id: UUID;
26
+ availableAmount: number;
27
+ usedAmount: number;
28
+ originId: UUID;
29
+ entityId: UUID;
30
+ creditAlreadyUsed: boolean;
31
+ updateDate: string;
32
+ type: 'LOCAL' | 'GLOBAL' | 'PERIODIC';
33
+ usage: 'TRIP' | 'REGISTRATION' | 'PRODUCTS' | 'ALL';
34
+ [key: string]: any;
35
+ };
17
36
 
18
37
  declare const chargeProduct: (client: Client, info: ChargeProductInfo) => Promise<Invoice>;
19
38
 
20
- export { type ChargeProductInfo, type Invoice, chargeProduct };
39
+ declare const getUserCreditsByEntityId: (client: Client, id: string) => Promise<Credit>;
40
+
41
+ interface Payload extends Omit<Credit, 'id' | 'availableAmount' | 'usedAmount' | 'originId' | 'creditAlreadyUsed' | 'updateDate'> {
42
+ }
43
+ declare const addCredits: (client: Client, payload: Payload) => Promise<Credit>;
44
+
45
+ export { type ChargeProductInfo, type Credit, type Invoice, addCredits, chargeProduct, getUserCreditsByEntityId };
package/dist/index.js CHANGED
@@ -30,7 +30,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- chargeProduct: () => chargeProduct
33
+ addCredits: () => addCredits,
34
+ chargeProduct: () => chargeProduct,
35
+ getUserCreditsByEntityId: () => getUserCreditsByEntityId
34
36
  });
35
37
  module.exports = __toCommonJS(index_exports);
36
38
 
@@ -56,7 +58,47 @@ var chargeProduct = async (client, info) => {
56
58
  }
57
59
  return client.post(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/product`, info).then(({ data }) => data);
58
60
  };
61
+
62
+ // src/getUserCreditsByEntityId.ts
63
+ var import_zod2 = require("zod");
64
+ var getUserCreditsByEntityId = async (client, id) => {
65
+ const result = import_zod2.z.string().trim().min(1).uuid().safeParse(id);
66
+ if (!result.success) {
67
+ throw new TypeError("Invalid id", {
68
+ cause: result.error.issues
69
+ });
70
+ }
71
+ return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${id}`).then(({ data }) => data);
72
+ };
73
+
74
+ // src/addCredits.ts
75
+ var import_zod3 = __toESM(require("zod"));
76
+ var schema2 = import_zod3.default.object({
77
+ initialAmount: import_zod3.default.number().min(0),
78
+ validityStartDate: import_zod3.default.string().datetime(),
79
+ validityEndDate: import_zod3.default.string().datetime(),
80
+ notes: import_zod3.default.string().trim().nullable().optional(),
81
+ discountCategory: import_zod3.default.enum(["CREDITS", "PERCENTAGE"]),
82
+ oneTimeUsage: import_zod3.default.boolean().optional(),
83
+ entityId: import_zod3.default.string().trim().min(1).uuid(),
84
+ type: import_zod3.default.enum(["LOCAL", "GLOBAL", "PERIODIC"]),
85
+ usage: import_zod3.default.enum(["TRIP", "REGISTRATION", "PRODUCTS", "ALL"])
86
+ });
87
+ var addCredits = async (client, payload) => {
88
+ const result = schema2.safeParse(payload);
89
+ if (!result.success) {
90
+ throw new TypeError("Invalid args", {
91
+ cause: result.error.issues
92
+ });
93
+ }
94
+ return client.post(
95
+ `/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/entities/${payload.entityId}/wallets`,
96
+ result.data
97
+ ).then(({ data }) => data);
98
+ };
59
99
  // Annotate the CommonJS export names for ESM import in node:
60
100
  0 && (module.exports = {
61
- chargeProduct
101
+ addCredits,
102
+ chargeProduct,
103
+ getUserCreditsByEntityId
62
104
  });
package/dist/index.mjs CHANGED
@@ -20,6 +20,46 @@ var chargeProduct = async (client, info) => {
20
20
  }
21
21
  return client.post(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/product`, info).then(({ data }) => data);
22
22
  };
23
+
24
+ // src/getUserCreditsByEntityId.ts
25
+ import { z as z2 } from "zod";
26
+ var getUserCreditsByEntityId = async (client, id) => {
27
+ const result = z2.string().trim().min(1).uuid().safeParse(id);
28
+ if (!result.success) {
29
+ throw new TypeError("Invalid id", {
30
+ cause: result.error.issues
31
+ });
32
+ }
33
+ return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${id}`).then(({ data }) => data);
34
+ };
35
+
36
+ // src/addCredits.ts
37
+ import z3 from "zod";
38
+ var schema2 = z3.object({
39
+ initialAmount: z3.number().min(0),
40
+ validityStartDate: z3.string().datetime(),
41
+ validityEndDate: z3.string().datetime(),
42
+ notes: z3.string().trim().nullable().optional(),
43
+ discountCategory: z3.enum(["CREDITS", "PERCENTAGE"]),
44
+ oneTimeUsage: z3.boolean().optional(),
45
+ entityId: z3.string().trim().min(1).uuid(),
46
+ type: z3.enum(["LOCAL", "GLOBAL", "PERIODIC"]),
47
+ usage: z3.enum(["TRIP", "REGISTRATION", "PRODUCTS", "ALL"])
48
+ });
49
+ var addCredits = async (client, payload) => {
50
+ const result = schema2.safeParse(payload);
51
+ if (!result.success) {
52
+ throw new TypeError("Invalid args", {
53
+ cause: result.error.issues
54
+ });
55
+ }
56
+ return client.post(
57
+ `/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/entities/${payload.entityId}/wallets`,
58
+ result.data
59
+ ).then(({ data }) => data);
60
+ };
23
61
  export {
24
- chargeProduct
62
+ addCredits,
63
+ chargeProduct,
64
+ getUserCreditsByEntityId
25
65
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vulog/aima-billing",
3
- "version": "1.1.74",
3
+ "version": "1.1.75",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -19,8 +19,8 @@
19
19
  "author": "Vulog",
20
20
  "license": "ISC",
21
21
  "dependencies": {
22
- "@vulog/aima-client": "1.1.74",
23
- "@vulog/aima-core": "1.1.74"
22
+ "@vulog/aima-client": "1.1.75",
23
+ "@vulog/aima-core": "1.1.75"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "zod": "^3.24.2"
@@ -0,0 +1,112 @@
1
+ import { describe, test, vi, expect } from 'vitest';
2
+
3
+ import { addCredits } from './addCredits';
4
+ import { Client } from '@vulog/aima-client';
5
+
6
+ describe('addCredits', () => {
7
+ const postMock = vi.fn();
8
+ const client = {
9
+ post: postMock,
10
+ clientOptions: {
11
+ fleetId: 'FLEET_ID',
12
+ },
13
+ } as unknown as Client;
14
+
15
+ const addCreditsSpy = vi.fn().mockImplementation(addCredits);
16
+
17
+ test('Call OK with valid payload', async () => {
18
+ const payload = {
19
+ initialAmount: 100,
20
+ validityStartDate: '2023-01-01T00:00:00Z',
21
+ validityEndDate: '2024-01-01T00:00:00Z',
22
+ notes: 'Test credit',
23
+ discountCategory: 'CREDITS',
24
+ oneTimeUsage: true,
25
+ entityId: 'de66ea16-e942-4eaa-9f86-491e62bf59a2',
26
+ type: 'LOCAL',
27
+ usage: 'TRIP',
28
+ };
29
+
30
+ const creditResponse = {
31
+ id: 'credit1',
32
+ availableAmount: 100,
33
+ usedAmount: 0,
34
+ originId: null,
35
+ creditAlreadyUsed: false,
36
+ updateDate: new Date().toISOString(),
37
+ ...payload,
38
+ };
39
+
40
+ postMock.mockResolvedValue({ data: creditResponse });
41
+
42
+ await addCreditsSpy(client, payload);
43
+ expect(addCreditsSpy).toHaveBeenCalledWith(client, payload);
44
+ expect(addCreditsSpy).toHaveBeenCalledTimes(1);
45
+ expect(postMock).toHaveBeenCalledTimes(1);
46
+ });
47
+
48
+ test('Should throw TypeError when entityId is not a valid UUID', async () => {
49
+ const invalidPayload = {
50
+ initialAmount: 100,
51
+ validityStartDate: '2023-01-01T00:00:00Z',
52
+ validityEndDate: '2024-01-01T00:00:00Z',
53
+ notes: 'Test credit',
54
+ discountCategory: 'CREDITS',
55
+ oneTimeUsage: true,
56
+ entityId: 'invalid-uuid',
57
+ type: 'LOCAL',
58
+ usage: 'TRIP',
59
+ };
60
+
61
+ await expect(addCreditsSpy(client, invalidPayload)).rejects.toThrow(TypeError);
62
+ await expect(addCreditsSpy(client, invalidPayload)).rejects.toThrow('Invalid args');
63
+ });
64
+
65
+ test('Should throw TypeError when initialAmount is negative', async () => {
66
+ const invalidPayload = {
67
+ initialAmount: -10,
68
+ validityStartDate: '2023-01-01T00:00:00Z',
69
+ validityEndDate: '2024-01-01T00:00:00Z',
70
+ notes: 'Test credit',
71
+ discountCategory: 'CREDITS',
72
+ oneTimeUsage: true,
73
+ entityId: 'de66ea16-e942-4eaa-9f86-491e62bf59a2',
74
+ type: 'LOCAL',
75
+ usage: 'TRIP',
76
+ };
77
+
78
+ await expect(addCreditsSpy(client, invalidPayload)).rejects.toThrow(TypeError);
79
+ });
80
+
81
+ test('Should throw TypeError when dates are invalid', async () => {
82
+ const invalidPayload = {
83
+ initialAmount: 100,
84
+ validityStartDate: 'not-a-date',
85
+ validityEndDate: '2024-01-01T00:00:00Z',
86
+ notes: 'Test credit',
87
+ discountCategory: 'CREDITS',
88
+ oneTimeUsage: true,
89
+ entityId: 'de66ea16-e942-4eaa-9f86-491e62bf59a2',
90
+ type: 'LOCAL',
91
+ usage: 'TRIP',
92
+ };
93
+
94
+ await expect(addCreditsSpy(client, invalidPayload)).rejects.toThrow(TypeError);
95
+ });
96
+
97
+ test('Should throw TypeError when enum value is invalid', async () => {
98
+ const invalidPayload = {
99
+ initialAmount: 100,
100
+ validityStartDate: '2023-01-01T00:00:00Z',
101
+ validityEndDate: '2024-01-01T00:00:00Z',
102
+ notes: 'Test credit',
103
+ discountCategory: 'INVALID_CATEGORY',
104
+ oneTimeUsage: true,
105
+ entityId: 'de66ea16-e942-4eaa-9f86-491e62bf59a2',
106
+ type: 'LOCAL',
107
+ usage: 'TRIP',
108
+ };
109
+
110
+ await expect(addCreditsSpy(client, invalidPayload as any)).rejects.toThrow(TypeError);
111
+ });
112
+ });
@@ -0,0 +1,36 @@
1
+ import { Client } from '@vulog/aima-client';
2
+
3
+ import z from 'zod';
4
+
5
+ import type { Credit } from './types';
6
+
7
+ interface Payload
8
+ extends Omit<Credit, 'id' | 'availableAmount' | 'usedAmount' | 'originId' | 'creditAlreadyUsed' | 'updateDate'> {}
9
+
10
+ const schema = z.object({
11
+ initialAmount: z.number().min(0),
12
+ validityStartDate: z.string().datetime(),
13
+ validityEndDate: z.string().datetime(),
14
+ notes: z.string().trim().nullable().optional(),
15
+ discountCategory: z.enum(['CREDITS', 'PERCENTAGE']),
16
+ oneTimeUsage: z.boolean().optional(),
17
+ entityId: z.string().trim().min(1).uuid(),
18
+ type: z.enum(['LOCAL', 'GLOBAL', 'PERIODIC']),
19
+ usage: z.enum(['TRIP', 'REGISTRATION', 'PRODUCTS', 'ALL']),
20
+ });
21
+
22
+ export const addCredits = async (client: Client, payload: Payload): Promise<Credit> => {
23
+ const result = schema.safeParse(payload);
24
+ if (!result.success) {
25
+ throw new TypeError('Invalid args', {
26
+ cause: result.error.issues,
27
+ });
28
+ }
29
+
30
+ return client
31
+ .post<Credit>(
32
+ `/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/entities/${payload.entityId}/wallets`,
33
+ result.data
34
+ )
35
+ .then(({ data }) => data);
36
+ };
@@ -0,0 +1,64 @@
1
+ import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { Client } from '@vulog/aima-client';
3
+ import { getUserCreditsByEntityId } from './getUserCreditsByEntityId';
4
+
5
+ describe('getUserCreditsByEntityId', () => {
6
+ const FLEET_ID = 'FLEET_ID';
7
+ const ENTITY_ID = 'de66ea16-e942-4eaa-9f86-491e62bf59a2';
8
+ const getMock = vi.fn();
9
+ const client = {
10
+ get: getMock,
11
+ clientOptions: {
12
+ fleetId: FLEET_ID,
13
+ },
14
+ } as unknown as Client;
15
+
16
+ beforeEach(() => {
17
+ getMock.mockReset();
18
+ vi.useFakeTimers({ now: new Date('2025-01-12T13:35:50.123Z') });
19
+ });
20
+
21
+ afterEach(() => {
22
+ vi.useRealTimers();
23
+ });
24
+
25
+ test('call OK', async () => {
26
+ const data = {
27
+ id: ENTITY_ID,
28
+ credits: [
29
+ {
30
+ id: 'credit-id',
31
+ name: 'Test Credit',
32
+ description: 'Test Description',
33
+ availableAmount: 100,
34
+ usedAmount: 50,
35
+ originId: 'origin-id',
36
+ entityId: ENTITY_ID,
37
+ creditAlreadyUsed: false,
38
+ updateDate: '2025-01-12T13:35:50.123Z',
39
+ type: 'LOCAL',
40
+ },
41
+ ],
42
+ };
43
+
44
+ getMock.mockResolvedValueOnce({ data });
45
+
46
+ const result = await getUserCreditsByEntityId(client, ENTITY_ID);
47
+ expect(result).toEqual(data);
48
+ expect(getMock).toHaveBeenCalledWith(`/boapi/proxy/billing/fleets/${FLEET_ID}/wallet/entity/${ENTITY_ID}`);
49
+ });
50
+
51
+ test('invalid id', async () => {
52
+ const rejects = expect(() => getUserCreditsByEntityId(client, 'invalid-id')).rejects;
53
+ await rejects.toThrowError('Invalid id');
54
+ await rejects.toSatisfy((error: any) => {
55
+ expect(error).toHaveProperty('cause');
56
+ expect(error.cause).toHaveLength(1);
57
+ expect(error.cause[0]).toHaveProperty('code');
58
+ expect(error.cause[0].code).toBe('invalid_string');
59
+ expect(error.cause[0]).toHaveProperty('message');
60
+ expect(error.cause[0].message).toBe('Invalid uuid');
61
+ return true;
62
+ });
63
+ });
64
+ });
@@ -0,0 +1,16 @@
1
+ import { Client } from '@vulog/aima-client';
2
+ import { z } from 'zod';
3
+
4
+ import { Credit } from './types';
5
+
6
+ export const getUserCreditsByEntityId = async (client: Client, id: string): Promise<Credit> => {
7
+ const result = z.string().trim().min(1).uuid().safeParse(id);
8
+ if (!result.success) {
9
+ throw new TypeError('Invalid id', {
10
+ cause: result.error.issues,
11
+ });
12
+ }
13
+ return client
14
+ .get<Credit>(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${id}`)
15
+ .then(({ data }) => data);
16
+ };
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './chargeProduct';
2
2
  export * from './types';
3
+ export * from './getUserCreditsByEntityId';
4
+ export * from './addCredits';
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { UUID } from 'crypto';
2
+
1
3
  export type ChargeProductInfo = {
2
4
  productId: string;
3
5
  serviceId?: string;
@@ -13,3 +15,22 @@ export type ChargeProductInfo = {
13
15
  export type Invoice = {
14
16
  [key: string]: any;
15
17
  };
18
+
19
+ export type Credit = {
20
+ initialAmount: number;
21
+ validityStartDate: string;
22
+ validityEndDate: string;
23
+ notes: string;
24
+ discountCategory: 'CREDITS' | 'PERCENTAGE';
25
+ oneTimeUsage: boolean;
26
+ id: UUID;
27
+ availableAmount: number;
28
+ usedAmount: number;
29
+ originId: UUID;
30
+ entityId: UUID;
31
+ creditAlreadyUsed: boolean;
32
+ updateDate: string;
33
+ type: 'LOCAL' | 'GLOBAL' | 'PERIODIC';
34
+ usage: 'TRIP' | 'REGISTRATION' | 'PRODUCTS' | 'ALL';
35
+ [key: string]: any;
36
+ };