@vendit-dev/thirdparty-adapters 0.3.31 → 0.4.0-beta.1

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,75 @@
1
+ import { UserServiceGrpcCaller } from '@vendit-dev/utility-modules';
2
+ import { AmanoHttpResponse, DeletePreDiscountInfoInput, DeletePreDiscountInfoResponse, FeeCarSearchInput, GetFeeCarSearchResponse, GetPreDiscountListInfoResponse, InsertPreDiscountInfoInput, InsertPreDiscountInfoResponse, PreDiscountListInfoInput, UpdatePreDiscountInfoInput, UpdatePreDiscountInfoResponse } from '../types/amanoKorea';
3
+ import MutexLock from '../utils/mutex';
4
+ declare type VenditAmanoKoreaAuthInfo = {
5
+ scope: string;
6
+ credential: {
7
+ lotAreaNo: string;
8
+ registUserId: string;
9
+ password: string;
10
+ eqpmNo: string;
11
+ discCodeNo: string;
12
+ };
13
+ };
14
+ export default class AmanoKoreaAdapter {
15
+ API_HOST: string;
16
+ TEST_SERVER_URL: string;
17
+ authenticator: UserServiceGrpcCaller;
18
+ redis: any;
19
+ PROVIDER_KEY: string;
20
+ mutexLock: MutexLock;
21
+ authorization: string;
22
+ dateFormat: string;
23
+ defaultPreDiscountInfo: {
24
+ lotAreaNo: number;
25
+ registUserId: string;
26
+ registUserName: string;
27
+ preDiscountId: number;
28
+ discCodeNo: number;
29
+ carNo: string;
30
+ dcCount: number;
31
+ startDtm: string;
32
+ endDtm: string;
33
+ dongcode: string;
34
+ hocode: string;
35
+ memo: string;
36
+ mobile: string;
37
+ };
38
+ constructor({ authenticator, redis, apiHost, }: {
39
+ authenticator: UserServiceGrpcCaller;
40
+ redis: any;
41
+ apiHost: string;
42
+ });
43
+ parseJSONSafe: (str: string) => any | boolean;
44
+ static indexAccommodation: ({ accommodationId, }: {
45
+ accommodationId: string;
46
+ }) => string;
47
+ checkAmanoKoreaIntegration: ({ accommodationId, }: {
48
+ accommodationId: string;
49
+ }) => Promise<boolean>;
50
+ getAccommodationAuthInfo: ({ accommodationId, }: {
51
+ accommodationId: string;
52
+ }) => Promise<VenditAmanoKoreaAuthInfo | boolean>;
53
+ setAuthorization: ({ authorization }: {
54
+ authorization: string;
55
+ }) => void;
56
+ callAPI: <TRequestType, TReturnType extends AmanoHttpResponse<any> = AmanoHttpResponse<any>, TErrorType extends AmanoHttpResponse<any> = AmanoHttpResponse<any>>({ directory, resources, }: {
57
+ directory: string;
58
+ resources: TRequestType;
59
+ }) => Promise<TReturnType | TErrorType>;
60
+ syncTimeToMCS: () => Promise<string>;
61
+ getPreDiscountListInfo: (resources: PreDiscountListInfoInput) => Promise<AmanoHttpResponse<GetPreDiscountListInfoResponse>>;
62
+ insertPreDiscountInfo: ({ resources, }: {
63
+ resources: InsertPreDiscountInfoInput;
64
+ }) => Promise<AmanoHttpResponse<InsertPreDiscountInfoResponse>>;
65
+ updatePreDiscountInfo: ({ resources, }: {
66
+ resources: UpdatePreDiscountInfoInput;
67
+ }) => Promise<AmanoHttpResponse<UpdatePreDiscountInfoResponse>>;
68
+ deletePreDiscountInfo: ({ resources, }: {
69
+ resources: DeletePreDiscountInfoInput;
70
+ }) => Promise<AmanoHttpResponse<DeletePreDiscountInfoResponse>>;
71
+ getFeeCarSearch: ({ resources, }: {
72
+ resources: FeeCarSearchInput;
73
+ }) => Promise<AmanoHttpResponse<GetFeeCarSearchResponse>>;
74
+ }
75
+ export {};
@@ -0,0 +1,292 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var node_fetch_1 = __importDefault(require("node-fetch"));
54
+ var mutex_1 = __importDefault(require("../utils/mutex"));
55
+ var AmanoKoreaAdapter = /** @class */ (function () {
56
+ function AmanoKoreaAdapter(_a) {
57
+ var _this = this;
58
+ var authenticator = _a.authenticator, redis = _a.redis, apiHost = _a.apiHost;
59
+ this.API_HOST = 'http://deploy.amanopark.co.kr:9099';
60
+ this.TEST_SERVER_URL = 'http://106.10.44.103:9948';
61
+ this.PROVIDER_KEY = 'AmanoKorea';
62
+ // base64
63
+ this.authorization = '';
64
+ this.dateFormat = 'YYYYMMDDHHmmss';
65
+ this.defaultPreDiscountInfo = {
66
+ lotAreaNo: 20,
67
+ registUserId: '0101*0201',
68
+ registUserName: '홍길동',
69
+ preDiscountId: 1,
70
+ discCodeNo: 99,
71
+ carNo: '23가8823',
72
+ dcCount: 0,
73
+ startDtm: '20200917000000',
74
+ endDtm: '20200917235959',
75
+ dongcode: '101',
76
+ hocode: '201',
77
+ memo: '방문차량수정요청',
78
+ mobile: '',
79
+ };
80
+ this.parseJSONSafe = function (str) {
81
+ try {
82
+ return JSON.parse(str);
83
+ }
84
+ catch (e) {
85
+ console.log('parseJSONSafe', e);
86
+ return false;
87
+ }
88
+ };
89
+ this.checkAmanoKoreaIntegration = function (_a) {
90
+ var accommodationId = _a.accommodationId;
91
+ return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_b) {
92
+ switch (_b.label) {
93
+ case 0: return [4 /*yield*/, this.getAccommodationAuthInfo({ accommodationId: accommodationId })];
94
+ case 1: return [2 /*return*/, !!(_b.sent())];
95
+ }
96
+ }); });
97
+ };
98
+ this.getAccommodationAuthInfo = function (_a) {
99
+ var accommodationId = _a.accommodationId;
100
+ return __awaiter(_this, void 0, void 0, function () {
101
+ var existingAuth, lockId, lockHash, newAuth, parsedCredential, err_1;
102
+ return __generator(this, function (_b) {
103
+ switch (_b.label) {
104
+ case 0: return [4 /*yield*/, this.redis.get(AmanoKoreaAdapter.indexAccommodation({ accommodationId: accommodationId }))];
105
+ case 1:
106
+ existingAuth = _b.sent();
107
+ if (existingAuth) {
108
+ if (existingAuth === 'NOT_FOUND') {
109
+ return [2 /*return*/, false];
110
+ }
111
+ return [2 /*return*/, this.parseJSONSafe(existingAuth)];
112
+ }
113
+ lockId = "AMANO-HKEY|" + accommodationId;
114
+ return [4 /*yield*/, this.mutexLock.getMutexLock(lockId)];
115
+ case 2:
116
+ lockHash = _b.sent();
117
+ return [4 /*yield*/, this.redis.get(AmanoKoreaAdapter.indexAccommodation({ accommodationId: accommodationId }))];
118
+ case 3:
119
+ existingAuth = _b.sent();
120
+ _b.label = 4;
121
+ case 4:
122
+ _b.trys.push([4, 9, 10, 12]);
123
+ if (existingAuth) {
124
+ return [2 /*return*/, this.parseJSONSafe(existingAuth)];
125
+ }
126
+ return [4 /*yield*/, this.authenticator.getThirdPartyCredential({
127
+ accommodationId: accommodationId,
128
+ provider: this.PROVIDER_KEY,
129
+ })];
130
+ case 5:
131
+ newAuth = _b.sent();
132
+ if (!(!newAuth || !(newAuth === null || newAuth === void 0 ? void 0 : newAuth.credential))) return [3 /*break*/, 7];
133
+ return [4 /*yield*/, this.redis.set(AmanoKoreaAdapter.indexAccommodation({ accommodationId: accommodationId }), 'NOT_FOUND', 'EX', 60 * 5)];
134
+ case 6:
135
+ _b.sent();
136
+ return [2 /*return*/, false];
137
+ case 7:
138
+ parsedCredential = this.parseJSONSafe(newAuth.credential);
139
+ if (parsedCredential === null || parsedCredential === void 0 ? void 0 : parsedCredential.authorization) {
140
+ this.setAuthorization(parsedCredential.authorization);
141
+ }
142
+ else {
143
+ throw new Error('Authorization is not found');
144
+ }
145
+ return [4 /*yield*/, this.redis.set(AmanoKoreaAdapter.indexAccommodation({ accommodationId: accommodationId }), newAuth.credential, 'EX', 60 * 5)];
146
+ case 8:
147
+ _b.sent();
148
+ return [2 /*return*/, parsedCredential];
149
+ case 9:
150
+ err_1 = _b.sent();
151
+ console.log(err_1);
152
+ throw err_1;
153
+ case 10: return [4 /*yield*/, this.mutexLock.unlockMutexLock(lockId, lockHash)];
154
+ case 11:
155
+ _b.sent();
156
+ return [7 /*endfinally*/];
157
+ case 12: return [2 /*return*/];
158
+ }
159
+ });
160
+ });
161
+ };
162
+ this.setAuthorization = function (_a) {
163
+ var authorization = _a.authorization;
164
+ _this.authorization = "Basic " + authorization;
165
+ };
166
+ this.callAPI = function (_a) {
167
+ var directory = _a.directory, resources = _a.resources;
168
+ return __awaiter(_this, void 0, void 0, function () {
169
+ var fetchResult, fetchResultJson, data, err_2;
170
+ return __generator(this, function (_b) {
171
+ switch (_b.label) {
172
+ case 0:
173
+ _b.trys.push([0, 3, , 4]);
174
+ return [4 /*yield*/, node_fetch_1.default(this.API_HOST + "/interop/" + directory, {
175
+ method: 'POST',
176
+ headers: {
177
+ 'Content-Type': 'application/json',
178
+ Authorization: this.authorization,
179
+ },
180
+ body: JSON.stringify(__assign({}, resources)),
181
+ })];
182
+ case 1:
183
+ fetchResult = _b.sent();
184
+ return [4 /*yield*/, fetchResult.json()];
185
+ case 2:
186
+ fetchResultJson = _b.sent();
187
+ console.log('fetchResultJson', fetchResultJson);
188
+ data = fetchResultJson.data;
189
+ return [2 /*return*/, data];
190
+ case 3:
191
+ err_2 = _b.sent();
192
+ console.error('err', err_2);
193
+ return [2 /*return*/, err_2];
194
+ case 4: return [2 /*return*/];
195
+ }
196
+ });
197
+ });
198
+ };
199
+ this.syncTimeToMCS = function () { return __awaiter(_this, void 0, void 0, function () {
200
+ var fetchRes, fetchResJson;
201
+ return __generator(this, function (_a) {
202
+ switch (_a.label) {
203
+ case 0: return [4 /*yield*/, node_fetch_1.default("" + 'http://deploy.amanopark.co.kr:9848/getTime', {
204
+ method: 'POST',
205
+ headers: {
206
+ 'Content-Type': 'application/json',
207
+ Authorization: this.authorization,
208
+ },
209
+ body: JSON.stringify({}),
210
+ })];
211
+ case 1:
212
+ fetchRes = _a.sent();
213
+ return [4 /*yield*/, fetchRes.json()];
214
+ case 2:
215
+ fetchResJson = _a.sent();
216
+ return [2 /*return*/, fetchResJson];
217
+ }
218
+ });
219
+ }); };
220
+ // 방문차량 등록 내역 조회
221
+ this.getPreDiscountListInfo = function (resources) { return __awaiter(_this, void 0, void 0, function () {
222
+ return __generator(this, function (_a) {
223
+ return [2 /*return*/, this.callAPI({
224
+ directory: 'getPreDiscountListInfo.do',
225
+ resources: resources,
226
+ })];
227
+ });
228
+ }); };
229
+ // 방문차량 등록
230
+ this.insertPreDiscountInfo = function (_a) {
231
+ var _b = _a.resources, resources = _b === void 0 ? _this.defaultPreDiscountInfo : _b;
232
+ return __awaiter(_this, void 0, void 0, function () {
233
+ return __generator(this, function (_c) {
234
+ return [2 /*return*/, this.callAPI({
235
+ directory: 'insertPreDiscountInfo.do',
236
+ resources: resources,
237
+ })];
238
+ });
239
+ });
240
+ };
241
+ // 방문차량 수정
242
+ this.updatePreDiscountInfo = function (_a) {
243
+ var _b = _a.resources, resources = _b === void 0 ? _this.defaultPreDiscountInfo : _b;
244
+ return __awaiter(_this, void 0, void 0, function () {
245
+ return __generator(this, function (_c) {
246
+ return [2 /*return*/, this.callAPI({
247
+ directory: 'updatePreDiscountInfo.do',
248
+ resources: resources,
249
+ })];
250
+ });
251
+ });
252
+ };
253
+ // 방문차량 삭제
254
+ this.deletePreDiscountInfo = function (_a) {
255
+ var _b = _a.resources, resources = _b === void 0 ? { lotAreaNo: 20, registUserId: '0101*0201', preDiscountId: 1 } : _b;
256
+ return __awaiter(_this, void 0, void 0, function () {
257
+ return __generator(this, function (_c) {
258
+ return [2 /*return*/, this.callAPI({
259
+ directory: 'deletePreDiscountInfo.do',
260
+ resources: resources,
261
+ })];
262
+ });
263
+ });
264
+ };
265
+ // 차량 조회 (요금 조회 대상)
266
+ this.getFeeCarSearch = function (_a) {
267
+ var _b = _a.resources, resources = _b === void 0 ? { lotAreaNo: 20, eqpmNo: 1, carNo4Dight: '1234' } : _b;
268
+ return __awaiter(_this, void 0, void 0, function () {
269
+ return __generator(this, function (_c) {
270
+ return [2 /*return*/, this.callAPI({
271
+ directory: 'getFeeCarSearch.do',
272
+ resources: resources,
273
+ })];
274
+ });
275
+ });
276
+ };
277
+ this.authenticator = authenticator;
278
+ this.API_HOST = apiHost;
279
+ this.redis = redis;
280
+ this.mutexLock = new mutex_1.default({
281
+ hashPrefix: 'RES_AMANO_MUTEX_',
282
+ mutexLockInterval: 50,
283
+ redis: redis,
284
+ });
285
+ }
286
+ AmanoKoreaAdapter.indexAccommodation = function (_a) {
287
+ var accommodationId = _a.accommodationId;
288
+ return "AUTH|AMANO-KOREA-API|" + accommodationId;
289
+ };
290
+ return AmanoKoreaAdapter;
291
+ }());
292
+ exports.default = AmanoKoreaAdapter;