Skip to content

Commit

Permalink
add ShortFareHotel prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSanEM committed Nov 11, 2021
1 parent 75a754a commit 6f45c77
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 0 deletions.
244 changes: 244 additions & 0 deletions ShortFareHotel.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@

const siteEditionRegExp = /^[a-z]{2}-[A-Z]{2}$/
const tenantCodeRegExp = /^[A-Z0-9]{2,4}$/
const _3to5upperCasedLettersRegExp = /^[A-Z]{3,5}$/
const dateRegExp = /^\d{4}-\d{2}-\d{2}/

class ShortFareHotel {
static formatSiteEdition (siteEdition) {
const [, lang, countryCode] = ('' + siteEdition).match(/^([A-Za-z]{2})[^A-Za-z]?([A-Za-z]{2})?$/) || []

if (!lang) return `INVALID_SITE_EDITION [${siteEdition}]`

return countryCode ? `${lang.toLowerCase()}_${countryCode.toUpperCase()}` : lang.toLowerCase()
}

constructor () {
this.tc = undefined // tenantCode
this.hc = undefined // hotelCode
this.rc = undefined // rateCode
this.cid = undefined // checkInDate
this.cod = undefined // checkOutDate
this.n = undefined // nights
this.g = undefined // guests
this.c = undefined // currencyCode
this.p = undefined // totalPrice
this.ca = undefined // createdAt
this.ua = undefined // createdAt
this.si = undefined // sourceId
}

get tenantCode () {
return this.tc
}

set tenantCode (v) {
if (!tenantCodeRegExp.test(v)) {
throw new Error(`tenantCode [${v}] does not match ${tenantCodeRegExp}`)
}

this.tc = v
}

get hotelCode () {
return this.hc
}

set hotelCode (v) {
if (!_3to5upperCasedLettersRegExp.test(v)) {
throw new Error(`hotelCode [${v}] does not match ${_3to5upperCasedLettersRegExp}`)
}

this.hc = v
}

get rateCode () {
return this.rc
}

set rateCode (v) {
if (!_3to5upperCasedLettersRegExp.test(v)) {
throw new Error(`rateCode [${v}] does not match ${_3to5upperCasedLettersRegExp}`)
}

this.rc = v
}

get checkInDate () {
return this.cid
}

set checkInDate (_v) {
const v = _v instanceof Date ? _v.toJSON() : _v

if (!dateRegExp.test(v)) {
throw new Error(`checkInDate [${v}] is not valid`)
}

this.cid = v
}

get checkOutDate () {
return this.cod
}

set checkOutDate (_v) {
const v = _v instanceof Date ? _v.toJSON() : _v
if (v !== undefined && !dateRegExp.test(v)) {
throw new Error(`checkInDate [${v}] is not valid`)
}

this.cod = v
}

get currencyCode () {
return this.c
}

set currencyCode (v) {
if (!_3to5upperCasedLettersRegExp.test(v)) {
throw new Error(`currencyCode [${v}] does not match ${_3to5upperCasedLettersRegExp}`)
}

this.c = v
}

get nights () {
return this.n
}

set nights (v) {
if (+v !== (v | 0)) {
throw new Error(`nights [${v}] is not a valid Integer`)
}

this.n = (v | 0)
}

get guests () {
return this.g
}

set guests (v) {
if (+v !== (v | 0)) {
throw new Error(`guests [${v}] is not a valid Integer`)
}

this.g = v
}

get flightType () {
return this.ft
}

set flightType (v) {
if (v !== 'I' && v !== 'D') {
throw new Error(`flightType [${v}] does not equal [I] or [D]`)
}

this.ft = v
}

get siteEdition () {
return this.se
}

set siteEdition (v) {
if (v === undefined) return

if (!siteEditionRegExp.test(v)) {
throw new Error(`siteEdition [${v}] does not match ${siteEditionRegExp}`)
}

this.se = v
}

get totalPrice () {
return this.p
}

set totalPrice (v) {
this.p = v
}

get createdAt () {
return this.ca
}

set createdAt (v) {
this.ca = v
}

get updatedAt () {
return this.ua
}

set updatedAt (v) {
this.ua = v
}

get sourceId () {
return this.si
}

set sourceId (v) {
this.si = v
}

get isSoldOut () {
return this.so
}

set isSoldOut (v) {
this.so = Boolean(v)
}

get _id () {
return {
tc: this.tc,
hc: this.hc,
rc: this.rc,
cid: this.cid,
cod: this.cod,
c: this.c,
n: this.n,
ft: this.ft,
se: this.se,
g: this.g
}
}

get mongoDoc () {
return {
_id: this._id,
p: this.p,
ca: this.ca,
ua: this.ua,
si: this.si,
so: this.so
}
}

get mongoUpdateDoc () {
return {
// $set: this.mongoDoc,
$set: { p: this.p },
$setOnInsert: {
ca: this.ca,
ua: this.ua,
si: this.si,
so: this.so
}
}
}

get mongoUpdateList () {
return [
{ _id: this._id },
this.mongoUpdateDoc,
{ upsert: true }
]
}
}

module.exports = { ShortFareHotel }
27 changes: 27 additions & 0 deletions ShortFareHotelToMongo.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

const { ShortFareHotel } = require('./ShortFareHotel.class')

class ShortFareHotelToMongo extends ShortFareHotel {
constructor (doc) {
super()

if (doc != null) {
this.tenantCode = doc.tenantCode
this.hotelCode = doc.hotelCode
this.rateCode = doc.rateCode
this.checkInDate = doc.checkInDate
this.checkOutDate = doc.checkOutDate
this.nights = doc.nights
this.guests = doc.guests
this.currencyCode = doc.currencyCode
this.totalPrice = doc.totalPrice
this.createdAt = doc.createdAt
this.createdAt = doc.createdAt
this.sourceId = doc.sourceId
}

Object.defineProperty(this, 'doc', { value: doc })
}
}

module.exports = { ShortFareHotelToMongo }

0 comments on commit 6f45c77

Please sign in to comment.