@chevre/domain 23.0.0-alpha.17 → 23.0.0-alpha.19

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,100 @@
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: String(process.env.PROJECT_ID) };
8
+ const { EVENT_ID } = process.env;
9
+
10
+ async function main() {
11
+ if (typeof EVENT_ID !== 'string') {
12
+ throw new Error('environment variables required');
13
+ }
14
+
15
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
16
+
17
+ const eventSellerMakesOfferRepo = await chevre.repository.EventSellerMakesOffer.createInstance(mongoose.connection);
18
+ const availabilityEnds = moment('2025-10-26T05:20:00.000Z')
19
+ .toDate();
20
+ const availabilityStarts = moment('2025-10-23T15:00:00.000Z')
21
+ .toDate();
22
+ const validFrom = moment('2025-10-12T15:00:00.000Z')
23
+ .toDate();
24
+ const validThrough = moment('2025-10-26T05:20:00.000Z')
25
+ .toDate();
26
+ const result = await eventSellerMakesOfferRepo.updateOffersByEventId(
27
+ {
28
+ project: { id: project.id },
29
+ offers: [
30
+ {
31
+ typeOf: chevre.factory.offerType.Offer,
32
+ availableAtOrFrom: {
33
+ id: '3eo6okferrsdpfd9j2ce1iv9k7'
34
+ },
35
+ availabilityEnds,
36
+ availabilityStarts,
37
+ validFrom,
38
+ validThrough
39
+ },
40
+ {
41
+ typeOf: chevre.factory.offerType.Offer,
42
+ availableAtOrFrom: {
43
+ id: '51qbjcfr72h62m06vtv5kkhgje'
44
+ },
45
+ availabilityEnds,
46
+ availabilityStarts,
47
+ validFrom,
48
+ validThrough
49
+ },
50
+ {
51
+ typeOf: chevre.factory.offerType.Offer,
52
+ availableAtOrFrom: {
53
+ id: '5h3gs22mu9j3ok7o63uog9o9i3'
54
+ },
55
+ availabilityEnds,
56
+ availabilityStarts,
57
+ validFrom,
58
+ validThrough
59
+ },
60
+ {
61
+ typeOf: chevre.factory.offerType.Offer,
62
+ availableAtOrFrom: {
63
+ id: '66e7p2g713675v96nrhdm975kg'
64
+ },
65
+ availabilityEnds,
66
+ availabilityStarts,
67
+ validFrom,
68
+ validThrough
69
+ },
70
+ {
71
+ typeOf: chevre.factory.offerType.Offer,
72
+ availableAtOrFrom: {
73
+ id: '7divuoimobsfgq95tp1csorjqq'
74
+ },
75
+ availabilityEnds,
76
+ availabilityStarts,
77
+ validFrom,
78
+ validThrough
79
+ },
80
+ {
81
+ typeOf: chevre.factory.offerType.Offer,
82
+ availableAtOrFrom: {
83
+ id: 'ckevmf3fueqcunnideu6artt'
84
+ },
85
+ availabilityEnds,
86
+ availabilityStarts,
87
+ validFrom,
88
+ validThrough
89
+ }
90
+ ],
91
+ itemOffered: { id: EVENT_ID }
92
+ }
93
+ );
94
+ // tslint:disable-next-line:no-null-keyword
95
+ console.dir(result, { depth: null });
96
+ }
97
+
98
+ main()
99
+ .then(console.log)
100
+ .catch(console.error);
@@ -0,0 +1,37 @@
1
+ // tslint:disable:no-implicit-dependencies no-console
2
+ import * as mongoose from 'mongoose';
3
+ import { chevre } from '../../../../lib/index';
4
+
5
+ const project = { id: String(process.env.PROJECT_ID) };
6
+
7
+ async function main() {
8
+ try {
9
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
10
+
11
+ const paymentServiceRepo = await chevre.repository.PaymentService.createInstance(mongoose.connection);
12
+
13
+ const limit = 10;
14
+ const page = 1;
15
+ const docs = await paymentServiceRepo.projectFields(
16
+ {
17
+ limit,
18
+ page,
19
+ project: { id: { $eq: project.id } },
20
+ availableChannel: { id: { $eq: 'xxx' } },
21
+ id: { $eq: 'xxx' }
22
+ },
23
+ ['availableChannel', 'productID']
24
+ );
25
+ // tslint:disable-next-line:no-null-keyword
26
+ console.dir(docs, { depth: null });
27
+ console.log(docs.length, 'docs found');
28
+ } catch (error) {
29
+ throw await chevre.errorHandler.handleMongoError(error);
30
+ }
31
+ }
32
+
33
+ main()
34
+ .then(() => {
35
+ console.log('success!');
36
+ })
37
+ .catch(console.error);
@@ -24,6 +24,7 @@ exports.handleAWSError = handleAWSError;
24
24
  const http_status_1 = require("http-status");
