@chevre/domain 21.8.0-alpha.58 → 21.8.0-alpha.59

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.
@@ -0,0 +1,23 @@
1
+ // tslint:disable:no-console
2
+ import * as mongoose from 'mongoose';
3
+
4
+ import { chevre } from '../../../lib/index';
5
+
6
+ async function main() {
7
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
8
+
9
+ const offerCatalogRepo = new chevre.repository.OfferCatalog(mongoose.connection);
10
+
11
+ const catalog = await offerCatalogRepo.findItemListElementById(
12
+ {
13
+ id: '0001',
14
+ project: { id: String(process.env.PROJECT_ID) }
15
+ }
16
+ );
17
+ console.log(catalog);
18
+ console.log(catalog.itemListElement.length);
19
+ }
20
+
21
+ main()
22
+ .then(console.log)
23
+ .catch(console.error);
@@ -23,5 +23,40 @@ export declare class MongoRepository {
23
23
  };
24
24
  count(params: Omit<factory.unitPriceOffer.ISearchConditions, 'limit' | 'page' | 'sort'>): Promise<number>;
25
25
  search(params: factory.unitPriceOffer.ISearchConditions, projection?: IProjection): Promise<factory.aggregateOffer.IAggregateOffer[]>;
26
+ pushIncludedInDataCatalog(params: {
27
+ project: {
28
+ id: string;
29
+ };
30
+ id: {
31
+ $in: string[];
32
+ };
33
+ $push: {
34
+ includedInDataCatalog: {
35
+ $each: {
36
+ id: string;
37
+ }[];
38
+ };
39
+ };
40
+ }): Promise<void>;
41
+ /**
42
+ * 含まれるカタログからカタログを除外する
43
+ */
44
+ pullIncludedInDataCatalog(params: {
45
+ project: {
46
+ id: string;
47
+ };
48
+ id?: {
49
+ $nin?: string[];
50
+ };
51
+ $pull: {
52
+ includedInDataCatalog: {
53
+ $elemMatch: {
54
+ id: {
55
+ $eq: string;
56
+ };
57
+ };
58
+ };
59
+ };
60
+ }): Promise<import("mongodb").UpdateResult>;
26
61
  }
27
62
  export {};
@@ -477,5 +477,58 @@ class MongoRepository {
477
477
  return aggregate.exec();
478
478
  });
479
479
  }
480
+ pushIncludedInDataCatalog(params) {
481
+ return __awaiter(this, void 0, void 0, function* () {
482
+ if (!Array.isArray(params.id.$in) || params.id.$in.length === 0) {
483
+ return;
484
+ }
485
+ const pushIncludedInDataCatalogIds = params.$push.includedInDataCatalog.$each.map((element) => element.id);
486
+ if (pushIncludedInDataCatalogIds.length === 0) {
487
+ return;
488
+ }
489
+ const newIncludedInDataCatalogs = pushIncludedInDataCatalogIds.map((catalogId) => {
490
+ return {
491
+ id: catalogId,
492
+ typeOf: 'OfferCatalog'
493
+ };
494
+ });
495
+ yield this.aggregateOfferModel.updateMany({
496
+ 'project.id': { $eq: params.project.id },
497
+ _id: { $in: params.id.$in },
498
+ // includedInDataCatalogのユニークネスを保証する
499
+ 'includedInDataCatalog.id': { $nin: pushIncludedInDataCatalogIds }
500
+ },
501
+ // { $addToSet: { includedInDataCatalog: { $each: newIncludedInDataCatalogs } } }
502
+ {
503
+ $push: {
504
+ includedInDataCatalog: {
505
+ $each: newIncludedInDataCatalogs
506
+ // $slice: params.$push.includedInDataCatalog.$slice
507
+ }
508
+ }
509
+ })
510
+ .exec();
511
+ });
512
+ }
513
+ /**
514
+ * 含まれるカタログからカタログを除外する
515
+ */
516
+ pullIncludedInDataCatalog(params) {
517
+ var _a;
518
+ return __awaiter(this, void 0, void 0, function* () {
519
+ const idNin = (_a = params.id) === null || _a === void 0 ? void 0 : _a.$nin;
520
+ return this.aggregateOfferModel.updateMany(Object.assign({ 'project.id': { $eq: params.project.id }, 'includedInDataCatalog.id': {
521
+ $exists: true,
522
+ $eq: params.$pull.includedInDataCatalog.$elemMatch.id.$eq
523
+ } }, (Array.isArray(idNin)) ? { _id: { $nin: idNin } } : undefined), {
524
+ $pull: {
525
+ includedInDataCatalog: {
526
+ id: { $eq: params.$pull.includedInDataCatalog.$elemMatch.id.$eq }
527
+ }
528
+ }
529
+ })
530
+ .exec();
531
+ });
532
+ }
480
533
  }
