oak-pay-business 3.1.2 → 3.1.3

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.
@@ -6,6 +6,8 @@ export interface Schema extends EntityShape {
6
6
  when?: Datetime;
7
7
  order: Order;
8
8
  price: Price;
9
+ settledAt?: Datetime;
10
+ closedAt?: Datetime;
9
11
  }
10
12
  type IState = 'unsettled' | 'settled' | 'closed';
11
13
  type IAction = 'settle' | 'close';
@@ -15,6 +15,8 @@ export const entityDesc = {
15
15
  order: '订单',
16
16
  price: '结算金额',
17
17
  iState: '结算计划状态',
18
+ settledAt: '结算时间',
19
+ closedAt: '关闭时间'
18
20
  },
19
21
  action: {
20
22
  settle: '结算',
@@ -13,6 +13,12 @@ export const desc = {
13
13
  notNull: true,
14
14
  type: "money"
15
15
  },
16
+ settledAt: {
17
+ type: "datetime"
18
+ },
19
+ closedAt: {
20
+ type: "datetime"
21
+ },
16
22
  iState: {
17
23
  type: "enum",
18
24
  enumeration: ["unsettled", "settled", "closed"]
@@ -7,6 +7,8 @@ export type OpSchema = EntityShape & {
7
7
  when?: Datetime | null;
8
8
  orderId: ForeignKey<"order">;
9
9
  price: Price;
10
+ settledAt?: Datetime | null;
11
+ closedAt?: Datetime | null;
10
12
  iState?: IState | null;
11
13
  } & {
12
14
  [A in ExpressionKey]?: any;
@@ -20,6 +22,8 @@ export type OpFilter = {
20
22
  when: Q_DateValue;
21
23
  orderId: Q_StringValue;
22
24
  price: Q_NumberValue;
25
+ settledAt: Q_DateValue;
26
+ closedAt: Q_DateValue;
23
27
  iState: Q_EnumValue<IState>;
24
28
  } & ExprOp<OpAttr | string>;
25
29
  export type OpProjection = {
@@ -32,6 +36,8 @@ export type OpProjection = {
32
36
  when?: number;
33
37
  orderId?: number;
34
38
  price?: number;
39
+ settledAt?: number;
40
+ closedAt?: number;
35
41
  iState?: number;
36
42
  } & Partial<ExprOp<OpAttr | string>>;
37
43
  export type OpSortAttr = Partial<{
@@ -42,6 +48,8 @@ export type OpSortAttr = Partial<{
42
48
  when: number;
43
49
  orderId: number;
44
50
  price: number;
51
+ settledAt: number;
52
+ closedAt: number;
45
53
  iState: number;
46
54
  [k: string]: any;
47
55
  } | ExprOp<OpAttr | string>>;
@@ -4,7 +4,9 @@
4
4
  "when": "结算时间",
5
5
  "order": "订单",
6
6
  "price": "结算金额",
7
- "iState": "结算计划状态"
7
+ "iState": "结算计划状态",
8
+ "settledAt": "结算时间",
9
+ "closedAt": "关闭时间"
8
10
  },
9
11
  "action": {
10
12
  "settle": "结算",
@@ -3,62 +3,50 @@ import assert from 'assert';
3
3
  import { groupBy } from 'oak-domain/lib/utils/lodash';
4
4
  const triggers = [
5
5
  {
6
- name: '当settlePlan创建时更新关联order的settlePlanned',
6
+ name: '当settlePlan创建后,更新关联order的settlePlanned',
7
7
  entity: 'settlePlan',
8
8
  action: 'create',
9
- when: 'before',
9
+ when: 'after',
10
10
  asRoot: true,
11
- priority: 99,
11
+ // priority: 99,
12
12
  fn: async ({ operation }, context, option) => {
13
13
  const { data } = operation;
14
- let count = 0;
14
+ let ids = [];
15
15
  if (data instanceof Array) {
16
- const planArr = groupBy(data, 'orderId');
17
- const orderIds = Object.keys(planArr);
18
- for (const orderId of orderIds) {
19
- const [order] = await context.select('order', {
20
- data: {
21
- id: 1,
22
- paid: 1,
23
- refunded: 1,
24
- settlePlanned: 1,
25
- },
26
- filter: {
27
- id: orderId,
28
- }
29
- }, { forUpdate: true });
30
- const { id, paid, refunded, settlePlanned } = order;
31
- let planPrice = 0;
32
- const plans = planArr[orderId];
33
- plans.forEach((plan) => planPrice += plan.price);
34
- const newSettlePlanned = planPrice + settlePlanned;
35
- await context.operate('order', {
36
- id: await generateNewIdAsync(),
37
- action: 'update',
38
- data: {
39
- settlePlanned: newSettlePlanned,
40
- },
41
- filter: {
42
- id,
43
- }
44
- }, option);
45
- }
46
- count += data.length;
16
+ ids = data.map((ele) => ele.id);
47
17
  }
48
18
  else {
19
+ ids = [data.id];
20
+ }
21
+ const settlePlans = await context.select('settlePlan', {
22
+ data: {
23
+ id: 1,
24
+ orderId: 1,
25
+ price: 1,
26
+ },
27
+ filter: {
28
+ id: {
29
+ $in: ids,
30
+ }
31
+ },
32
+ }, { forUpdate: true });
33
+ const planArr = groupBy(settlePlans, 'orderId');
34
+ const orderIds = Object.keys(planArr);
35
+ for (const orderId of orderIds) {
49
36
  const [order] = await context.select('order', {
50
37
  data: {
51
38
  id: 1,
52
- paid: 1,
53
- refunded: 1,
54
39
  settlePlanned: 1,
55
40
  },
56
41
  filter: {
57
- id: data.orderId,
42
+ id: orderId,
58
43
  }
59
44
  }, { forUpdate: true });
60
- const { id, paid, refunded, settlePlanned } = order;
61
- const newSettlePlanned = data.price + settlePlanned;
45
+ const { id, settlePlanned } = order;
46
+ let planPrice = 0;
47
+ const plans = planArr[orderId];
48
+ plans.forEach((plan) => planPrice += plan.price);
49
+ const newSettlePlanned = planPrice + settlePlanned;
62
50
  await context.operate('order', {
63
51
  id: await generateNewIdAsync(),
64
52
  action: 'update',
@@ -69,20 +57,19 @@ const triggers = [
69
57
  id,
70
58
  }
71
59
  }, option);
72
- count++;
73
60
  }
74
- return count;
61
+ return ids.length;
75
62
  },
76
63
  },
77
64
  {
78
- name: '当settlePlan执行settle时将关联的settlement执行settle,并更新order的settled',
65
+ name: '当settlePlan执行settle时将关联的settlement执行settle',
79
66
  entity: 'settlePlan',
80
67
  action: 'settle',
81
68
  when: 'before',
82
69
  asRoot: true,
83
70
  priority: 99,
84
71
  fn: async ({ operation }, context, option) => {
85
- const { filter } = operation;
72
+ const { filter, data } = operation;
86
73
  const settlePlans = await context.select('settlePlan', {
87
74
  data: {
88
75
  id: 1,
@@ -99,6 +86,63 @@ const triggers = [
99
86
  iState: 'unsettled',
100
87
  }
101
88
  },
89
+ },
90
+ filter,
91
+ }, { forUpdate: true });
92
+ const now = Date.now();
93
+ for (const plan of settlePlans) {
94
+ const { settlement$plan: settlements, } = plan;
95
+ assert(settlements && settlements.length > 0);
96
+ //关联的settlement均执行settle并生成accountOper
97
+ for (const settlement of settlements) {
98
+ const { id: settlementId, price: settlementPrice, accountId, } = settlement;
99
+ await context.operate('settlement', {
100
+ id: await generateNewIdAsync(),
101
+ action: 'settle',
102
+ data: {
103
+ settledAt: now,
104
+ accountOper$entity: [{
105
+ id: await generateNewIdAsync(),
106
+ action: 'create',
107
+ data: {
108
+ id: await generateNewIdAsync(),
109
+ totalPlus: settlementPrice,
110
+ availPlus: settlementPrice,
111
+ accountId: accountId,
112
+ type: 'settle',
113
+ }
114
+ }]
115
+ },
116
+ filter: {
117
+ id: settlementId,
118
+ }
119
+ }, option);
120
+ }
121
+ }
122
+ //为settlePlan赋上settledAt
123
+ if (data instanceof Array) {
124
+ for (const d of data) {
125
+ d.settledAt = now;
126
+ }
127
+ }
128
+ else {
129
+ data.settledAt = now;
130
+ }
131
+ return settlePlans.length;
132
+ },
133
+ },
134
+ {
135
+ name: '当settlePlan执行settle后,更新order的settled',
136
+ entity: 'settlePlan',
137
+ action: 'settle',
138
+ when: 'after',
139
+ asRoot: true,
140
+ fn: async ({ operation }, context, option) => {
141
+ const { filter } = operation;
142
+ const settlePlans = await context.select('settlePlan', {
143
+ data: {
144
+ id: 1,
145
+ price: 1,
102
146
  orderId: 1,
103
147
  order: {
104
148
  id: 1,
@@ -109,43 +153,14 @@ const triggers = [
109
153
  },
110
154
  filter,
111
155
  }, { forUpdate: true });
112
- const now = Date.now();
156
+ assert(settlePlans.length > 0, '未查询到settlePlan,请检查filter');
113
157
  const planArr = groupBy(settlePlans, 'orderId');
114
158
  const orderIds = Object.keys(planArr);
115
159
  for (const orderId of orderIds) {
116
160
  const plans = planArr[orderId];
117
161
  const order = planArr[orderId][0].order;
118
162
  let planPrice = 0;
119
- for (const plan of plans) {
120
- const { id, price, settlement$plan: settlements, } = plan;
121
- assert(settlements && settlements.length > 0);
122
- //关联的settlement均执行settle并生成accountOper
123
- for (const settlement of settlements) {
124
- const { id: settlementId, price: settlementPrice, accountId, } = settlement;
125
- await context.operate('settlement', {
126
- id: await generateNewIdAsync(),
127
- action: 'settle',
128
- data: {
129
- settledAt: now,
130
- accountOper$entity: [{
131
- id: await generateNewIdAsync(),
132
- action: 'create',
133
- data: {
134
- id: await generateNewIdAsync(),
135
- totalPlus: settlementPrice,
136
- availPlus: settlementPrice,
137
- accountId: accountId,
138
- type: 'settle',
139
- }
140
- }]
141
- },
142
- filter: {
143
- id: settlementId,
144
- }
145
- }, option);
146
- }
147
- planPrice += price;
148
- }
163
+ plans.forEach((plan) => planPrice += plan.price);
149
164
  //更新order的settled
150
165
  const newSettledPrice = order?.settled + planPrice;
151
166
  await context.operate('order', {
@@ -159,18 +174,18 @@ const triggers = [
159
174
  }
160
175
  }, option);
161
176
  }
162
- return settlePlans.length;
177
+ return orderIds.length;
163
178
  },
164
179
  },
165
180
  {
166
- name: '当settlePlan执行close时将关联的settlement执行close,并更新order的settlePlanned',
181
+ name: '当settlePlan执行close时将关联的settlement执行close',
167
182
  entity: 'settlePlan',
168
183
  action: 'close',
169
184
  when: 'before',
170
185
  asRoot: true,
171
186
  priority: 99,
172
187
  fn: async ({ operation }, context, option) => {
173
- const { filter } = operation;
188
+ const { filter, data } = operation;
174
189
  const settlePlans = await context.select('settlePlan', {
175
190
  data: {
176
191
  id: 1,
@@ -187,6 +202,52 @@ const triggers = [
187
202
  iState: 'unsettled',
188
203
  }
189
204
  },
205
+ },
206
+ filter,
207
+ }, { forUpdate: true });
208
+ const now = Date.now();
209
+ for (const plan of settlePlans) {
210
+ const { settlement$plan: settlements, } = plan;
211
+ assert(settlements && settlements.length > 0);
212
+ //关联的settlement均执行close
213
+ const settlementIds = settlements.map((ele) => ele.id);
214
+ await context.operate('settlement', {
215
+ id: await generateNewIdAsync(),
216
+ action: 'close',
217
+ data: {
218
+ closedAt: now,
219
+ },
220
+ filter: {
221
+ id: {
222
+ $in: settlementIds,
223
+ },
224
+ }
225
+ }, option);
226
+ }
227
+ //为settlePlan赋上closedAt
228
+ if (data instanceof Array) {
229
+ for (const d of data) {
230
+ d.closedAt = now;
231
+ }
232
+ }
233
+ else {
234
+ data.closedAt = now;
235
+ }
236
+ return settlePlans.length;
237
+ },
238
+ },
239
+ {
240
+ name: '当settlePlan执行close后,并更新order的settlePlanned',
241
+ entity: 'settlePlan',
242
+ action: 'close',
243
+ when: 'after',
244
+ asRoot: true,
245
+ fn: async ({ operation }, context, option) => {
246
+ const { filter } = operation;
247
+ const settlePlans = await context.select('settlePlan', {
248
+ data: {
249
+ id: 1,
250
+ price: 1,
190
251
  orderId: 1,
191
252
  order: {
192
253
  id: 1,
@@ -195,33 +256,14 @@ const triggers = [
195
256
  },
196
257
  filter,
197
258
  }, { forUpdate: true });
198
- const now = Date.now();
259
+ assert(settlePlans.length > 0, '未查询到settlePlan,请检查filter');
199
260
  const planArr = groupBy(settlePlans, 'orderId');
200
261
  const orderIds = Object.keys(planArr);
201
262
  for (const orderId of orderIds) {
202
263
  const plans = planArr[orderId];
203
264
  const order = planArr[orderId][0].order;
204
265
  let planPrice = 0;
205
- for (const plan of plans) {
206
- const { id, price, settlement$plan: settlements, } = plan;
207
- assert(settlements && settlements.length > 0);
208
- //关联的settlement均执行close
209
- const settlementIds = settlements.map((ele) => ele.id);
210
- await context.operate('settlement', {
211
- id: await generateNewIdAsync(),
212
- action: 'close',
213
- data: {
214
- closedAt: now,
215
- },
216
- filter: {
217
- id: {
218
- $in: settlementIds,
219
- },
220
- iState: 'unsettled'
221
- }
222
- }, option);
223
- planPrice += price;
224
- }
266
+ plans.forEach((plan) => planPrice += plan.price);
225
267
  //更新order的settlePlanned
226
268
  const newSettlePlanned = order?.settlePlanned - planPrice;
227
269
  await context.operate('order', {
@@ -235,7 +277,7 @@ const triggers = [
235
277
  }
236
278
  }, option);
237
279
  }
238
- return settlePlans.length;
280
+ return orderIds.length;
239
281
  },
240
282
  },
241
283
  ];
@@ -1,8 +1,10 @@
1
+ import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
1
2
  const watchers = [
2
3
  {
4
+ //settle 更新order时filter不允许携带iState故使用fn方式
3
5
  name: '当settlePlan达到结算时间时,结算settlePlan',
4
6
  entity: 'settlePlan',
5
- filter: () => {
7
+ filter: async () => {
6
8
  const now = Date.now();
7
9
  return {
8
10
  iState: 'unsettled',
@@ -11,8 +13,25 @@ const watchers = [
11
13
  },
12
14
  };
13
15
  },
14
- action: 'settle',
15
- actionData: {},
16
+ projection: {
17
+ id: 1,
18
+ },
19
+ fn: async (context, data) => {
20
+ if (data.length > 0) {
21
+ const ids = data.map((ele) => ele.id);
22
+ await context.operate('settlePlan', {
23
+ id: await generateNewIdAsync(),
24
+ action: 'settle',
25
+ data: {},
26
+ filter: {
27
+ id: {
28
+ $in: ids,
29
+ }
30
+ }
31
+ }, {});
32
+ }
33
+ return context.opResult;
34
+ }
16
35
  }
17
36
  ];
18
37
  export default watchers;
@@ -6,6 +6,8 @@ export interface Schema extends EntityShape {
6
6
  when?: Datetime;
7
7
  order: Order;
8
8
  price: Price;
9
+ settledAt?: Datetime;
10
+ closedAt?: Datetime;
9
11
  }
10
12
  type IState = 'unsettled' | 'settled' | 'closed';
11
13
  type IAction = 'settle' | 'close';
@@ -18,6 +18,8 @@ exports.entityDesc = {
18
18
  order: '订单',
19
19
  price: '结算金额',
20
20
  iState: '结算计划状态',
21
+ settledAt: '结算时间',
22
+ closedAt: '关闭时间'
21
23
  },
22
24
  action: {
23
25
  settle: '结算',
@@ -16,6 +16,12 @@ exports.desc = {
16
16
  notNull: true,
17
17
  type: "money"
18
18
  },
19
+ settledAt: {
20
+ type: "datetime"
21
+ },
22
+ closedAt: {
23
+ type: "datetime"
24
+ },
19
25
  iState: {
20
26
  type: "enum",
21
27
  enumeration: ["unsettled", "settled", "closed"]
@@ -7,6 +7,8 @@ export type OpSchema = EntityShape & {
7
7
  when?: Datetime | null;
8
8
  orderId: ForeignKey<"order">;
9
9
  price: Price;
10
+ settledAt?: Datetime | null;
11
+ closedAt?: Datetime | null;
10
12
  iState?: IState | null;
11
13
  } & {
12
14
  [A in ExpressionKey]?: any;
@@ -20,6 +22,8 @@ export type OpFilter = {
20
22
  when: Q_DateValue;
21
23
  orderId: Q_StringValue;
22
24
  price: Q_NumberValue;
25
+ settledAt: Q_DateValue;
26
+ closedAt: Q_DateValue;
23
27
  iState: Q_EnumValue<IState>;
24
28
  } & ExprOp<OpAttr | string>;
25
29
  export type OpProjection = {
@@ -32,6 +36,8 @@ export type OpProjection = {
32
36
  when?: number;
33
37
  orderId?: number;
34
38
  price?: number;
39
+ settledAt?: number;
40
+ closedAt?: number;
35
41
  iState?: number;
36
42
  } & Partial<ExprOp<OpAttr | string>>;
37
43
  export type OpSortAttr = Partial<{
@@ -42,6 +48,8 @@ export type OpSortAttr = Partial<{
42
48
  when: number;
43
49
  orderId: number;
44
50
  price: number;
51
+ settledAt: number;
52
+ closedAt: number;
45
53
  iState: number;
46
54
  [k: string]: any;
47
55
  } | ExprOp<OpAttr | string>>;
@@ -4,7 +4,9 @@
4
4
  "when": "结算时间",
5
5
  "order": "订单",
6
6
  "price": "结算金额",
7
- "iState": "结算计划状态"
7
+ "iState": "结算计划状态",
8
+ "settledAt": "结算时间",
9
+ "closedAt": "关闭时间"
8
10
  },
9
11
  "action": {
10
12
  "settle": "结算",
@@ -6,62 +6,50 @@ const assert_1 = tslib_1.__importDefault(require("assert"));
6
6
  const lodash_1 = require("oak-domain/lib/utils/lodash");
7
7
  const triggers = [
8
8
  {
9
- name: '当settlePlan创建时更新关联order的settlePlanned',
9
+ name: '当settlePlan创建后,更新关联order的settlePlanned',
10
10
  entity: 'settlePlan',
11
11
  action: 'create',
12
- when: 'before',
12
+ when: 'after',
13
13
  asRoot: true,
14
- priority: 99,
14
+ // priority: 99,
15
15
  fn: async ({ operation }, context, option) => {
16
16
  const { data } = operation;
17
- let count = 0;
17
+ let ids = [];
18
18
  if (data instanceof Array) {
19
- const planArr = (0, lodash_1.groupBy)(data, 'orderId');
20
- const orderIds = Object.keys(planArr);
21
- for (const orderId of orderIds) {
22
- const [order] = await context.select('order', {
23
- data: {
24
- id: 1,
25
- paid: 1,
26
- refunded: 1,
27
- settlePlanned: 1,
28
- },
29
- filter: {
30
- id: orderId,
31
- }
32
- }, { forUpdate: true });
33
- const { id, paid, refunded, settlePlanned } = order;
34
- let planPrice = 0;
35
- const plans = planArr[orderId];
36
- plans.forEach((plan) => planPrice += plan.price);
37
- const newSettlePlanned = planPrice + settlePlanned;
38
- await context.operate('order', {
39
- id: await (0, uuid_1.generateNewIdAsync)(),
40
- action: 'update',
41
- data: {
42
- settlePlanned: newSettlePlanned,
43
- },
44
- filter: {
45
- id,
46
- }
47
- }, option);
48
- }
49
- count += data.length;
19
+ ids = data.map((ele) => ele.id);
50
20
  }
51
21
  else {
22
+ ids = [data.id];
23
+ }
24
+ const settlePlans = await context.select('settlePlan', {
25
+ data: {
26
+ id: 1,
27
+ orderId: 1,
28
+ price: 1,
29
+ },
30
+ filter: {
31
+ id: {
32
+ $in: ids,
33
+ }
34
+ },
35
+ }, { forUpdate: true });
36
+ const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId');
37
+ const orderIds = Object.keys(planArr);
38
+ for (const orderId of orderIds) {
52
39
  const [order] = await context.select('order', {
53
40
  data: {
54
41
  id: 1,
55
- paid: 1,
56
- refunded: 1,
57
42
  settlePlanned: 1,
58
43
  },
59
44
  filter: {
60
- id: data.orderId,
45
+ id: orderId,
61
46
  }
62
47
  }, { forUpdate: true });
63
- const { id, paid, refunded, settlePlanned } = order;
64
- const newSettlePlanned = data.price + settlePlanned;
48
+ const { id, settlePlanned } = order;
49
+ let planPrice = 0;
50
+ const plans = planArr[orderId];
51
+ plans.forEach((plan) => planPrice += plan.price);
52
+ const newSettlePlanned = planPrice + settlePlanned;
65
53
  await context.operate('order', {
66
54
  id: await (0, uuid_1.generateNewIdAsync)(),
67
55
  action: 'update',
@@ -72,20 +60,19 @@ const triggers = [
72
60
  id,
73
61
  }
74
62
  }, option);
75
- count++;
76
63
  }
77
- return count;
64
+ return ids.length;
78
65
  },
79
66
  },
80
67
  {
81
- name: '当settlePlan执行settle时将关联的settlement执行settle,并更新order的settled',
68
+ name: '当settlePlan执行settle时将关联的settlement执行settle',
82
69
  entity: 'settlePlan',
83
70
  action: 'settle',
84
71
  when: 'before',
85
72
  asRoot: true,
86
73
  priority: 99,
87
74
  fn: async ({ operation }, context, option) => {
88
- const { filter } = operation;
75
+ const { filter, data } = operation;
89
76
  const settlePlans = await context.select('settlePlan', {
90
77
  data: {
91
78
  id: 1,
@@ -102,6 +89,63 @@ const triggers = [
102
89
  iState: 'unsettled',
103
90
  }
104
91
  },
92
+ },
93
+ filter,
94
+ }, { forUpdate: true });
95
+ const now = Date.now();
96
+ for (const plan of settlePlans) {
97
+ const { settlement$plan: settlements, } = plan;
98
+ (0, assert_1.default)(settlements && settlements.length > 0);
99
+ //关联的settlement均执行settle并生成accountOper
100
+ for (const settlement of settlements) {
101
+ const { id: settlementId, price: settlementPrice, accountId, } = settlement;
102
+ await context.operate('settlement', {
103
+ id: await (0, uuid_1.generateNewIdAsync)(),
104
+ action: 'settle',
105
+ data: {
106
+ settledAt: now,
107
+ accountOper$entity: [{
108
+ id: await (0, uuid_1.generateNewIdAsync)(),
109
+ action: 'create',
110
+ data: {
111
+ id: await (0, uuid_1.generateNewIdAsync)(),
112
+ totalPlus: settlementPrice,
113
+ availPlus: settlementPrice,
114
+ accountId: accountId,
115
+ type: 'settle',
116
+ }
117
+ }]
118
+ },
119
+ filter: {
120
+ id: settlementId,
121
+ }
122
+ }, option);
123
+ }
124
+ }
125
+ //为settlePlan赋上settledAt
126
+ if (data instanceof Array) {
127
+ for (const d of data) {
128
+ d.settledAt = now;
129
+ }
130
+ }
131
+ else {
132
+ data.settledAt = now;
133
+ }
134
+ return settlePlans.length;
135
+ },
136
+ },
137
+ {
138
+ name: '当settlePlan执行settle后,更新order的settled',
139
+ entity: 'settlePlan',
140
+ action: 'settle',
141
+ when: 'after',
142
+ asRoot: true,
143
+ fn: async ({ operation }, context, option) => {
144
+ const { filter } = operation;
145
+ const settlePlans = await context.select('settlePlan', {
146
+ data: {
147
+ id: 1,
148
+ price: 1,
105
149
  orderId: 1,
106
150
  order: {
107
151
  id: 1,
@@ -112,43 +156,14 @@ const triggers = [
112
156
  },
113
157
  filter,
114
158
  }, { forUpdate: true });
115
- const now = Date.now();
159
+ (0, assert_1.default)(settlePlans.length > 0, '未查询到settlePlan,请检查filter');
116
160
  const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId');
117
161
  const orderIds = Object.keys(planArr);
118
162
  for (const orderId of orderIds) {
119
163
  const plans = planArr[orderId];
120
164
  const order = planArr[orderId][0].order;
121
165
  let planPrice = 0;
122
- for (const plan of plans) {
123
- const { id, price, settlement$plan: settlements, } = plan;
124
- (0, assert_1.default)(settlements && settlements.length > 0);
125
- //关联的settlement均执行settle并生成accountOper
126
- for (const settlement of settlements) {
127
- const { id: settlementId, price: settlementPrice, accountId, } = settlement;
128
- await context.operate('settlement', {
129
- id: await (0, uuid_1.generateNewIdAsync)(),
130
- action: 'settle',
131
- data: {
132
- settledAt: now,
133
- accountOper$entity: [{
134
- id: await (0, uuid_1.generateNewIdAsync)(),
135
- action: 'create',
136
- data: {
137
- id: await (0, uuid_1.generateNewIdAsync)(),
138
- totalPlus: settlementPrice,
139
- availPlus: settlementPrice,
140
- accountId: accountId,
141
- type: 'settle',
142
- }
143
- }]
144
- },
145
- filter: {
146
- id: settlementId,
147
- }
148
- }, option);
149
- }
150
- planPrice += price;
151
- }
166
+ plans.forEach((plan) => planPrice += plan.price);
152
167
  //更新order的settled
153
168
  const newSettledPrice = order?.settled + planPrice;
154
169
  await context.operate('order', {
@@ -162,18 +177,18 @@ const triggers = [
162
177
  }
163
178
  }, option);
164
179
  }
165
- return settlePlans.length;
180
+ return orderIds.length;
166
181
  },
167
182
  },
168
183
  {
169
- name: '当settlePlan执行close时将关联的settlement执行close,并更新order的settlePlanned',
184
+ name: '当settlePlan执行close时将关联的settlement执行close',
170
185
  entity: 'settlePlan',
171
186
  action: 'close',
172
187
  when: 'before',
173
188
  asRoot: true,
174
189
  priority: 99,
175
190
  fn: async ({ operation }, context, option) => {
176
- const { filter } = operation;
191
+ const { filter, data } = operation;
177
192
  const settlePlans = await context.select('settlePlan', {
178
193
  data: {
179
194
  id: 1,
@@ -190,6 +205,52 @@ const triggers = [
190
205
  iState: 'unsettled',
191
206
  }
192
207
  },
208
+ },
209
+ filter,
210
+ }, { forUpdate: true });
211
+ const now = Date.now();
212
+ for (const plan of settlePlans) {
213
+ const { settlement$plan: settlements, } = plan;
214
+ (0, assert_1.default)(settlements && settlements.length > 0);
215
+ //关联的settlement均执行close
216
+ const settlementIds = settlements.map((ele) => ele.id);
217
+ await context.operate('settlement', {
218
+ id: await (0, uuid_1.generateNewIdAsync)(),
219
+ action: 'close',
220
+ data: {
221
+ closedAt: now,
222
+ },
223
+ filter: {
224
+ id: {
225
+ $in: settlementIds,
226
+ },
227
+ }
228
+ }, option);
229
+ }
230
+ //为settlePlan赋上closedAt
231
+ if (data instanceof Array) {
232
+ for (const d of data) {
233
+ d.closedAt = now;
234
+ }
235
+ }
236
+ else {
237
+ data.closedAt = now;
238
+ }
239
+ return settlePlans.length;
240
+ },
241
+ },
242
+ {
243
+ name: '当settlePlan执行close后,并更新order的settlePlanned',
244
+ entity: 'settlePlan',
245
+ action: 'close',
246
+ when: 'after',
247
+ asRoot: true,
248
+ fn: async ({ operation }, context, option) => {
249
+ const { filter } = operation;
250
+ const settlePlans = await context.select('settlePlan', {
251
+ data: {
252
+ id: 1,
253
+ price: 1,
193
254
  orderId: 1,
194
255
  order: {
195
256
  id: 1,
@@ -198,33 +259,14 @@ const triggers = [
198
259
  },
199
260
  filter,
200
261
  }, { forUpdate: true });
201
- const now = Date.now();
262
+ (0, assert_1.default)(settlePlans.length > 0, '未查询到settlePlan,请检查filter');
202
263
  const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId');
203
264
  const orderIds = Object.keys(planArr);
204
265
  for (const orderId of orderIds) {
205
266
  const plans = planArr[orderId];
206
267
  const order = planArr[orderId][0].order;
207
268
  let planPrice = 0;
208
- for (const plan of plans) {
209
- const { id, price, settlement$plan: settlements, } = plan;
210
- (0, assert_1.default)(settlements && settlements.length > 0);
211
- //关联的settlement均执行close
212
- const settlementIds = settlements.map((ele) => ele.id);
213
- await context.operate('settlement', {
214
- id: await (0, uuid_1.generateNewIdAsync)(),
215
- action: 'close',
216
- data: {
217
- closedAt: now,
218
- },
219
- filter: {
220
- id: {
221
- $in: settlementIds,
222
- },
223
- iState: 'unsettled'
224
- }
225
- }, option);
226
- planPrice += price;
227
- }
269
+ plans.forEach((plan) => planPrice += plan.price);
228
270
  //更新order的settlePlanned
229
271
  const newSettlePlanned = order?.settlePlanned - planPrice;
230
272
  await context.operate('order', {
@@ -238,7 +280,7 @@ const triggers = [
238
280
  }
239
281
  }, option);
240
282
  }
241
- return settlePlans.length;
283
+ return orderIds.length;
242
284
  },
243
285
  },
244
286
  ];
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const uuid_1 = require("oak-domain/lib/utils/uuid");
3
4
  const watchers = [
4
5
  {
6
+ //settle 更新order时filter不允许携带iState故使用fn方式
5
7
  name: '当settlePlan达到结算时间时,结算settlePlan',
6
8
  entity: 'settlePlan',
7
- filter: () => {
9
+ filter: async () => {
8
10
  const now = Date.now();
9
11
  return {
10
12
  iState: 'unsettled',
@@ -13,8 +15,25 @@ const watchers = [
13
15
  },
14
16
  };
15
17
  },
16
- action: 'settle',
17
- actionData: {},
18
+ projection: {
19
+ id: 1,
20
+ },
21
+ fn: async (context, data) => {
22
+ if (data.length > 0) {
23
+ const ids = data.map((ele) => ele.id);
24
+ await context.operate('settlePlan', {
25
+ id: await (0, uuid_1.generateNewIdAsync)(),
26
+ action: 'settle',
27
+ data: {},
28
+ filter: {
29
+ id: {
30
+ $in: ids,
31
+ }
32
+ }
33
+ }, {});
34
+ }
35
+ return context.opResult;
36
+ }
18
37
  }
19
38
  ];
20
39
  exports.default = watchers;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-pay-business",
3
- "version": "3.1.2",
3
+ "version": "3.1.3",
4
4
  "description": "",
5
5
  "files": [
6
6
  "lib/**/*",
@@ -10,6 +10,8 @@ export interface Schema extends EntityShape {
10
10
  when?: Datetime;
11
11
  order: Order;
12
12
  price: Price;
13
+ settledAt?: Datetime;
14
+ closedAt?: Datetime;
13
15
  };
14
16
 
15
17
  type IState = 'unsettled' | 'settled' | 'closed';
@@ -36,6 +38,8 @@ export const entityDesc: EntityDesc<Schema, Action, '', {
36
38
  order: '订单',
37
39
  price: '结算金额',
38
40
  iState: '结算计划状态',
41
+ settledAt: '结算时间',
42
+ closedAt: '关闭时间'
39
43
  },
40
44
  action: {
41
45
  settle: '结算',