@chevre/domain 22.14.0-alpha.20 → 22.14.0-alpha.22

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,92 @@
1
+ // tslint:disable:no-console
2
+ import { sign } from 'jsonwebtoken';
3
+ import * as mongoose from 'mongoose';
4
+
5
+ import { chevre } from '../../../../lib/index';
6
+
7
+ const project = { id: String(process.env.PROJECT_ID) };
8
+
9
+ async function main() {
10
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
11
+
12
+ const productOfferRepo = await chevre.repository.ProductOffer.createInstance(mongoose.connection);
13
+ const issuerRepo = await chevre.repository.Issuer.createInstance(mongoose.connection);
14
+ const memberProgramRepo = await chevre.repository.MemberProgram.createInstance(mongoose.connection);
15
+
16
+ const productOffer = (await productOfferRepo.findProductOffers(
17
+ {
18
+ limit: 1,
19
+ page: 1,
20
+ project: { id: { $eq: project.id } },
21
+ id: { $eq: '68d378a0c6f90b6bea9427ee' }
22
+ },
23
+ ['validForMemberTier']
24
+ )).shift();
25
+ if (productOffer === undefined) {
26
+ throw new chevre.factory.errors.NotFound(chevre.factory.offerType.Offer);
27
+ }
28
+
29
+ const tierIdentifier = productOffer.validForMemberTier?.identifier;
30
+ if (typeof tierIdentifier !== 'string') {
31
+ throw new chevre.factory.errors.NotFound('productOffer.validForMemberTier.identifier');
32
+ }
33
+
34
+ const tier = (await memberProgramRepo.projectMemberProgramTiers(
35
+ {
36
+ limit: 1,
37
+ page: 1,
38
+ project: { id: { $eq: project.id } },
39
+ identifier: { $eq: tierIdentifier }
40
+ }
41
+ )).shift();
42
+ if (tier === undefined) {
43
+ throw new chevre.factory.errors.NotFound('MemberProgramTier');
44
+ }
45
+
46
+ const { url, tokenSecret } = await issuerRepo.findByIdentifier({
47
+ identifier: tier.isTierOf.hostingOrganization.identifier,
48
+ project: { id: project.id }
49
+ });
50
+ if (typeof tokenSecret !== 'string') {
51
+ throw new chevre.factory.errors.NotFound('issuer.tokenSecret');
52
+ }
53
+
54
+ const payload = {
55
+ member: {
56
+ memberOf: {
57
+ identifier: tierIdentifier,
58
+ isTierOf: { identifier: tier.isTierOf.identifier }
59
+ }
60
+ }
61
+ };
62
+
63
+ const token = await new Promise<string>((resolve, reject) => {
64
+ // 所有権を暗号化する
65
+ sign(
66
+ payload,
67
+ tokenSecret,
68
+ {
69
+ // algorithm: jwtSetting.algorithm,
70
+ issuer: url,
71
+ expiresIn: 1800
72
+ // subject,
73
+ },
74
+ (err, encoded) => {
75
+ if (err instanceof Error) {
76
+ reject(err);
77
+ } else {
78
+ if (typeof encoded !== 'string') {
79
+ reject(new Error('cannot be signed unexpectedly'));
80
+ } else {
81
+ resolve(encoded);
82
+ }
83
+ }
84
+ }
85
+ );
86
+ });
87
+ console.log(token);
88
+ }
89
+
90
+ main()
91
+ .then()
92
+ .catch(console.error);
@@ -14,6 +14,9 @@ interface ISearchConditions {
14
14
  $eq?: string;
15
15
  };
16
16
  };
