@chevre/domain 21.20.0-alpha.62 → 21.20.0-alpha.63

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.
@@ -1,6 +1,11 @@
1
- import type { Connection, Model } from 'mongoose';
1
+ import type { Connection, PipelineStage } from 'mongoose';
2
2
  import * as factory from '../factory';
3
+ type IMatchStage = PipelineStage.Match;
3
4
  export type IOrder4report = factory.report.accountingReport.IOrderAsMainEntity;
5
+ export interface IChildReport {
6
+ typeOf: 'Report';
7
+ mainEntity: factory.report.accountingReport.IAction;
8
+ }
4
9
  export interface IAccountingReport {
5
10
  project: {
6
11
  id: string;
@@ -14,8 +19,13 @@ export interface IAccountingReport {
14
19
  * 経理レポートリポジトリ
15
20
  */
16
21
  export declare class MongoRepository {
17
- readonly accountingReportModel: typeof Model;
22
+ private readonly accountingReportModel;
18
23
  constructor(connection: Connection);
24
+ static CREATE_MONGO_CONDITIONS(params: factory.report.accountingReport.ISearchConditions & {
25
+ seller?: {
26
+ id?: string;
27
+ };
28
+ }): IMatchStage[];
19
29
  /**
20
30
  * なければ作成する
21
31
  */
@@ -28,4 +38,16 @@ export declare class MongoRepository {
28
38
  orderNumber: string;
29
39
  };
30
40
  }): Promise<void>;
41
+ addChildReport(params: {
42
+ mainEntity: {
43
+ orderNumber: string;
44
+ };
45
+ hasPart: IChildReport;
46
+ }): Promise<void>;
47
+ search(params: factory.report.accountingReport.ISearchConditions & {
48
+ seller?: {
49
+ id?: string;
50
+ };
51
+ }): Promise<any[]>;
31
52
  }
53
+ export {};
@@ -12,6 +12,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MongoRepository = void 0;
13
13
  const accountingReport_1 = require("./mongoose/schemas/accountingReport");
14
14
  const errorHandler_1 = require("../errorHandler");
15
+ const factory = require("../factory");
16
+ const DEFAULT_SEARCH_LIMIT = 100;
15
17
  /**
16
18
  * 経理レポートリポジトリ
17
19
  */
@@ -19,6 +21,67 @@ class MongoRepository {
19
21
  constructor(connection) {
20
22
  this.accountingReportModel = connection.model(accountingReport_1.modelName, (0, accountingReport_1.createSchema)());
21
23
  }
24
+ static CREATE_MONGO_CONDITIONS(params) {
25
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
26
+ const matchStages = [];
27
+ const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
28
+ if (typeof projectIdEq === 'string') {
29
+ matchStages.push({ $match: { 'project.id': { $eq: projectIdEq } } });
30
+ }
31
+ // req.seller.idを考慮(2023-07-21~)
32
+ if (typeof ((_c = params.seller) === null || _c === void 0 ? void 0 : _c.id) === 'string') {
33
+ matchStages.push({ $match: { 'mainEntity.seller.id': { $exists: true, $eq: params.seller.id } } });
34
+ }
35
+ const orderNumberEq = (_e = (_d = params.order) === null || _d === void 0 ? void 0 : _d.orderNumber) === null || _e === void 0 ? void 0 : _e.$eq;
36
+ if (typeof orderNumberEq === 'string') {
37
+ matchStages.push({ $match: { 'mainEntity.orderNumber': { $eq: orderNumberEq } } });
38
+ }
39
+ const sellerIdEq = (_h = (_g = (_f = params.order) === null || _f === void 0 ? void 0 : _f.seller) === null || _g === void 0 ? void 0 : _g.id) === null || _h === void 0 ? void 0 : _h.$eq;
40
+ if (typeof sellerIdEq === 'string') {
41
+ matchStages.push({ $match: { 'mainEntity.seller.id': { $exists: true, $eq: sellerIdEq } } });
42
+ }
43
+ const paymentMethodIdEq = (_l = (_k = (_j = params.order) === null || _j === void 0 ? void 0 : _j.paymentMethods) === null || _k === void 0 ? void 0 : _k.paymentMethodId) === null || _l === void 0 ? void 0 : _l.$eq;
44
+ if (typeof paymentMethodIdEq === 'string') {
45
+ matchStages.push({
46
+ $match: { 'mainEntity.paymentMethods.paymentMethodId': { $exists: true, $eq: paymentMethodIdEq } }
47
+ });
48
+ }
49
+ const orderDateGte = (_o = (_m = params.order) === null || _m === void 0 ? void 0 : _m.orderDate) === null || _o === void 0 ? void 0 : _o.$gte;
50
+ if (orderDateGte instanceof Date) {
51
+ matchStages.push({
52
+ $match: { 'mainEntity.orderDate': { $gte: orderDateGte } }
53
+ });
54
+ }
55
+ const orderDateLte = (_q = (_p = params.order) === null || _p === void 0 ? void 0 : _p.orderDate) === null || _q === void 0 ? void 0 : _q.$lte;
56
+ if (orderDateLte instanceof Date) {
57
+ matchStages.push({
58
+ $match: { 'mainEntity.orderDate': { $lte: orderDateLte } }
59
+ });
60
+ }
61
+ const reservationForStartDateGte = (_v = (_u = (_t = (_s = (_r = params.order) === null || _r === void 0 ? void 0 : _r.acceptedOffers) === null || _s === void 0 ? void 0 : _s.itemOffered) === null || _t === void 0 ? void 0 : _t.reservationFor) === null || _u === void 0 ? void 0 : _u.startDate) === null || _v === void 0 ? void 0 : _v.$gte;
62
+ if (reservationForStartDateGte instanceof Date) {
63
+ matchStages.push({
64
+ $match: {
65
+ 'mainEntity.acceptedOffers.itemOffered.reservationFor.startDate': {
66
+ $exists: true,
67
+ $gte: reservationForStartDateGte
68
+ }
69
+ }
70
+ });
71
+ }
72
+ const reservationForStartDateLte = (_0 = (_z = (_y = (_x = (_w = params.order) === null || _w === void 0 ? void 0 : _w.acceptedOffers) === null || _x === void 0 ? void 0 : _x.itemOffered) === null || _y === void 0 ? void 0 : _y.reservationFor) === null || _z === void 0 ? void 0 : _z.startDate) === null || _0 === void 0 ? void 0 : _0.$lte;
73
+ if (reservationForStartDateLte instanceof Date) {
74
+ matchStages.push({
75
+ $match: {
76
+ 'mainEntity.acceptedOffers.itemOffered.reservationFor.startDate': {
77
+ $exists: true,
78
+ $lte: reservationForStartDateLte
79
+ }
80
+ }
81
+ });
82
+ }
83
+ return matchStages;
84
+ }
22
85
  /**
23
86
  * なければ作成する
24
87
  */
@@ -51,5 +114,41 @@ class MongoRepository {
51
114
  .exec();
52
115
  });
53
116
  }
