@vulog/aima-billing 1.1.96 → 1.1.98
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 +26 -2
- package/dist/index.d.ts +26 -2
- package/dist/index.js +52 -4
- package/dist/index.mjs +49 -3
- package/package.json +3 -3
- package/src/getWalletsByEntity.ts +16 -0
- package/src/index.ts +2 -0
- package/src/refund.test.ts +50 -0
- package/src/refund.ts +9 -3
- package/src/types.ts +18 -0
- package/src/updateWallet.test.ts +62 -0
- package/src/updateWallet.ts +35 -0
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Client } from '@vulog/aima-client';
|
|
2
2
|
import { UUID } from 'crypto';
|
|
3
|
+
import { PatchAction } from '@vulog/aima-core';
|
|
3
4
|
|
|
4
5
|
type ChargeProductInfo = {
|
|
5
6
|
productId: string;
|
|
@@ -112,6 +113,23 @@ type RefundableAmount = {
|
|
|
112
113
|
refundablePaymentReceipts: Receipt[];
|
|
113
114
|
refundedPaymentReceipts: Receipt[];
|
|
114
115
|
};
|
|
116
|
+
type Wallet = {
|
|
117
|
+
initialAmount: number;
|
|
118
|
+
validityStartDate: string;
|
|
119
|
+
validityEndDate: string;
|
|
120
|
+
notes: string;
|
|
121
|
+
discountCategory: string;
|
|
122
|
+
oneTimeUsage: boolean;
|
|
123
|
+
id: string;
|
|
124
|
+
availableAmount: number;
|
|
125
|
+
usedAmount: number;
|
|
126
|
+
originId: string;
|
|
127
|
+
entityId: string;
|
|
128
|
+
creditAlreadyUsed: boolean;
|
|
129
|
+
updateDate: string;
|
|
130
|
+
type: string;
|
|
131
|
+
usage: string;
|
|
132
|
+
};
|
|
115
133
|
|
|
116
134
|
declare const chargeProduct: (client: Client, info: ChargeProductInfo) => Promise<Invoice>;
|
|
117
135
|
|
|
@@ -128,9 +146,15 @@ declare const getRefundableAmount: (client: Client, invoiceId: string) => Promis
|
|
|
128
146
|
interface Payload {
|
|
129
147
|
invoiceId: string;
|
|
130
148
|
amount?: number;
|
|
131
|
-
|
|
149
|
+
note?: string | null;
|
|
132
150
|
paymentIntentPspReference?: string;
|
|
133
151
|
}
|
|
134
152
|
declare const refund: (client: Client, payload: Payload) => Promise<void>;
|
|
135
153
|
|
|
136
|
-
|
|
154
|
+
declare const getWalletsByEntity: (client: Client, entityId: string) => Promise<Wallet[]>;
|
|
155
|
+
|
|
156
|
+
declare const paths: readonly ["/availableAmount", "/percentage"];
|
|
157
|
+
type Paths = (typeof paths)[number];
|
|
158
|
+
declare const updateWallet: (client: Client, walletId: string, actions: PatchAction<Paths>[]) => Promise<void>;
|
|
159
|
+
|
|
160
|
+
export { type ChargeProductInfo, type Credit, type Invoice, type Paths, type Receipt, type RefundableAmount, type Wallet, addCredits, chargeProduct, getInvoiceById, getRefundableAmount, getUserCreditsByEntityId, getWalletsByEntity, refund, updateWallet };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Client } from '@vulog/aima-client';
|
|
2
2
|
import { UUID } from 'crypto';
|
|
3
|
+
import { PatchAction } from '@vulog/aima-core';
|
|
3
4
|
|
|
4
5
|
type ChargeProductInfo = {
|
|
5
6
|
productId: string;
|
|
@@ -112,6 +113,23 @@ type RefundableAmount = {
|
|
|
112
113
|
refundablePaymentReceipts: Receipt[];
|
|
113
114
|
refundedPaymentReceipts: Receipt[];
|
|
114
115
|
};
|
|
116
|
+
type Wallet = {
|
|
117
|
+
initialAmount: number;
|
|
118
|
+
validityStartDate: string;
|
|
119
|
+
validityEndDate: string;
|
|
120
|
+
notes: string;
|
|
121
|
+
discountCategory: string;
|
|
122
|
+
oneTimeUsage: boolean;
|
|
123
|
+
id: string;
|
|
124
|
+
availableAmount: number;
|
|
125
|
+
usedAmount: number;
|
|
126
|
+
originId: string;
|
|
127
|
+
entityId: string;
|
|
128
|
+
creditAlreadyUsed: boolean;
|
|
129
|
+
updateDate: string;
|
|
130
|
+
type: string;
|
|
131
|
+
usage: string;
|
|
132
|
+
};
|
|
115
133
|
|
|
116
134
|
declare const chargeProduct: (client: Client, info: ChargeProductInfo) => Promise<Invoice>;
|
|
117
135
|
|
|
@@ -128,9 +146,15 @@ declare const getRefundableAmount: (client: Client, invoiceId: string) => Promis
|
|
|
128
146
|
interface Payload {
|
|
129
147
|
invoiceId: string;
|
|
130
148
|
amount?: number;
|
|
131
|
-
|
|
149
|
+
note?: string | null;
|
|
132
150
|
paymentIntentPspReference?: string;
|
|
133
151
|
}
|
|
134
152
|
declare const refund: (client: Client, payload: Payload) => Promise<void>;
|
|
135
153
|
|
|
136
|
-
|
|
154
|
+
declare const getWalletsByEntity: (client: Client, entityId: string) => Promise<Wallet[]>;
|
|
155
|
+
|
|
156
|
+
declare const paths: readonly ["/availableAmount", "/percentage"];
|
|
157
|
+
type Paths = (typeof paths)[number];
|
|
158
|
+
declare const updateWallet: (client: Client, walletId: string, actions: PatchAction<Paths>[]) => Promise<void>;
|
|
159
|
+
|
|
160
|
+
export { type ChargeProductInfo, type Credit, type Invoice, type Paths, type Receipt, type RefundableAmount, type Wallet, addCredits, chargeProduct, getInvoiceById, getRefundableAmount, getUserCreditsByEntityId, getWalletsByEntity, refund, updateWallet };
|
package/dist/index.js
CHANGED
|
@@ -35,7 +35,9 @@ __export(index_exports, {
|
|
|
35
35
|
getInvoiceById: () => getInvoiceById,
|
|
36
36
|
getRefundableAmount: () => getRefundableAmount,
|
|
37
37
|
getUserCreditsByEntityId: () => getUserCreditsByEntityId,
|
|
38
|
-
|
|
38
|
+
getWalletsByEntity: () => getWalletsByEntity,
|
|
39
|
+
refund: () => refund,
|
|
40
|
+
updateWallet: () => updateWallet
|
|
39
41
|
});
|
|
40
42
|
module.exports = __toCommonJS(index_exports);
|
|
41
43
|
|
|
@@ -129,7 +131,7 @@ var getRefundableAmount = async (client, invoiceId) => {
|
|
|
129
131
|
var import_zod6 = __toESM(require("zod"));
|
|
130
132
|
var schema4 = import_zod6.default.object({
|
|
131
133
|
amount: import_zod6.default.number().min(0).optional(),
|
|
132
|
-
|
|
134
|
+
note: import_zod6.default.string().trim().nullable().optional(),
|
|
133
135
|
paymentIntentPspReference: import_zod6.default.string().trim().min(1).optional()
|
|
134
136
|
});
|
|
135
137
|
var refund = async (client, payload) => {
|
|
@@ -139,11 +141,55 @@ var refund = async (client, payload) => {
|
|
|
139
141
|
cause: result.error.issues
|
|
140
142
|
});
|
|
141
143
|
}
|
|
144
|
+
const filteredData = Object.fromEntries(
|
|
145
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
146
|
+
Object.entries(result.data).filter(([_, value]) => value !== void 0 && value !== null)
|
|
147
|
+
);
|
|
142
148
|
return client.post(
|
|
143
149
|
`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${payload.invoiceId}/refund`,
|
|
144
|
-
|
|
150
|
+
// filter boolean fields
|
|
151
|
+
filteredData
|
|
145
152
|
).then(({ data }) => data);
|
|
146
153
|
};
|
|
154
|
+
|
|
155
|
+
// src/getWalletsByEntity.ts
|
|
156
|
+
var import_zod7 = require("zod");
|
|
157
|
+
var getWalletsByEntity = async (client, entityId) => {
|
|
158
|
+
const result = import_zod7.z.string().trim().min(1).uuid().safeParse(entityId);
|
|
159
|
+
if (!result.success) {
|
|
160
|
+
throw new TypeError("Invalid entityId", {
|
|
161
|
+
cause: result.error.issues
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${entityId}`).then(({ data }) => data);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// src/updateWallet.ts
|
|
168
|
+
var import_zod8 = require("zod");
|
|
169
|
+
var paths = ["/availableAmount", "/percentage"];
|
|
170
|
+
var schema5 = import_zod8.z.object({
|
|
171
|
+
walletId: import_zod8.z.string().trim().min(1).uuid(),
|
|
172
|
+
actions: import_zod8.z.array(
|
|
173
|
+
import_zod8.z.object({
|
|
174
|
+
op: import_zod8.z.enum(["replace"]),
|
|
175
|
+
path: import_zod8.z.enum(paths),
|
|
176
|
+
value: import_zod8.z.string().min(1)
|
|
177
|
+
})
|
|
178
|
+
).min(1).max(1)
|
|
179
|
+
});
|
|
180
|
+
var updateWallet = async (client, walletId, actions) => {
|
|
181
|
+
const result = schema5.safeParse({ walletId, actions });
|
|
182
|
+
if (!result.success) {
|
|
183
|
+
throw new TypeError("Invalid args", {
|
|
184
|
+
cause: result.error.issues
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
await client.patch(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallets/${walletId}`, actions, {
|
|
188
|
+
headers: {
|
|
189
|
+
"Content-Type": "application/json-patch+json"
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
};
|
|
147
193
|
// Annotate the CommonJS export names for ESM import in node:
|
|
148
194
|
0 && (module.exports = {
|
|
149
195
|
addCredits,
|
|
@@ -151,5 +197,7 @@ var refund = async (client, payload) => {
|
|
|
151
197
|
getInvoiceById,
|
|
152
198
|
getRefundableAmount,
|
|
153
199
|
getUserCreditsByEntityId,
|
|
154
|
-
|
|
200
|
+
getWalletsByEntity,
|
|
201
|
+
refund,
|
|
202
|
+
updateWallet
|
|
155
203
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -88,7 +88,7 @@ var getRefundableAmount = async (client, invoiceId) => {
|
|
|
88
88
|
import z6 from "zod";
|
|
89
89
|
var schema4 = z6.object({
|
|
90
90
|
amount: z6.number().min(0).optional(),
|
|
91
|
-
|
|
91
|
+
note: z6.string().trim().nullable().optional(),
|
|
92
92
|
paymentIntentPspReference: z6.string().trim().min(1).optional()
|
|
93
93
|
});
|
|
94
94
|
var refund = async (client, payload) => {
|
|
@@ -98,16 +98,62 @@ var refund = async (client, payload) => {
|
|
|
98
98
|
cause: result.error.issues
|
|
99
99
|
});
|
|
100
100
|
}
|
|
101
|
+
const filteredData = Object.fromEntries(
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
103
|
+
Object.entries(result.data).filter(([_, value]) => value !== void 0 && value !== null)
|
|
104
|
+
);
|
|
101
105
|
return client.post(
|
|
102
106
|
`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${payload.invoiceId}/refund`,
|
|
103
|
-
|
|
107
|
+
// filter boolean fields
|
|
108
|
+
filteredData
|
|
104
109
|
).then(({ data }) => data);
|
|
105
110
|
};
|
|
111
|
+
|
|
112
|
+
// src/getWalletsByEntity.ts
|
|
113
|
+
import { z as z7 } from "zod";
|
|
114
|
+
var getWalletsByEntity = async (client, entityId) => {
|
|
115
|
+
const result = z7.string().trim().min(1).uuid().safeParse(entityId);
|
|
116
|
+
if (!result.success) {
|
|
117
|
+
throw new TypeError("Invalid entityId", {
|
|
118
|
+
cause: result.error.issues
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${entityId}`).then(({ data }) => data);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/updateWallet.ts
|
|
125
|
+
import { z as z8 } from "zod";
|
|
126
|
+
var paths = ["/availableAmount", "/percentage"];
|
|
127
|
+
var schema5 = z8.object({
|
|
128
|
+
walletId: z8.string().trim().min(1).uuid(),
|
|
129
|
+
actions: z8.array(
|
|
130
|
+
z8.object({
|
|
131
|
+
op: z8.enum(["replace"]),
|
|
132
|
+
path: z8.enum(paths),
|
|
133
|
+
value: z8.string().min(1)
|
|
134
|
+
})
|
|
135
|
+
).min(1).max(1)
|
|
136
|
+
});
|
|
137
|
+
var updateWallet = async (client, walletId, actions) => {
|
|
138
|
+
const result = schema5.safeParse({ walletId, actions });
|
|
139
|
+
if (!result.success) {
|
|
140
|
+
throw new TypeError("Invalid args", {
|
|
141
|
+
cause: result.error.issues
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
await client.patch(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallets/${walletId}`, actions, {
|
|
145
|
+
headers: {
|
|
146
|
+
"Content-Type": "application/json-patch+json"
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
};
|
|
106
150
|
export {
|
|
107
151
|
addCredits,
|
|
108
152
|
chargeProduct,
|
|
109
153
|
getInvoiceById,
|
|
110
154
|
getRefundableAmount,
|
|
111
155
|
getUserCreditsByEntityId,
|
|
112
|
-
|
|
156
|
+
getWalletsByEntity,
|
|
157
|
+
refund,
|
|
158
|
+
updateWallet
|
|
113
159
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vulog/aima-billing",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.98",
|
|
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": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@vulog/aima-client": "1.1.
|
|
23
|
-
"@vulog/aima-core": "1.1.
|
|
22
|
+
"@vulog/aima-client": "1.1.98",
|
|
23
|
+
"@vulog/aima-core": "1.1.98"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"zod": "^3.25.76"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Client } from '@vulog/aima-client';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { Wallet } from './types';
|
|
5
|
+
|
|
6
|
+
export const getWalletsByEntity = async (client: Client, entityId: string): Promise<Wallet[]> => {
|
|
7
|
+
const result = z.string().trim().min(1).uuid().safeParse(entityId);
|
|
8
|
+
if (!result.success) {
|
|
9
|
+
throw new TypeError('Invalid entityId', {
|
|
10
|
+
cause: result.error.issues,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return client
|
|
14
|
+
.get<Wallet[]>(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${entityId}`)
|
|
15
|
+
.then(({ data }) => data);
|
|
16
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { refund } from './refund';
|
|
3
|
+
import { Client } from '@vulog/aima-client';
|
|
4
|
+
|
|
5
|
+
describe('refund', () => {
|
|
6
|
+
const postMock = vi.fn();
|
|
7
|
+
const client = {
|
|
8
|
+
post: postMock,
|
|
9
|
+
clientOptions: {
|
|
10
|
+
fleetId: 'FLEET_ID',
|
|
11
|
+
},
|
|
12
|
+
} as unknown as Client;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
vi.clearAllMocks();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('should return invalid args', async () => {
|
|
19
|
+
const payload = {
|
|
20
|
+
invoiceId: 'INVOICE_ID',
|
|
21
|
+
amount: -10, // Invalid amount
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// expect to throw error if invalid args are provided
|
|
25
|
+
await expect(refund(client, payload)).rejects.toThrow('Invalid args');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should process refund successfully', async () => {
|
|
29
|
+
const payload = {
|
|
30
|
+
invoiceId: 'INVOICE_ID',
|
|
31
|
+
amount: 50,
|
|
32
|
+
note: 'Customer requested refund',
|
|
33
|
+
paymentIntentPspReference: undefined,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
postMock.mockResolvedValueOnce({
|
|
37
|
+
data: undefined,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const result = await refund(client, payload);
|
|
41
|
+
expect(result).toBeUndefined();
|
|
42
|
+
expect(postMock).toHaveBeenCalledWith(
|
|
43
|
+
`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${payload.invoiceId}/refund`,
|
|
44
|
+
{
|
|
45
|
+
amount: 50,
|
|
46
|
+
note: 'Customer requested refund',
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
});
|
package/src/refund.ts
CHANGED
|
@@ -5,13 +5,13 @@ import z from 'zod';
|
|
|
5
5
|
interface Payload {
|
|
6
6
|
invoiceId: string;
|
|
7
7
|
amount?: number;
|
|
8
|
-
|
|
8
|
+
note?: string | null;
|
|
9
9
|
paymentIntentPspReference?: string;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
const schema = z.object({
|
|
13
13
|
amount: z.number().min(0).optional(),
|
|
14
|
-
|
|
14
|
+
note: z.string().trim().nullable().optional(),
|
|
15
15
|
paymentIntentPspReference: z.string().trim().min(1).optional(),
|
|
16
16
|
});
|
|
17
17
|
|
|
@@ -23,10 +23,16 @@ export const refund = async (client: Client, payload: Payload): Promise<void> =>
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
const filteredData = Object.fromEntries(
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
28
|
+
Object.entries(result.data).filter(([_, value]) => value !== undefined && value !== null)
|
|
29
|
+
);
|
|
30
|
+
|
|
26
31
|
return client
|
|
27
32
|
.post<void>(
|
|
28
33
|
`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${payload.invoiceId}/refund`,
|
|
29
|
-
|
|
34
|
+
// filter boolean fields
|
|
35
|
+
filteredData
|
|
30
36
|
)
|
|
31
37
|
.then(({ data }) => data);
|
|
32
38
|
};
|
package/src/types.ts
CHANGED
|
@@ -125,3 +125,21 @@ export type RefundableAmount = {
|
|
|
125
125
|
refundablePaymentReceipts: Receipt[];
|
|
126
126
|
refundedPaymentReceipts: Receipt[];
|
|
127
127
|
};
|
|
128
|
+
|
|
129
|
+
export type Wallet = {
|
|
130
|
+
initialAmount: number;
|
|
131
|
+
validityStartDate: string;
|
|
132
|
+
validityEndDate: string;
|
|
133
|
+
notes: string;
|
|
134
|
+
discountCategory: string;
|
|
135
|
+
oneTimeUsage: boolean;
|
|
136
|
+
id: string;
|
|
137
|
+
availableAmount: number;
|
|
138
|
+
usedAmount: number;
|
|
139
|
+
originId: string;
|
|
140
|
+
entityId: string;
|
|
141
|
+
creditAlreadyUsed: boolean;
|
|
142
|
+
updateDate: string;
|
|
143
|
+
type: string;
|
|
144
|
+
usage: string;
|
|
145
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { PatchAction } from '@vulog/aima-core';
|
|
3
|
+
import { updateWallet, Paths } from './updateWallet';
|
|
4
|
+
|
|
5
|
+
import { Client } from '@vulog/aima-client';
|
|
6
|
+
|
|
7
|
+
describe('updateWallet', () => {
|
|
8
|
+
const patchMock = vi.fn();
|
|
9
|
+
const client = {
|
|
10
|
+
patch: patchMock,
|
|
11
|
+
clientOptions: {
|
|
12
|
+
fleetId: 'FLEET_ID',
|
|
13
|
+
},
|
|
14
|
+
} as unknown as Client;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
vi.clearAllMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('should return invalid args', async () => {
|
|
21
|
+
const walletId = 'WALLET_ID';
|
|
22
|
+
const actions = [
|
|
23
|
+
{
|
|
24
|
+
op: 'invalid-op',
|
|
25
|
+
path: '/availableAmount',
|
|
26
|
+
value: '100',
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
await expect(updateWallet(client, walletId, actions as PatchAction<Paths>[])).rejects.toThrowError();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('should return invalid args with too much entries in the array', async () => {
|
|
34
|
+
const walletId = 'WALLET_ID';
|
|
35
|
+
const actions = [
|
|
36
|
+
{
|
|
37
|
+
op: 'replace',
|
|
38
|
+
path: '/availableAmount',
|
|
39
|
+
value: '100',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
op: 'replace',
|
|
43
|
+
path: '/percentage',
|
|
44
|
+
value: '20.50',
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
await expect(updateWallet(client, walletId, actions as PatchAction<Paths>[])).rejects.toThrowError();
|
|
49
|
+
});
|
|
50
|
+
test('should update wallet successfully', async () => {
|
|
51
|
+
const walletId = '550e8400-e29b-41d4-a716-446655440000';
|
|
52
|
+
const actions = [
|
|
53
|
+
{
|
|
54
|
+
op: 'replace' as const,
|
|
55
|
+
path: '/availableAmount' as const,
|
|
56
|
+
value: '100',
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
await expect(updateWallet(client, walletId, actions)).resolves.toBeUndefined();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Client } from '@vulog/aima-client';
|
|
2
|
+
import { PatchAction } from '@vulog/aima-core';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
const paths = ['/availableAmount', '/percentage'] as const;
|
|
6
|
+
export type Paths = (typeof paths)[number];
|
|
7
|
+
|
|
8
|
+
const schema = z.object({
|
|
9
|
+
walletId: z.string().trim().min(1).uuid(),
|
|
10
|
+
actions: z
|
|
11
|
+
.array(
|
|
12
|
+
z.object({
|
|
13
|
+
op: z.enum(['replace']),
|
|
14
|
+
path: z.enum(paths),
|
|
15
|
+
value: z.string().min(1),
|
|
16
|
+
})
|
|
17
|
+
)
|
|
18
|
+
.min(1)
|
|
19
|
+
.max(1),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const updateWallet = async (client: Client, walletId: string, actions: PatchAction<Paths>[]): Promise<void> => {
|
|
23
|
+
const result = schema.safeParse({ walletId, actions });
|
|
24
|
+
if (!result.success) {
|
|
25
|
+
throw new TypeError('Invalid args', {
|
|
26
|
+
cause: result.error.issues,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await client.patch(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallets/${walletId}`, actions, {
|
|
31
|
+
headers: {
|
|
32
|
+
'Content-Type': 'application/json-patch+json',
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
};
|