17
+ id?: {
18
+ $eq?: string;
19
+ };
17
20
  itemOffered?: {
18
21
  identifier?: {
19
22
  $eq?: string;
@@ -32,29 +32,29 @@ class ProductOfferRepo {
32
32
  this.productOfferModel = connection.model(productOffer_1.modelName, (0, productOffer_1.createSchema)());
33
33
  }
34
34
  static CREATE_MONGO_CONDITIONS(params) {
35
- var _a, _b, _c, _d, _e, _f, _g, _h;
35
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
36
36
  const andConditions = [];
37
- // const idIn = params.id?.$in;
38
- // if (Array.isArray(idIn)) {
39
- // andConditions.push({ _id: { $in: idIn } });
40
- // }
41
- const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
37
+ const idEq = (_a = params.id) === null || _a === void 0 ? void 0 : _a.$eq;
38
+ if (typeof idEq === 'string') {
39
+ andConditions.push({ _id: { $eq: idEq } });
40
+ }
41
+ const projectIdEq = (_c = (_b = params.project) === null || _b === void 0 ? void 0 : _b.id) === null || _c === void 0 ? void 0 : _c.$eq;
42
42
  if (typeof projectIdEq === 'string') {
43
43
  andConditions.push({ 'project.id': { $eq: projectIdEq } });
44
44
  }
45
- const itemOfferedIdentifierEq = (_d = (_c = params.itemOffered) === null || _c === void 0 ? void 0 : _c.identifier) === null || _d === void 0 ? void 0 : _d.$eq;
45
+ const itemOfferedIdentifierEq = (_e = (_d = params.itemOffered) === null || _d === void 0 ? void 0 : _d.identifier) === null || _e === void 0 ? void 0 : _e.$eq;
46
46
  if (typeof itemOfferedIdentifierEq === 'string') {
47
47
  andConditions.push({ 'itemOffered.identifier': { $eq: itemOfferedIdentifierEq } });
48
48
  }
49
- const validForMemberTierIdentifierEq = (_f = (_e = params.validForMemberTier) === null || _e === void 0 ? void 0 : _e.identifier) === null || _f === void 0 ? void 0 : _f.$eq;
49
+ const validForMemberTierIdentifierEq = (_g = (_f = params.validForMemberTier) === null || _f === void 0 ? void 0 : _f.identifier) === null || _g === void 0 ? void 0 : _g.$eq;
50
50
  if (typeof validForMemberTierIdentifierEq === 'string') {
51
51
  andConditions.push({ 'validForMemberTier.identifier': { $exists: true, $eq: validForMemberTierIdentifierEq } });
52
52
  }
53
- const validFromLte = (_g = params.validFrom) === null || _g === void 0 ? void 0 : _g.$lte;
53
+ const validFromLte = (_h = params.validFrom) === null || _h === void 0 ? void 0 : _h.$lte;
54
54
  if (validFromLte instanceof Date) {
55
55
  andConditions.push({ validFrom: { $lte: validFromLte } });
56
56
  }
57
- const validThroughGte = (_h = params.validThrough) === null || _h === void 0 ? void 0 : _h.$gte;
57
+ const validThroughGte = (_j = params.validThrough) === null || _j === void 0 ? void 0 : _j.$gte;
58
58
  if (validThroughGte instanceof Date) {
59
59
  andConditions.push({ validThrough: { $gte: validThroughGte } });
60
60
  }
@@ -90,7 +90,7 @@ function validateMemberTier(params) {
90
90
  validThrough: { $gte: acceptedDate.toDate() }
91
91
  }, ['identifier'])).shift();
92
92
  if (productOfferForMemberTier === undefined) {
93
- throw new factory.errors.NotFound(factory.offerType.Offer, 'product offer for member tier not found');
93
+ throw new factory.errors.NotFound(factory.offerType.Offer, 'valid product offers for member tier not found');
94
94
  }
95
95
  // let validThroughMoment: moment.Moment;
96
96
  // let validFromMoment: moment.Moment;
@@ -115,7 +115,7 @@ function validateMemberTier(params) {
115
115
  */
116
116
  function validateEventOfferPeriod(params) {
117
117
  return (repos) => __awaiter(this, void 0, void 0, function* () {
118
- var _a;
118
+ var _a, _b;
119
119
  const { event, availableAt, tokenizedMemberProgramTier } = params;
120
120
  const acceptedDate = moment(params.now);
121
121
  const eventOffers = event.offers;
@@ -145,51 +145,49 @@ function validateEventOfferPeriod(params) {
145
145
  }
146
146
  // 有効メンバープログラムティアが存在する場合
147
147
  // support validForMemberTier(2025-05-14~)
148
- if (makesOfferOnApplication.typeOf === factory.offerType.AggregateOffer) {
149
- const validForMemberTierExists = makesOfferOnApplication.validForMemberTier.typeOf === 'MemberProgramTier';
150
- if (validForMemberTierExists) {
151
- const validForMemberTierToken = tokenizedMemberProgramTier === null || tokenizedMemberProgramTier === void 0 ? void 0 : tokenizedMemberProgramTier.token;
152
- const memberProgramIdentifierMustBe = (_a = tokenizedMemberProgramTier === null || tokenizedMemberProgramTier === void 0 ? void 0 : tokenizedMemberProgramTier.isTierOf) === null || _a === void 0 ? void 0 : _a.identifier;
153
- // ティアトークンが必須
154
- if (typeof validForMemberTierToken !== 'string' || validForMemberTierToken === '') {
155
- throw new factory.errors.ArgumentNull('reservationFor.offers.validForMemberTier.token');
156
- }
157
- // メンバープログラムコード指定が必須
158
- if (typeof memberProgramIdentifierMustBe !== 'string' || memberProgramIdentifierMustBe === '') {
159
- throw new factory.errors.ArgumentNull('reservationFor.offers.validForMemberTier.isTierOf.identifier');
160
- }
161
- // 有効メンバープログラムティアが存在する場合、オファーコレクションコードが必須
162
- const aggregateOfferIdentifier = makesOfferOnApplication.identifier;
163
- if (typeof aggregateOfferIdentifier !== 'string' || aggregateOfferIdentifier === '') {
164
- throw new factory.errors.NotFound('makesOfferOnApplication.identifier');
165
- }
166
- // トークン検証
167
- const memberProgram = (yield repos.memberProgram.projectMemberPrograms({
168
- limit: 1,
169
- page: 1,
170
- project: { id: { $eq: params.event.project.id } },
171
- identifier: { $eq: memberProgramIdentifierMustBe }
172
- })).shift();
173
- if (memberProgram === undefined) {
174
- throw new factory.errors.NotFound('MemberProgram', `MemberProgram '${memberProgramIdentifierMustBe}' not found`);
175
- }
176
- const issuer = yield repos.issuer.findByIdentifier({
177
- project: { id: params.event.project.id },
178
- identifier: memberProgram.hostingOrganization.identifier
179
- });
180
- if (typeof issuer.tokenSecret !== 'string' || issuer.tokenSecret === '') {
181
- throw new factory.errors.NotFound('issuer.tokenSecret');
182
- }
183
- const verifiedValidForMemberTier = yield verifyOfferedByToken({
184
- secret: issuer.tokenSecret,
185
- issuer: issuer.url,
186
- token: validForMemberTierToken
187
- });
188
- yield validateMemberTier({
189
- event, acceptedDate, verifiedValidForMemberTier,
190
- memberProgramIdentifierMustBe, aggregateOfferIdentifier
191
- })(repos);
148
+ const validForMemberTierExists = ((_a = makesOfferOnApplication.validForMemberTier) === null || _a === void 0 ? void 0 : _a.typeOf) === 'MemberProgramTier';
149
+ if (validForMemberTierExists) {
150
+ const validForMemberTierToken = tokenizedMemberProgramTier === null || tokenizedMemberProgramTier === void 0 ? void 0 : tokenizedMemberProgramTier.token;
151
+ const memberProgramIdentifierMustBe = (_b = tokenizedMemberProgramTier === null || tokenizedMemberProgramTier === void 0 ? void 0 : tokenizedMemberProgramTier.isTierOf) === null || _b === void 0 ? void 0 : _b.identifier;
152
+ // ティアトークンが必須
153
+ if (typeof validForMemberTierToken !== 'string' || validForMemberTierToken === '') {
154
+ throw new factory.errors.ArgumentNull('reservationFor.offers.validForMemberTier.token');
155
+ }
156
+ // メンバープログラムコード指定が必須
157
+ if (typeof memberProgramIdentifierMustBe !== 'string' || memberProgramIdentifierMustBe === '') {
158
+ throw new factory.errors.ArgumentNull('reservationFor.offers.validForMemberTier.isTierOf.identifier');
159
+ }
160
+ // 有効メンバープログラムティアが存在する場合、オファーコレクションコードが必須
161
+ const aggregateOfferIdentifier = eventOffers.identifier;
162
+ if (typeof aggregateOfferIdentifier !== 'string' || aggregateOfferIdentifier === '') {
163
+ throw new factory.errors.NotFound('makesOfferOnApplication.identifier');
192
164
  }
165
+ // トークン検証
166
+ const memberProgram = (yield repos.memberProgram.projectMemberPrograms({
167
+ limit: 1,
168
+ page: 1,
169
+ project: { id: { $eq: params.event.project.id } },
170
+ identifier: { $eq: memberProgramIdentifierMustBe }
171
+ })).shift();
172
+ if (memberProgram === undefined) {
173
+ throw new factory.errors.NotFound('MemberProgram', `MemberProgram '${memberProgramIdentifierMustBe}' not found`);
174
+ }
175
+ const issuer = yield repos.issuer.findByIdentifier({
176
+ project: { id: params.event.project.id },
177
+ identifier: memberProgram.hostingOrganization.identifier
178
+ });
179
+ if (typeof issuer.tokenSecret !== 'string' || issuer.tokenSecret === '') {
180
+ throw new factory.errors.NotFound('issuer.tokenSecret');
181
+ }
182
+ const verifiedValidForMemberTier = yield verifyOfferedByToken({
183
+ secret: issuer.tokenSecret,
184
+ issuer: issuer.url,
185
+ token: validForMemberTierToken
186
+ });
187
+ yield validateMemberTier({
188
+ event, acceptedDate, verifiedValidForMemberTier,
189
+ memberProgramIdentifierMustBe, aggregateOfferIdentifier
190
+ })(repos);
193
191
  }
194
192
  });
195
193
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "@aws-sdk/client-cognito-identity-provider": "3.600.0",
13
13
  "@aws-sdk/credential-providers": "3.600.0",
14
- "@chevre/factory": "4.399.0-alpha.18",
14
+ "@chevre/factory": "4.399.0-alpha.19",
15
15
  "@cinerino/sdk": "12.2.0",
16
16
  "@motionpicture/coa-service": "9.6.0",
17
17
  "@motionpicture/gmo-service": "5.4.0-alpha.1",
@@ -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": "22.14.0-alpha.20"
118
+ "version": "22.14.0-alpha.22"
119
119
  }