117
+ addChildReport(params) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ yield this.accountingReportModel.updateOne({ 'mainEntity.orderNumber': { $eq: params.mainEntity.orderNumber } }, { $addToSet: { hasPart: params.hasPart } })
120
+ .exec();
121
+ });
122
+ }
123
+ search(params) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ const limit = (typeof params.limit === 'number') ? Math.min(params.limit, DEFAULT_SEARCH_LIMIT) : DEFAULT_SEARCH_LIMIT;
126
+ const page = (typeof params.page === 'number') ? Math.max(params.page, 1) : 1;
127
+ const unwindAcceptedOffers = params.$unwindAcceptedOffers === '1';
128
+ const matchStages = MongoRepository.CREATE_MONGO_CONDITIONS(params);
129
+ const aggregate = this.accountingReportModel.aggregate([
130
+ // pipelineの順序に注意
131
+ // @see https://docs.mongodb.com/manual/reference/operator/aggregation/sort/
132
+ { $sort: { 'mainEntity.orderDate': factory.sortType.Descending } },
133
+ { $unwind: '$hasPart' },
134
+ ...(unwindAcceptedOffers) ? [{ $unwind: '$mainEntity.acceptedOffers' }] : [],
135
+ ...matchStages,
136
+ {
137
+ $project: {
138
+ _id: 0,
139
+ mainEntity: '$hasPart.mainEntity',
140
+ isPartOf: {
141
+ mainEntity: '$mainEntity'
142
+ }
143
+ }
144
+ }
145
+ ]);
146
+ return aggregate.allowDiskUse(true)
147
+ .limit(limit * page)
148
+ .skip(limit * (page - 1))
149
+ // .setOptions({ maxTimeMS: 10000 })
150
+ .exec();
151
+ });
152
+ }
54
153
  }
