@chevre/domain 23.2.0-alpha.0 → 23.2.0-alpha.2

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,6 @@
1
+ import * as factory from '../../factory';
2
+ import type { ICallResult, IExecutableTaskKeys, IOperationExecute } from '../taskHandler';
3
+ /**
4
+ * タスク実行関数
5
+ */
6
+ export declare function call(params: Pick<factory.task.syncResourcesFromCOA.ITask, IExecutableTaskKeys>): IOperationExecute<ICallResult>;
@@ -0,0 +1,299 @@
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.call = call;
13
+ const COA = require("@motionpicture/coa-service");
14
+ const moment = require("moment-timezone");
15
+ const factory = require("../../factory");
16
+ const action_1 = require("../../repo/action");
17
+ const categoryCode_1 = require("../../repo/categoryCode");
18
+ const creativeWork_1 = require("../../repo/creativeWork");
19
+ const credentials_1 = require("../../repo/credentials");
20
+ const event_1 = require("../../repo/event");
21
+ const eventSeries_1 = require("../../repo/eventSeries");
22
+ const movieTheater_1 = require("../../repo/place/movieTheater");
23
+ const screeningRoom_1 = require("../../repo/place/screeningRoom");
24
+ const reserveInterface_1 = require("../../repo/reserveInterface");
25
+ const saveScreeningEvents_1 = require("../event/saveScreeningEvents");
26
+ const saveScreeningEventSeries_1 = require("../event/saveScreeningEventSeries");
27
+ let coaAuthClient;
28
+ /**
29
+ * タスク実行関数
30
+ */
31
+ function call(params) {
32
+ // tslint:disable-next-line:max-func-body-length
33
+ return (_a, options_1) => __awaiter(this, [_a, options_1], void 0, function* ({ connection, redisClient, settings }, options) {
34
+ var _b, _c, _d;
35
+ if (redisClient === undefined) {
36
+ throw new factory.errors.Argument('settings', 'redisClient required');
37
+ }
38
+ // 遅延実行(executeByName)には対応しない
39
+ if (!options.executeById) {
40
+ return;
41
+ }
42
+ const reserveInterfaceRepo = new reserveInterface_1.ReserveInterfaceRepo(connection);
43
+ const coaAPI = yield reserveInterfaceRepo.findOne({ project: { id: { $eq: params.project.id } } });
44
+ if (typeof (coaAPI === null || coaAPI === void 0 ? void 0 : coaAPI.id) !== 'string') {
45
+ throw new factory.errors.NotFound('WebAPI');
46
+ }
47
+ const credentials = (_b = coaAPI.availableChannel) === null || _b === void 0 ? void 0 : _b.credentials;
48
+ if (typeof (credentials === null || credentials === void 0 ? void 0 : credentials.refreshToken) !== 'string') {
49
+ throw new factory.errors.NotFound('WebAPI');
50
+ }
51
+ if (coaAuthClient === undefined) {
52
+ let credentialsRepo;
53
+ const credentialsExpireInSeconds = (_d = (_c = coaAPI.availableChannel) === null || _c === void 0 ? void 0 : _c.credentials) === null || _d === void 0 ? void 0 : _d.expireInSeconds;
54
+ if (typeof credentialsExpireInSeconds === 'number') {
55
+ // set credentialsRepo(2024-11-20~)
56
+ credentialsRepo = new credentials_1.CredentialsRepo(redisClient, {
57
+ scope: `COA:${coaAPI.id}`,
58
+ expireInSeconds: credentialsExpireInSeconds
59
+ });
60
+ }
61
+ coaAuthClient = new COA.auth.RefreshToken(Object.assign(Object.assign({}, credentials), (credentialsRepo !== undefined) ? { credentialsRepo } : undefined));
62
+ }
63
+ const actionRepo = new action_1.ActionRepo(connection);
64
+ try {
65
+ const masterService = new COA.service.Master({
66
+ endpoint: coaAuthClient.options.endpoint, // same as authClient(2024-07-17~)
67
+ auth: coaAuthClient
68
+ }, { timeout: settings.coa.timeout });
69
+ const { agent, object } = params.data;
70
+ if (object.typeOf === factory.eventType.ScreeningEventSeries) {
71
+ yield syncEventSeries({
72
+ project: { id: params.project.id },
73
+ agent,
74
+ object,
75
+ sameAs: { id: params.id }
76
+ })({
77
+ action: actionRepo,
78
+ categoryCode: new categoryCode_1.CategoryCodeRepo(connection),
79
+ creativeWork: new creativeWork_1.CreativeWorkRepo(connection),
80
+ eventSeries: new eventSeries_1.EventSeriesRepo(connection),
81
+ movieTheater: new movieTheater_1.MovieTheaterRepo(connection),
82
+ masterService
83
+ });
84
+ }
85
+ else if (object.typeOf === factory.eventType.ScreeningEvent) {
86
+ yield syncEvents({
87
+ project: { id: params.project.id },
88
+ agent,
89
+ object,
90
+ sameAs: { id: params.id }
91
+ })({
92
+ action: actionRepo,
93
+ categoryCode: new categoryCode_1.CategoryCodeRepo(connection),
94
+ creativeWork: new creativeWork_1.CreativeWorkRepo(connection),
95
+ event: new event_1.EventRepo(connection),
96
+ eventSeries: new eventSeries_1.EventSeriesRepo(connection),
97
+ movieTheater: new movieTheater_1.MovieTheaterRepo(connection),
98
+ screeningRoom: new screeningRoom_1.ScreeningRoomRepo(connection),
99
+ masterService
100
+ });
101
+ }
102
+ }
103
+ catch (error) {
104
+ let throwsError = true;
105
+ // アクションが存在すればタスクを実行済扱いにする
106
+ const action = (yield actionRepo.searchBySameAs({
107
+ sameAs: { id: { $eq: params.id } },
108
+ typeOf: { $eq: factory.actionType.ReplaceAction }
109
+ })).shift();
110
+ if (typeof (action === null || action === void 0 ? void 0 : action.id) === 'string') {
111
+ throwsError = false;
112
+ }
113
+ if (throwsError) {
114
+ throw error;
115
+ }
116
+ }
117
+ finally {
118
+ // no op
119
+ }
120
+ });
121
+ }
122
+ function syncEventSeries(params) {
123
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
124
+ const { locationBranchCode, titleBranchNum, titleCode } = params.object;
125
+ const actionAttributes = {
126
+ project: { typeOf: factory.organizationType.Project, id: params.project.id },
127
+ typeOf: factory.actionType.ReplaceAction,
128
+ agent: params.agent,
129
+ // tslint:disable-next-line:no-suspicious-comment
130
+ // TODO define object
131
+ object: Object.assign(Object.assign({}, params.object), { typeOf: factory.eventType.ScreeningEventSeries }),
132
+ // instrument,
133
+ targetCollection: { typeOf: factory.eventType.ScreeningEventSeries },
134
+ sameAs: { id: params.sameAs.id, typeOf: 'Task' }
135
+ };
136
+ const action = yield repos.action.start(actionAttributes);
137
+ let saveResult;
138
+ try {
139
+ const movieTheater = (yield repos.movieTheater.projectFields({
140
+ limit: 1,
141
+ page: 1,
142
+ project: { id: { $eq: params.project.id } },
143
+ branchCode: { $eq: locationBranchCode }
144
+ }, ['id', 'typeOf', 'branchCode', 'name', 'kanaName', 'parentOrganization']
145
+ // []
146
+ )).shift();
147
+ if (movieTheater === undefined) {
148
+ throw new factory.errors.NotFound(factory.placeType.MovieTheater);
149
+ }
150
+ // console.log('movieTheater:', movieTheater);
151
+ saveResult = yield (0, saveScreeningEventSeries_1.saveScreeningEventSeries)({
152
+ project: { id: params.project.id, typeOf: factory.organizationType.Project },
153
+ seller: { id: movieTheater.parentOrganization.id },
154
+ movieTheater,
155
+ locationBranchCode,
156
+ saveScreeningEventSeries: true,
157
+ saveScreeningEventSeriesPeriodInMonth: 3
158
+ }, {
159
+ filterFilmsFromCOA: {
160
+ titleBranchNum,
161
+ titleCode
162
+ }
163
+ })(repos);
164
+ // console.log(
165
+ // 'result:',
166
+ // saveResult.savedEventsCount,
167
+ // saveResult.screeningEventSerieses.length
168
+ // );
169
+ }
170
+ catch (error) {
171
+ try {
172
+ yield repos.action.giveUp({ typeOf: action.typeOf, id: action.id, error });
173
+ }
174
+ catch (__) {
175
+ // no op
176
+ }
177
+ throw error;
178
+ }
179
+ const result = saveResult.screeningEventSerieses.map((eventSeries) => {
180
+ return {
181
+ id: eventSeries.id,
182
+ typeOf: eventSeries.typeOf
183
+ };
184
+ });
185
+ yield repos.action.completeWithVoid({ typeOf: action.typeOf, id: action.id, result });
186
+ });
187
+ }
188
+ // tslint:disable-next-line:max-func-body-length
189
+ function syncEvents(params) {
190
+ // tslint:disable-next-line:max-func-body-length
191
+ return (repos) => __awaiter(this, void 0, void 0, function* () {
192
+ const { locationBranchCode, titleBranchNum, titleCode, roomCode, startDate } = params.object;
193
+ const actionAttributes = {
194
+ project: { typeOf: factory.organizationType.Project, id: params.project.id },
195
+ typeOf: factory.actionType.ReplaceAction,
196
+ agent: params.agent,
197
+ // tslint:disable-next-line:no-suspicious-comment
198
+ // TODO define object
199
+ object: Object.assign(Object.assign({}, params.object), { typeOf: factory.eventType.ScreeningEvent }),
200
+ // instrument,
201
+ targetCollection: { typeOf: factory.eventType.ScreeningEvent },
202
+ sameAs: { id: params.sameAs.id, typeOf: 'Task' }
203
+ };
204
+ const action = yield repos.action.start(actionAttributes);
205
+ let saveResult;
206
+ try {
207
+ const movieTheater = (yield repos.movieTheater.projectFields({
208
+ limit: 1,
209
+ page: 1,
210
+ project: { id: { $eq: params.project.id } },
211
+ branchCode: { $eq: locationBranchCode }
212
+ }, ['id', 'typeOf', 'branchCode', 'name', 'kanaName', 'parentOrganization']
213
+ // []
214
+ )).shift();
215
+ if (movieTheater === undefined) {
216
+ throw new factory.errors.NotFound(factory.placeType.MovieTheater);
217
+ }
218
+ // console.log('movieTheater:', movieTheater);
219
+ const screeningRoom = (yield repos.screeningRoom.searchScreeningRooms({
220
+ project: { id: { $eq: params.project.id } },
221
+ containedInPlace: { branchCode: { $eq: movieTheater.branchCode } },
222
+ branchCode: { $eq: roomCode }
223
+ })).shift();
224
+ if (screeningRoom === undefined) {
225
+ throw new factory.errors.NotFound(factory.placeType.ScreeningRoom);
226
+ }
227
+ // console.log(screeningRoom);
228
+ const eventSeriesId = (0, saveScreeningEventSeries_1.createScreeningEventSeriesId)({
229
+ theaterCode: locationBranchCode,
230
+ titleCode,
231
+ titleBranchNum
232
+ });
233
+ const eventSeries = (yield repos.eventSeries.projectEventSeriesFields({
234
+ id: { $eq: eventSeriesId },
235
+ project: { id: { $eq: params.project.id } }
236
+ }, [
237
+ 'project', 'typeOf', 'eventStatus', 'identifier',
238
+ 'name', 'kanaName', 'alternativeHeadline', 'location',
239
+ 'organizer', 'videoFormat', 'soundFormat', 'workPerformed',
240
+ 'duration', 'endDate', 'startDate', 'coaInfo',
241
+ 'offers', 'additionalProperty'
242
+ ])).shift();
243
+ if (eventSeries === undefined) {
244
+ throw new factory.errors.NotFound(factory.eventType.ScreeningEventSeries);
245
+ }
246
+ // console.log(eventSeries);
247
+ const targetImportFrom = moment(startDate)
248
+ .tz('Asia/Tokyo')
249
+ .startOf('day');
250
+ const targetImportThrough = moment(startDate)
251
+ .tz('Asia/Tokyo')
252
+ .endOf('day');
253
+ const begin = moment(targetImportFrom)
254
+ .add(-1, 'day') // 深夜帯スケジュールが前日検索の結果に含まれるため
255
+ .tz('Asia/Tokyo')
256
+ .format('YYYYMMDD'); // COAは日本時間で判断
257
+ const end = moment(targetImportThrough)
258
+ // .add(-1, 'day')
259
+ .tz('Asia/Tokyo')
260
+ .format('YYYYMMDD'); // COAは日本時間で判断
261
+ // console.log('importing screening events...', targetImportFrom, targetImportThrough, begin, end);
262
+ saveResult = yield (0, saveScreeningEvents_1.saveScreeningEvents)({
263
+ project: { id: params.project.id, typeOf: factory.organizationType.Project },
264
+ seller: { id: movieTheater.parentOrganization.id },
265
+ movieTheater,
266
+ screeningRooms: [screeningRoom],
267
+ screeningEventSerieses: [eventSeries],
268
+ targetImportFrom: targetImportFrom.toDate(),
269
+ targetImportThrough: targetImportThrough.toDate(),
270
+ instrument: {
271
+ theaterCode: locationBranchCode,
272
+ begin, // debug(2024-04-24~)
273
+ end, // debug(2024-04-24~)
274
+ typeOf: 'WebAPI',
275
+ identifier: factory.service.webAPI.Identifier.COA
276
+ }
277
+ }, {
278
+ filterSchedulesFromCOA: { titleBranchNum, titleCode, roomCode }
279
+ })(repos);
280
+ // console.log('saveResult:', saveResult.length, 'events');
281
+ }
282
+ catch (error) {
283
+ try {
284
+ yield repos.action.giveUp({ typeOf: action.typeOf, id: action.id, error });
285
+ }
286
+ catch (__) {
287
+ // no op
288
+ }
289
+ throw error;
290
+ }
291
+ const result = saveResult.map((event) => {
292
+ return {
293
+ id: event.id,
294
+ typeOf: event.typeOf
295
+ };
296
+ });
297
+ yield repos.action.completeWithVoid({ typeOf: action.typeOf, id: action.id, result });
298
+ });
299
+ }
@@ -69,6 +69,7 @@ function executeTask(task, next) {
69
69
  case factory.taskName.ReturnPayTransaction:
70
70
  case factory.taskName.ReturnReserveTransaction:
71
71
  case factory.taskName.SendEmailMessage:
72
+ case factory.taskName.SyncResourcesFromCOA:
72
73
  case factory.taskName.TriggerWebhook:
73
74
  callResult = yield call(task)(settings, options);
74
75
  break;
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": "5.4.0-alpha.3",
14
+ "@chevre/factory": "5.4.0-alpha.5",
15
15
  "@cinerino/sdk": "12.12.1",
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": "23.2.0-alpha.0"
118
+ "version": "23.2.0-alpha.2"
119
119
  }