srtrain
Version:
Node.js SRT train unofficial SDK
99 lines (98 loc) • 4.6 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SRTTrain = void 0;
const moment_1 = __importDefault(require("moment"));
const train_1 = require("./constants/train");
const passenger_1 = require("./passenger");
const errorCode_1 = require("./constants/errorCode");
const prioritySeatType_1 = require("./constants/prioritySeatType");
const error_1 = require("./error");
const station_1 = require("./station");
class SRTTrain {
srt;
code;
name;
number;
departureDate;
arrivalDate;
departureStation;
arrivalStation;
hasGeneralSeat;
hasSpecialSeat;
constructor(srt, options) {
this.srt = srt;
this.code = options['stlbTrnClsfCd'];
this.name = train_1.SRTTrainName[this.code];
this.number = options['trnNo'];
this.departureDate = (0, moment_1.default)(`${options['dptDt']} ${options['dptTm']}`, 'YYYYMMDD HHmmss');
this.arrivalDate = (0, moment_1.default)(`${options['arvDt'] || options['dptDt']} ${options['arvTm']}`, 'YYYYMMDD HHmmss');
this.departureStation = new station_1.SRTStation(options.dptRsStnCd);
this.arrivalStation = new station_1.SRTStation(options.arvRsStnCd);
this.hasGeneralSeat = options['gnrmRsvPsbStr']?.includes('예약가능');
this.hasSpecialSeat = options['sprmRsvPsbStr']?.includes('예약가능');
}
[Symbol.for('nodejs.util.inspect.custom')]() {
return this.toString();
}
async reserve(options) {
const { passengers, prioritySeatType } = options;
if (!this.srt.isLoggined) {
throw new error_1.SRTError(errorCode_1.SRTErrorCode.LOGIN_REQUIRED, '로그인 후 사용하십시요.');
}
if (this.code !== train_1.SRTTrainCode.SRT) {
throw new error_1.SRTError(errorCode_1.SRTErrorCode.ONLY_SRT_TRAIN, 'SRT 열차만 예약할 수 있습니다.');
}
const actualPassengers = passengers || [passenger_1.SRTPassengers.Adult()];
let isSpecialSeat = null;
switch (prioritySeatType) {
default:
case prioritySeatType_1.SRTPrioritySeatType.GENERAL_ONLY:
isSpecialSeat = false;
break;
case prioritySeatType_1.SRTPrioritySeatType.SPECIAL_ONLY:
isSpecialSeat = true;
break;
case prioritySeatType_1.SRTPrioritySeatType.GENERAL_FIRST:
isSpecialSeat = !this.hasGeneralSeat;
break;
case prioritySeatType_1.SRTPrioritySeatType.SPECIAL_FIRST:
isSpecialSeat = !this.hasSpecialSeat;
break;
}
const { data } = await this.srt.axios.post('/arc/selectListArc05013_n.do', {
'reserveType': '11',
'jobId': '1101',
'jrnyCnt': '1',
'jrnyTpCd': '11',
'jrnySqno1': '001',
'stndFlg': 'N',
'trnGpCd1': '300',
'stlbTrnClsfCd1': this.code,
'dptDt1': this.departureDate.format('YYYYMMDD'),
'dptTm1': this.departureDate.format('HHmmss'),
'runDt1': this.departureDate.format('YYYYMMDD'),
'trnNo1': this.number.padStart(5, '0'),
'dptRsStnCd1': this.departureStation.code,
'dptRsStnCdNm1': this.departureStation.name,
'arvRsStnCd1': this.arrivalStation.code,
'arvRsStnCdNm1': this.arrivalStation.name,
...passenger_1.SRTPassenger.toObject(actualPassengers),
});
this.srt.parseResponse(data);
const reservationId = data['reservListMap'][0]['pnrNo'];
const reservation = await this.srt.getReservationById(reservationId);
if (!reservation) {
throw new error_1.SRTError(errorCode_1.SRTErrorCode.FAIL_TO_RESERVE, '알 수 없는 원인으로 인해 예약에 실패하였습니다.');
}
return reservation;
}
toString() {
const hasGeneralSeat = this.hasGeneralSeat ? '예약가능' : '예약불가';
const hasSpecialSeat = this.hasSpecialSeat ? '예약가능' : '예약불가';
return `[${this.name} ${this.number}] ${this.departureStation.toString()} -> ${this.arrivalStation.toString()} (${this.departureDate.format('YYYY년 M월 D일 H시 m분')} -> ${this.arrivalDate.format('YYYY년 M월 D일 H시 m분')}) 특실: ${hasSpecialSeat} 일반실: ${hasGeneralSeat}`;
}
}
exports.SRTTrain = SRTTrain;