55
154
  exports.MongoRepository = MongoRepository;
@@ -47,8 +47,10 @@ function onReturnFeePaid(params) {
47
47
  }
48
48
  : undefined);
49
49
  const childReport = { typeOf: 'Report', mainEntity: action4save };
50
- yield repos.accountingReport.accountingReportModel.findOneAndUpdate({ 'mainEntity.orderNumber': orderNumber }, { $addToSet: { hasPart: childReport } })
51
- .exec();
50
+ yield repos.accountingReport.addChildReport({
51
+ mainEntity: { orderNumber },
52
+ hasPart: childReport
53
+ });
52
54
  });
53
55
  }
54
56
  function onOrderPaid(params) {
@@ -67,7 +69,9 @@ function onOrderPaid(params) {
67
69
  }
68
70
  : undefined);
69
71
  const childReport = { typeOf: 'Report', mainEntity: action4save };
70
- yield repos.accountingReport.accountingReportModel.findOneAndUpdate({ 'mainEntity.orderNumber': orderNumber }, { $addToSet: { hasPart: childReport } })
71
- .exec();
72
+ yield repos.accountingReport.addChildReport({
73
+ mainEntity: { orderNumber },
74
+ hasPart: childReport
75
+ });
72
76
  });
73
77
  }
@@ -43,7 +43,9 @@ function onOrderRefunded(params) {
43
43
  }
44
44
  : undefined);
45
45
  const childReport = { typeOf: 'Report', mainEntity: action4save };
46
- yield repos.accountingReport.accountingReportModel.findOneAndUpdate({ 'mainEntity.orderNumber': orderNumber }, { $addToSet: { hasPart: childReport } })
47
- .exec();
46
+ yield repos.accountingReport.addChildReport({
47
+ mainEntity: { orderNumber },
48
+ hasPart: childReport
49
+ });
48
50
  });
49
51
  }
package/package.json CHANGED
@@ -111,5 +111,5 @@
111
111
  "postversion": "git push origin --tags",
112
112
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
113
113
  },
114
- "version": "21.20.0-alpha.62"
114
+ "version": "21.20.0-alpha.63"
115
115
  }
@@ -1,57 +0,0 @@
1
- // tslint:disable:no-console
2
- import * as moment from 'moment';
3
- import * as mongoose from 'mongoose';
4
-
5
- import { chevre } from '../../../lib/index';
6
-
7
- // const project = { id: '' };
8
- // const MASKED_PROFILE = '****';
9
-
10
- async function main() {
11
- await mongoose.connect(<string>process.env.MONGOLAB_URI);
12
-
13
- const accountingReportRepo = await chevre.repository.AccountingReport.createInstance(mongoose.connection);
14
-
15
- const cursor = await accountingReportRepo.accountingReportModel.find(
16
- {
17
- 'mainEntity.orderDate': {
18
- $lt: moment()
19
- .add(-1, 'year')
20
- .toDate()
21
- }
22
- },
23
- {
24
- // _id: 1,
25
- }
26
- )
27
- // .limit(10)
28
- .sort({ 'mainEntity.orderDate': chevre.factory.sortType.Descending })
29
- .cursor();
30
- console.log('reports found');
31
-
32
- let i = 0;
33
- let updateCount = 0;
34
- await cursor.eachAsync(async (doc) => {
35
- i += 1;
36
- const accountingReport = doc.toObject();
37
-
38
- console.log(
39
- 'deleting report...',
40
- accountingReport.id, accountingReport.mainEntity.orderNumber, accountingReport.mainEntity.orderDate, i);
41
- await accountingReportRepo.accountingReportModel.findByIdAndDelete(
42
- accountingReport.id
43
- )
44
- .exec();
45
- updateCount += 1;
46
- console.log(
47
- 'report deleted',
48
- accountingReport.id, accountingReport.mainEntity.orderNumber, accountingReport.mainEntity.orderDate, i);
49
- });
50
-
51
- console.log(i, 'reports checked');
52
- console.log(updateCount, 'reports updated');
53
- }
54
-
55
- main()
56
- .then()
57
- .catch(console.error);