25
25
  const factory_1 = require("./factory");
26
26
  let mongo;
27
+ let mongooseError;
27
28
  /**
28
29
  * https://www.mongodb.com/ja-jp/docs/manual/reference/error-codes/
29
30
  */
@@ -44,9 +45,13 @@ function isMongoError(error) {
44
45
  }
45
46
  function handleMongoError(error) {
46
47
  return __awaiter(this, void 0, void 0, function* () {
48
+ var _a;
47
49
  if (mongo === undefined) {
48
50
  mongo = (yield Promise.resolve().then(() => require('mongoose'))).mongo;
49
51
  }
52
+ if (mongooseError === undefined) {
53
+ mongooseError = (yield Promise.resolve().then(() => require('mongoose'))).Error;
54
+ }
50
55
  let handledError = error;
51
56
  if (handledError instanceof mongo.MongoError || handledError instanceof mongo.MongoServerError) {
52
57
  switch (handledError.code) {
@@ -66,6 +71,11 @@ function handleMongoError(error) {
66
71
  if (mongo.BSON.BSONError.isBSONError(handledError)) {
67
72
  handledError = new factory_1.errors.Argument('', handledError.message);
68
73
  }
74
+ // handle CastError(2025-10-27~)
75
+ // CastError: Cast to ObjectId failed for value "xxx" (type string) at path "_id" for model "xxxxxxxxxxxxx"
76
+ if (handledError instanceof mongooseError.CastError) {
77
+ handledError = new factory_1.errors.Argument(`${(_a = handledError.model) === null || _a === void 0 ? void 0 : _a.modelName}.${handledError.path}`, (handledError.reason instanceof Error) ? handledError.reason.message : handledError.message);
78
+ }
69
79
  return handledError;
70
80
  });
71
81
  }
@@ -1,54 +1,21 @@
1
1
  import type { Connection } from 'mongoose';
2
2
  import * as factory from '../factory';
3
- type IAggregateMakesOfferResult = Pick<factory.event.screeningEvent.ISellerMakesOffer, 'availabilityEnds' | 'availabilityStarts' | 'validFrom' | 'validThrough'> & {
4
- itemOffered: {
5
- serviceOutput: {
6
- reservationFor: {
7
- /**
8
- * イベントID
9
- */
10
- id: string;
11
- };
12
- };
13
- };
14
- };
15
3
  /**
16
- * イベントの販売者makesOfferリポジトリ
4
+ * イベントのアプリケーションオファーリポジトリ
17
5
  */
18
6
  export declare class EventSellerMakesOfferRepo {
19
7
  private readonly eventModel;
20
8
  constructor(connection: Connection);
21
9
  /**
22
- * 指定クライアントのオファーを集計する(2024-10-09~)
10
+ * 単一イベントのmakesOfferを更新する
23
11
  */
24
- aggregateMakesOffer(conditions: {
25
- limit?: number;
26
- page?: number;
12
+ updateOffersByEventId(params: {
27
13
  project: {
28
- id: {
29
- $eq: string;
30
- };
31
- };
32
- /**
33
- * アプリケーション(指定必須)
34
- */
35
- availableAtOrFrom: {
36
- id: {
37
- $eq: string;
38
- };
14
+ id: string;
39
15
  };
16
+ offers: factory.event.screeningEvent.ISellerMakesOffer[];
40
17
  itemOffered: {
41
- serviceOutput: {
42
- reservationFor: {
43
- /**
44
- * イベントID
45
- */
46
- id: {
47
- $in?: string[];
48
- };
49
- };
50
- };
18
+ id: string;
51
19
  };
52
- }): Promise<IAggregateMakesOfferResult[]>;
20
+ }): Promise<void>;
53
21
  }
54
- export {};
@@ -11,62 +11,53 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.EventSellerMakesOfferRepo = void 0;
13
13
  const factory = require("../factory");
14
- const settings_1 = require("../settings");
14
+ // import { MONGO_MAX_TIME_MS } from '../settings';
15
15
  const event_1 = require("./mongoose/schemas/event");
16
+ // type IMatchStage = PipelineStage.Match;
17
+ // type IAggregateMakesOfferResult = Pick<
18
+ // factory.event.screeningEvent.ISellerMakesOffer,
19
+ // 'availabilityEnds' | 'availabilityStarts' | 'validFrom' | 'validThrough'
20
+ // > & {
21
+ // itemOffered: {
22
+ // serviceOutput: {
23
+ // reservationFor: {
24
+ // /**
25
+ // * イベントID
26
+ // */
27
+ // id: string;
28
+ // };
29
+ // };
30
+ // };
31
+ // };
16
32
  /**
17
- * イベントの販売者makesOfferリポジトリ
33
+ * イベントのアプリケーションオファーリポジトリ
18
34
  */
19
35
  class EventSellerMakesOfferRepo {
20
36
  constructor(connection) {
21
37
  this.eventModel = connection.model(event_1.modelName, (0, event_1.createSchema)());
22
38
  }
23
39
  /**
24
- * 指定クライアントのオファーを集計する(2024-10-09~)
40
+ * 単一イベントのmakesOfferを更新する
25
41
  */
26
- aggregateMakesOffer(conditions) {
42
+ updateOffersByEventId(params) {
27
43
  return __awaiter(this, void 0, void 0, function* () {
28
- var _a;
29
- const { availableAtOrFrom } = conditions;
30
- if (typeof ((_a = availableAtOrFrom === null || availableAtOrFrom === void 0 ? void 0 : availableAtOrFrom.id) === null || _a === void 0 ? void 0 : _a.$eq) !== 'string' || availableAtOrFrom.id.$eq === '') {
31
- throw new factory.errors.ArgumentNull('availableAtOrFrom.id.$eq');
32
- }
33
- const matchStages = [
34
- { $match: { 'project.id': { $eq: conditions.project.id.$eq } } },
35
- { $match: { 'offers.seller.makesOffer.availableAtOrFrom.id': { $exists: true, $eq: availableAtOrFrom.id.$eq } } }
36
- ];
37
- const reservationForIdIn = conditions.itemOffered.serviceOutput.reservationFor.id.$in;
38
- if (Array.isArray(reservationForIdIn)) {
39
- matchStages.push({ $match: { _id: { $in: reservationForIdIn } } });
40
- }
41
- const aggregate = this.eventModel.aggregate([
42
- { $unwind: '$offers.seller.makesOffer' },
43
- ...matchStages,
44
- { $sort: { _id: factory.sortType.Ascending } },
45
- {
46
- $project: {
47
- _id: 0,
48
- availabilityEnds: '$offers.seller.makesOffer.availabilityEnds',
49
- availabilityStarts: '$offers.seller.makesOffer.availabilityStarts',
50
- validFrom: '$offers.seller.makesOffer.validFrom',
51
- validThrough: '$offers.seller.makesOffer.validThrough',
52
- itemOffered: {
53
- serviceOutput: {
54
- reservationFor: {
55
- id: { $toString: '$_id' }
56
- }
57
- }
58
- }
59
- }
44
+ const { offers, itemOffered, project } = params;
45
+ const updateQuery = {
46
+ $set: {
47
+ 'offers.seller.makesOffer': offers
60
48
  }
61
- ]);
62
- if (typeof conditions.limit === 'number' && conditions.limit > 0) {
63
- const page = (typeof conditions.page === 'number' && conditions.page > 0) ? conditions.page : 1;
64
- aggregate.limit(conditions.limit * page)
65
- .skip(conditions.limit * (page - 1));
66
- }
67
- return aggregate
68
- .option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
49
+ };
50
+ const updateFilter = {
51
+ _id: { $eq: itemOffered.id },
52
+ 'project.id': { $eq: project.id }
53
+ };
54
+ const queryOptions = { upsert: false, new: false, projection: { _id: 1 } };
55
+ const doc = yield this.eventModel.findOneAndUpdate(updateFilter, updateQuery, queryOptions)
56
+ .lean()
69
57
  .exec();
58
+ if (doc === null) {
59
+ throw new factory.errors.NotFound(this.eventModel.modelName);
60
+ }
70
61
  });
71
62
  }
72
63
  }
package/package.json CHANGED
@@ -115,5 +115,5 @@
115
115
  "postversion": "git push origin --tags",
116
116
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
117
117
  },
118
- "version": "23.0.0-alpha.17"
118
+ "version": "23.0.0-alpha.19"
119
119
  }
@@ -1,32 +0,0 @@
1
- // tslint:disable:no-console
2
- import * as mongoose from 'mongoose';
3
-
4
- import { chevre } from '../../../lib/index';
5
-
6
- const project = { id: String(process.env.PROJECT_ID) };
7
-
8
- async function main() {
9
- await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
10
-
11
- const eventSellerMakesOfferRepo = await chevre.repository.EventSellerMakesOffer.createInstance(mongoose.connection);
12
- const result = await eventSellerMakesOfferRepo.aggregateMakesOffer(
13
- {
14
- limit: 10,
15
- page: 1,
16
- // id: { $eq: 'bm0f0cadu' },
17
- // typeOf: chevre.factory.eventType.ScreeningEvent,
18
- project: { id: { $eq: project.id } },
19
- availableAtOrFrom: { id: { $eq: '3eo6okferrsdpfd9j2ce1iv9k7' } },
20
- itemOffered: {
21
- serviceOutput: { reservationFor: { id: { $in: ['bm0f0cadu', 'blco2394l'] } } }
22
- }
23
- }
24
- );
25
- // tslint:disable-next-line:no-null-keyword
26
- console.dir(result, { depth: null });
27
- console.dir(result.length, 'results found');
28
- }
29
-
30
- main()
31
- .then(console.log)
32
- .catch(console.error);
@@ -1,32 +0,0 @@
1
- // tslint:disable:no-implicit-dependencies no-console
2
- import * as mongoose from 'mongoose';
3
- import { chevre } from '../../../lib/index';
4
-
5
- const project = { id: String(process.env.PROJECT_ID) };
6
-
7
- async function main() {
8
- await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
9
-
10
- const paymentServiceRepo = await chevre.repository.PaymentService.createInstance(mongoose.connection);
11
-
12
- const limit = 10;
13
- const page = 1;
14
- const docs = await paymentServiceRepo.projectFields(
15
- {
16
- limit,
17
- page,
18
- project: { id: { $eq: project.id } },
19
- availableChannel: { id: { $eq: 'xxx' } }
20
- },
21
- ['availableChannel', 'productID']
22
- );
23
- // tslint:disable-next-line:no-null-keyword
24
- console.dir(docs, { depth: null });
25
- console.log(docs.length, 'docs found');
26
- }
27
-
28
- main()
29
- .then(() => {
30
- console.log('success!');
31
- })
32
- .catch(console.error);