481
534
  exports.MongoRepository = MongoRepository;
@@ -52,16 +52,19 @@ declare const schema: Schema<any, import("mongoose").Model<any, any, any, any, a
52
52
  };
53
53
  }, {
54
54
  offers: any[];
55
+ includedInDataCatalog: any[];
55
56
  _id?: string | undefined;
56
57
  typeOf?: string | undefined;
57
58
  project?: any;
58
59
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
59
60
  offers: any[];
61
+ includedInDataCatalog: any[];
60
62
  _id?: string | undefined;
61
63
  typeOf?: string | undefined;
62
64
  project?: any;
63
65
  }>> & Omit<import("mongoose").FlatRecord<{
64
66
  offers: any[];
67
+ includedInDataCatalog: any[];
65
68
  _id?: string | undefined;
66
69
  typeOf?: string | undefined;
67
70
  project?: any;
@@ -12,7 +12,8 @@ const schema = new mongoose_1.Schema({
12
12
  project: mongoose_1.SchemaTypes.Mixed,
13
13
  _id: String,
14
14
  typeOf: String,
15
- offers: [mongoose_1.SchemaTypes.Mixed]
15
+ offers: [mongoose_1.SchemaTypes.Mixed],
16
+ includedInDataCatalog: [mongoose_1.SchemaTypes.Mixed]
16
17
  }, {
17
18
  collection: 'aggregateOffers',
18
19
  id: true,
@@ -69,6 +69,12 @@ export declare class MongoRepository {
69
69
  search(params: factory.offerCatalog.ISearchConditions): Promise<(Omit<factory.offerCatalog.IOfferCatalog, 'itemListElement'> & {
70
70
  numberOfItems?: number;
71
71
  })[]>;
72
+ findItemListElementById(params: {
73
+ id: string;
74
+ project: {
75
+ id: string;
76
+ };
77
+ }): Promise<Pick<factory.offerCatalog.IOfferCatalog, 'itemListElement'>>;
72
78
  deleteById(params: {
73
79
  id: string;
74
80
  }): Promise<void>;
@@ -240,6 +240,19 @@ class MongoRepository {
240
240
  return aggregate.exec();
241
241
  });
242
242
  }
243
+ findItemListElementById(params) {
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ const doc = yield this.offerCatalogModel.findOne({
246
+ 'project.id': { $eq: params.project.id },
247
+ _id: { $eq: params.id }
248
+ }, { 'itemListElement.id': 1 })
249
+ .exec();
250
+ if (doc === null) {
251
+ throw new factory.errors.NotFound(this.offerCatalogModel.modelName);
252
+ }
253
+ return doc.toObject();
254
+ });
255
+ }
243
256
  deleteById(params) {
244
257
  return __awaiter(this, void 0, void 0, function* () {
245
258
  yield this.offerCatalogModel.findOneAndRemove({
@@ -1,6 +1,7 @@
1
1
  import * as factory from '../../../factory';
2
2
  import { MongoRepository as AccountTitleRepo } from '../../../repo/accountTitle';
3
3
  import { MongoRepository as ActionRepo } from '../../../repo/action';
4
+ import { MongoRepository as AggregateOfferRepo } from '../../../repo/aggregateOffer';
4
5
  import { MongoRepository as CategoryCodeRepo } from '../../../repo/categoryCode';
5
6
  import { MongoRepository as CreativeWorkRepo } from '../../../repo/creativeWork';
6
7
  import { MongoRepository as EventRepo } from '../../../repo/event';
@@ -15,6 +16,7 @@ import { MongoRepository as TaskRepo } from '../../../repo/task';
15
16
  export declare function onResourceDeleted(params: factory.task.onResourceUpdated.IData): (repos: {
16
17
  accountTitle: AccountTitleRepo;
17
18
  action: ActionRepo;
19
+ aggregateOffer: AggregateOfferRepo;
18
20
  categoryCode: CategoryCodeRepo;
19
21
  creativeWork: CreativeWorkRepo;
20
22
  event: EventRepo;
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.onResourceDeleted = void 0;
13
13
  const factory = require("../../../factory");
14
+ const syncOfferCatalog_1 = require("./syncOfferCatalog");
14
15
  function onResourceDeleted(params) {
15
16
  return (repos) => __awaiter(this, void 0, void 0, function* () {
16
17
  const isDeleted = params.isDeleted === true;
@@ -52,6 +53,12 @@ function onResourceDeleted(params) {
52
53
  project: { id: params.project.id },
53
54
  ids: params.id
54
55
  })(repos);
56
+ yield (0, syncOfferCatalog_1.syncOfferCatalog)({
57
+ project: { id: params.project.id },
58
+ ids: params.id,
59
+ typeOf: params.typeOf,
60
+ isDeleted: true
61
+ })(repos);
55
62
  break;
56
63
  case factory.product.ProductType.EventService:
57
64
  case factory.product.ProductType.Product:
@@ -0,0 +1,17 @@
1
+ import * as factory from '../../../factory';
2
+ import { MongoRepository as AggregateOfferRepo } from '../../../repo/aggregateOffer';
3
+ import { MongoRepository as OfferCatalogRepo } from '../../../repo/offerCatalog';
4
+ /**
5
+ * カタログのitemListElementを集計オファーへ同期する
6
+ */
7
+ export declare function syncOfferCatalog(params: {
8
+ project: {
9
+ id: string;
10
+ };
11
+ ids: string[];
12
+ typeOf: factory.task.onResourceUpdated.OfferCatalogType;
13
+ isDeleted: boolean;
14
+ }): (repos: {
15
+ aggregateOffer: AggregateOfferRepo;
16
+ offerCatalog: OfferCatalogRepo;
17
+ }) => Promise<void>;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.syncOfferCatalog = void 0;
13
+ /**
14
+ * カタログのitemListElementを集計オファーへ同期する
15
+ */
16
+ function syncOfferCatalog(params) {
17
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
18
+ // if (params.ids.length !== 1) {
19
+ // throw new factory.errors.Argument('id', 'id.length must be 1');
20
+ // }
21
+ if (params.isDeleted) {
22
+ for (const offerCatalogId of params.ids) {
23
+ // カタログを含む全集計オファーからカタログを除外
24
+ yield repos.aggregateOffer.pullIncludedInDataCatalog({
25
+ project: { id: params.project.id },
26
+ $pull: {
27
+ includedInDataCatalog: { $elemMatch: { id: { $eq: offerCatalogId } } }
28
+ }
29
+ });
30
+ }
31
+ }
32
+ else {
33
+ for (const offerCatalogId of params.ids) {
34
+ // 集計オファーIDリストを検索
35
+ const { itemListElement } = yield repos.offerCatalog.findItemListElementById({
36
+ id: offerCatalogId,
37
+ project: { id: params.project.id }
38
+ });
39
+ // tslint:disable-next-line:no-console
40
+ console.log('onOfferCatalog', offerCatalogId, ' updated.', itemListElement.length, 'itemListElement found.', itemListElement);
41
+ const aggregateOfferIds = itemListElement.map((element) => element.id);
42
+ // カタログを含み、かつ、新しいカタログに含まれない集計オファーからカタログを除外
43
+ yield repos.aggregateOffer.pullIncludedInDataCatalog(Object.assign({ project: { id: params.project.id }, $pull: {
44
+ includedInDataCatalog: { $elemMatch: { id: { $eq: offerCatalogId } } }
45
+ } }, (aggregateOfferIds.length > 0) ? { id: { $nin: aggregateOfferIds } } : undefined));
46
+ if (aggregateOfferIds.length > 0) {
47
+ // 新しいカタログに含まれる全集計オファーにカタログを追加
48
+ yield repos.aggregateOffer.pushIncludedInDataCatalog({
49
+ project: { id: params.project.id },
50
+ id: { $in: aggregateOfferIds },
51
+ $push: {
52
+ includedInDataCatalog: { $each: [{ id: offerCatalogId }] }
53
+ }
54
+ });
55
+ }
56
+ }
57
+ }
58
+ });
59
+ }
60
+ exports.syncOfferCatalog = syncOfferCatalog;
@@ -13,6 +13,7 @@ exports.call = void 0;
13
13
  const factory = require("../../factory");
14
14
  const accountTitle_1 = require("../../repo/accountTitle");
15
15
  const action_1 = require("../../repo/action");
16
+ const aggregateOffer_1 = require("../../repo/aggregateOffer");
16
17
  const categoryCode_1 = require("../../repo/categoryCode");
17
18
  const creativeWork_1 = require("../../repo/creativeWork");
18
19
  const event_1 = require("../../repo/event");
@@ -25,6 +26,7 @@ const product_1 = require("../../repo/product");
25
26
  const productOffer_1 = require("../../repo/productOffer");
26
27
  const task_1 = require("../../repo/task");
27
28
  const onResourceDeleted_1 = require("./onResourceUpdated/onResourceDeleted");
29
+ const syncOfferCatalog_1 = require("./onResourceUpdated/syncOfferCatalog");
28
30
  const settings_1 = require("../../settings");
29
31
  const informResources = settings_1.settings.onResourceUpdated.informResource;
30
32
  /**
@@ -35,6 +37,7 @@ function call(data) {
35
37
  yield onResourceUpdated(data)({
36
38
  accountTitle: new accountTitle_1.MongoRepository(connectionSettings.connection),
37
39
  action: new action_1.MongoRepository(connectionSettings.connection),
40
+ aggregateOffer: new aggregateOffer_1.MongoRepository(connectionSettings.connection),
38
41
  categoryCode: new categoryCode_1.MongoRepository(connectionSettings.connection),
39
42
  creativeWork: new creativeWork_1.MongoRepository(connectionSettings.connection),
40
43
  event: new event_1.MongoRepository(connectionSettings.connection),
@@ -107,6 +110,15 @@ function onResourceUpdated(params) {
107
110
  typeOf: params.typeOf
108
111
  })(repos);
109
112
  break;
113
+ // カタログに対応(2023-09-12~)
114
+ case 'OfferCatalog':
115
+ yield (0, syncOfferCatalog_1.syncOfferCatalog)({
116
+ project: { id: params.project.id },
117
+ ids: params.id,
118
+ typeOf: params.typeOf,
119
+ isDeleted: false
120
+ })(repos);
121
+ break;
110
122
  default:
111
123
  // no op
112
124
  }
package/package.json CHANGED
@@ -117,5 +117,5 @@
117
117
  "postversion": "git push origin --tags",
118
118
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
119
119
  },
120
- "version": "21.8.0-alpha.58"
120
+ "version": "21.8.0-alpha.59"
121
121
  }