@chevre/domain 22.1.0-alpha.10 → 22.1.0-alpha.11

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,31 @@
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 reservationRepo = await chevre.repository.Reservation.createInstance(mongoose.connection);
12
+
13
+ const reservations = await reservationRepo.projectFields<chevre.factory.reservationType.EventReservation>(
14
+ {
15
+ limit: 1,
16
+ page: 1,
17
+ typeOf: chevre.factory.reservationType.EventReservation,
18
+ project: { id: { $eq: project.id } }
19
+ },
20
+ {
21
+ id: 1
22
+ }
23
+ );
24
+ // tslint:disable-next-line:no-null-keyword
25
+ console.dir(reservations, { depth: null });
26
+ console.log(reservations.length, 'reservations found');
27
+ }
28
+
29
+ main()
30
+ .then()
31
+ .catch(console.error);
@@ -14,7 +14,7 @@ async function main() {
14
14
  let updateResult: any;
15
15
  updateResult = await accountingReportRepo.unsetUnnecessaryFields({
16
16
  filter: {
17
- typeOf: { $exists: true }
17
+ _id: { $exists: true }
18
18
  },
19
19
  $unset: {
20
20
  createdAt: 1,
@@ -26,7 +26,7 @@ async function main() {
26
26
 
27
27
  updateResult = await aggregateReservationRepo.unsetUnnecessaryFields({
28
28
  filter: {
29
- typeOf: { $exists: true }
29
+ _id: { $exists: true }
30
30
  },
31
31
  $unset: {
32
32
  createdAt: 1,
@@ -38,7 +38,7 @@ async function main() {
38
38
 
39
39
  updateResult = await projectRepo.unsetUnnecessaryFields({
40
40
  filter: {
41
- typeOf: { $exists: true }
41
+ _id: { $exists: true }
42
42
  },
43
43
  $unset: {
44
44
  createdAt: 1,
@@ -50,7 +50,7 @@ async function main() {
50
50
 
51
51
  updateResult = await reservationRepo.unsetUnnecessaryFields({
52
52
  filter: {
53
- typeOf: { $exists: true }
53
+ _id: { $exists: true }
54
54
  },
55
55
  $unset: {
56
56
  createdAt: 1,
@@ -62,5 +62,7 @@ async function main() {
62
62
  }
63
63
 
64
64
  main()
65
- .then()
65
+ .then(() => {
66
+ console.log('success!');
67
+ })
66
68
  .catch(console.error);
@@ -34,7 +34,7 @@ export type ICreatingReservation<T extends factory.reservationType> = T extends
34
34
  } : T extends factory.reservationType.EventReservation ? (factory.reservation.eventReservation.IReservation) & {
35
35
  _id: string;
36
36
  } : never;
37
- type IKeyOfProjection = keyof factory.reservation.IReservation<factory.reservationType.BusReservation | factory.reservationType.EventReservation> | '_id' | 'reservedTicket.dateUsed' | 'reservationFor.id' | 'reservationFor.typeOf';
37
+ type IKeyOfProjection = keyof factory.reservation.IReservation<factory.reservationType.BusReservation | factory.reservationType.EventReservation> | 'reservedTicket.dateUsed' | 'reservationFor.id' | 'reservationFor.typeOf';
38
38
  export type IAttendedReservation = Pick<factory.reservation.IReservation<factory.reservationType.EventReservation>, 'id' | 'typeOf' | 'project' | 'modifiedTime'> & {
39
39
  reservationFor: Pick<factory.reservation.IReservationFor<factory.reservationType.EventReservation>, 'id' | 'typeOf'>;
40
40
  reservedTicket: Pick<factory.reservation.ITicket, 'dateUsed'>;
@@ -52,9 +52,10 @@ export declare class ReservationRepo {
52
52
  count<T extends factory.reservationType>(params: factory.reservation.ISearchConditions<T>): Promise<number>;
53
53
  /**
54
54
  * 予約検索
55
+ * migrate from search(2024-08-08~)
55
56
  */
56
- search<T extends factory.reservationType>(params: factory.reservation.ISearchConditions<T>, projection?: {
57
- [key in IKeyOfProjection]?: 0 | 1;
57
+ projectFields<T extends factory.reservationType>(params: factory.reservation.ISearchConditions<T>, inclusion?: {
58
+ [key in IKeyOfProjection]?: 1;
58
59
  }): Promise<factory.reservation.IReservation<factory.reservationType.EventReservation>[]>;
59
60
  projectFieldsById<T extends factory.reservationType>(params: {
60
61
  id: string;
@@ -150,7 +151,6 @@ export declare class ReservationRepo {
150
151
  $in: string[];
151
152
  };
152
153
  }): Promise<string[]>;
153
- deleteReservedTicketUnderName(): Promise<import("mongodb").UpdateResult>;
154
154
  getCursor(conditions: any, projection: any): import("mongoose").Cursor<any, import("mongoose").QueryOptions<any>>;
155
155
  unsetUnnecessaryFields(params: {
156
156
  filter: any;
@@ -805,18 +805,54 @@ class ReservationRepo {
805
805
  }
806
806
  /**
807
807
  * 予約検索
808
+ * migrate from search(2024-08-08~)
808
809
  */
809
- search(params, projection) {
810
+ projectFields(params, inclusion) {
810
811
  var _a;
811
812
  return __awaiter(this, void 0, void 0, function* () {
812
813
  const conditions = ReservationRepo.CREATE_MONGO_CONDITIONS(params);
813
- const query = this.reservationModel.find((conditions.length > 0) ? { $and: conditions } : {}, (projection !== undefined && projection !== null)
814
- ? projection
815
- : {
816
- __v: 0,
817
- createdAt: 0,
818
- updatedAt: 0
819
- });
814
+ let projection = {};
815
+ if (inclusion !== undefined && inclusion !== null) {
816
+ projection = {
817
+ _id: 0,
818
+ id: { $toString: '$_id' }
819
+ };
820
+ for (const [field, value] of Object.entries(inclusion)) {
821
+ // 厳密に1を指定されたフィールドのみprojectionに追加
822
+ if (value === 1 && field !== '_id' && field !== 'id') {
823
+ projection[field] = 1;
824
+ }
825
+ }
826
+ }
827
+ else {
828
+ projection = {
829
+ _id: 0,
830
+ id: { $toString: '$_id' },
831
+ project: 1,
832
+ provider: 1,
833
+ typeOf: 1,
834
+ additionalTicketText: 1,
835
+ bookingTime: 1,
836
+ broker: 1,
837
+ modifiedTime: 1,
838
+ numSeats: 1,
839
+ previousReservationStatus: 1,
840
+ price: 1,
841
+ priceCurrency: 1,
842
+ programMembershipUsed: 1,
843
+ reservationFor: 1,
844
+ reservationNumber: 1,
845
+ reservationStatus: 1,
846
+ reservedTicket: 1,
847
+ subReservation: 1,
848
+ underName: 1,
849
+ checkedIn: 1,
850
+ attended: 1,
851
+ additionalProperty: 1,
852
+ issuedThrough: 1
853
+ };
854
+ }
855
+ const query = this.reservationModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection);
820
856
  if (typeof params.limit === 'number' && params.limit > 0) {
821
857
  const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
822
858
  query.limit(params.limit)
@@ -828,10 +864,39 @@ class ReservationRepo {
828
864
  query.sort({ modifiedTime: params.sort.modifiedTime });
829
865
  }
830
866
  return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
831
- .exec()
832
- .then((docs) => docs.map((doc) => doc.toObject()));
867
+ .lean()
868
+ .exec();
833
869
  });
834
870
  }
871
+ // public async search<T extends factory.reservationType>(
872
+ // params: factory.reservation.ISearchConditions<T>,
873
+ // projection?: { [key in IKeyOfProjection]?: 0 | 1 }
874
+ // ): Promise<factory.reservation.IReservation<factory.reservationType.EventReservation>[]> {
875
+ // const conditions = ReservationRepo.CREATE_MONGO_CONDITIONS(params);
876
+ // const query = this.reservationModel.find(
877
+ // (conditions.length > 0) ? { $and: conditions } : {},
878
+ // (projection !== undefined && projection !== null)
879
+ // ? projection
880
+ // : {
881
+ // __v: 0,
882
+ // createdAt: 0,
883
+ // updatedAt: 0
884
+ // }
885
+ // );
886
+ // if (typeof params.limit === 'number' && params.limit > 0) {
887
+ // const page: number = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
888
+ // query.limit(params.limit)
889
+ // .skip(params.limit * (page - 1));
890
+ // }
891
+ // // tslint:disable-next-line:no-single-line-block-comment
892
+ // /* istanbul ignore else */
893
+ // if (params.sort?.modifiedTime !== undefined) {
894
+ // query.sort({ modifiedTime: params.sort.modifiedTime });
895
+ // }
896
+ // return query.setOptions({ maxTimeMS: MONGO_MAX_TIME_MS })
897
+ // .exec()
898
+ // .then((docs) => docs.map((doc) => doc.toObject()));
899
+ // }
835
900
  projectFieldsById(params) {
836
901
  return __awaiter(this, void 0, void 0, function* () {
837
902
  let useInclusionProjection = false;
@@ -843,7 +908,7 @@ class ReservationRepo {
843
908
  id: { $toString: '$_id' }
844
909
  };
845
910
  params.inclusion.forEach((field) => {
846
- if (field !== '_id' && field !== 'id') {
911
+ if (String(field) !== '_id' && field !== 'id') {
847
912
  projection[field] = 1;
848
913
  }
849
914
  });
@@ -1159,18 +1224,16 @@ class ReservationRepo {
1159
1224
  .then((docs) => docs.map((doc) => doc._id.toString()));
1160
1225
  });
1161
1226
  }
1162
- deleteReservedTicketUnderName() {
1163
- return __awaiter(this, void 0, void 0, function* () {
1164
- const conditions = {
1165
- 'reservedTicket.underName': { $exists: true }
1166
- };
1167
- const update = {
1168
- $unset: { 'reservedTicket.underName': 1 }
1169
- };
1170
- return this.reservationModel.updateMany(conditions, update)
1171
- .exec();
1172
- });
1173
- }
1227
+ // public async deleteReservedTicketUnderName() {
1228
+ // const conditions = {
1229
+ // 'reservedTicket.underName': { $exists: true }
1230
+ // };
1231
+ // const update = {
1232
+ // $unset: { 'reservedTicket.underName': 1 }
1233
+ // };
1234
+ // return this.reservationModel.updateMany(conditions, update)
1235
+ // .exec();
1236
+ // }
1174
1237
  getCursor(conditions, projection) {
1175
1238
  return this.reservationModel.find(conditions, projection)
1176
1239
  .sort({ bookingTime: factory.sortType.Descending })
@@ -288,10 +288,10 @@ function cancelReservation(actionAttributesList) {
288
288
  const reservationNumber = actionAttributes.object.reservationNumber;
289
289
  if (typeof reservationNumber === 'string' && reservationNumber.length > 0) {
290
290
  // 最新のconfirmedReservationsを検索
291
- canceledReservations = yield repos.reservation.search({
291
+ canceledReservations = yield repos.reservation.projectFields({
292
292
  reservationNumber: { $eq: reservationNumber },
293
293
  typeOf: factory.reservationType.EventReservation
294
- });
294
+ }, { project: 1, typeOf: 1, modifiedTime: 1, reservationNumber: 1 });
295
295
  canceledReservations = canceledReservations.map((r) => {
296
296
  // _idは不要であり、存在すると予期せぬ影響を及ぼす可能性がある
297
297
  delete r._id;
@@ -36,10 +36,13 @@ function confirmReservation(params) {
36
36
  const reservationNumber = params.potentialReserveAction.object.reservationNumber;
37
37
  if (typeof reservationNumber === 'string' && reservationNumber.length > 0) {
38
38
  // 最新のconfirmedReservationsを検索
39
- confirmedReservations = yield repos.reservation.search({
39
+ confirmedReservations = yield repos.reservation.projectFields({
40
40
  reservationNumber: { $eq: reservationNumber },
41
41
  typeOf: factory.reservationType.EventReservation
42
- });
42
+ }
43
+ // project all fields
44
+ // {}
45
+ );
43
46
  confirmedReservations = confirmedReservations.map((r) => {
44
47
  // _idは不要であり、存在すると予期せぬ影響を及ぼす可能性がある
45
48
  delete r._id;
@@ -90,7 +90,7 @@ function searchByOrder(params) {
90
90
  }
91
91
  break;
92
92
  default:
93
- reservations = yield repos.reservation.search({
93
+ reservations = yield repos.reservation.projectFields({
94
94
  project: { id: { $eq: params.project.id } },
95
95
  typeOf: reservationType,
96
96
  id: { $in: reservationIds }
@@ -181,13 +181,13 @@ function checkUsedReservationExists(params) {
181
181
  }, 'acceptedOffers.itemOffered.reservationNumber');
182
182
  if (reservationNumbers.length > 0) {
183
183
  // 使用済の予約がひとつでもあれば不可
184
- const existingUsedReservations = yield repos.reservation.search({
184
+ const existingUsedReservations = yield repos.reservation.projectFields({
185
185
  limit: 1,
186
186
  page: 1,
187
187
  typeOf: factory.reservationType.EventReservation,
188
188
  reservationNumber: { $in: reservationNumbers },
189
189
  attended: true
190
- }, { _id: 1 });
190
+ }, { id: 1 });
191
191
  if (existingUsedReservations.length > 0) {
192
192
  usedReservationExists = true;
193
193
  }
package/package.json CHANGED
@@ -110,5 +110,5 @@
110
110
  "postversion": "git push origin --tags",
111
111
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
112
112
  },
113
- "version": "22.1.0-alpha.10"
113
+ "version": "22.1.0-alpha.11"
114
114
  }
@@ -1,55 +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
- mongoose.Model.on('index', (...args) => {
9
- console.error('******** index event emitted. ********\n', args);
10
- });
11
-
12
- async function main() {
13
- await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
14
-
15
- const indexes = await mongoose.connection.db.collection('orders')
16
- .indexes();
17
- // console.log(indexes);
18
- console.log(indexes.length, 'indexes found');
19
-
20
- const orderRepo = await chevre.repository.Order.createInstance(mongoose.connection);
21
-
22
- const orders = await orderRepo.projectFields(
23
- {
24
- limit: 3,
25
- page: 1,
26
- sort: { orderDate: -1 },
27
- project: { id: { $eq: project.id } },
28
- acceptedOffers: {
29
- // itemOffered: {
30
- // reservedTicket: {
31
- // identifier: { $eq: 'bls2qevbb:Default:B-9' }
32
- // }
33
- // }
34
- // $size: 1
35
- // itemOffered: {
36
- // issuedThrough: { typeOf: { $eq: chevre.factory.product.ProductType.MembershipService } }
37
- // }
38
- }
39
- },
40
- {
41
- inclusion: [
42
- 'orderNumber', 'orderDate'
43
- , 'paymentMethods.name'
44
- // , 'confirmationNumber', 'identifier', 'orderDate', 'orderStatus', 'price', 'typeOf'
45
- ]
46
- }
47
- );
48
- // tslint:disable-next-line:no-null-keyword
49
- console.dir(orders, { depth: null });
50
- console.log(orders.length, 'orders found');
51
- }
52
-
53
- main()
54
- .then()
55
- .catch(console.error);