/
var
/
www
/
qr4y.kz
/
server
/
delivery
/
services
/
/var/www/qr4y.kz/server/delivery/services
mkdir
upload
Name
Size
Mode
Actions
firebase/
-
0755
rm
branch.service.js
6823
0644
edit
dl
rm
index.js
486
0644
edit
dl
rm
order.service.js
15023
0644
edit
dl
rm
webSocket.service.js
4563
0644
edit
dl
rm
Edit:
/var/www/qr4y.kz/server/delivery/services/branch.service.js
(6823B)
const logger = require('../logger')({ service: 'OrderService' }) const { Op } = require('sequelize') const PointInPolygon = require('point-in-polygon') const { isSet, isArray } = require('../api/utils/validator.util') module.exports = class BranchService { constructor({ db, config }) { this.db = db this.config = config this.branches = VARS.branches logger.child({ service: 'BranchService', method: 'Constructor' }).info('Success') } async Run() { // загрузить отделения this.branches = await this.db.branch.findAll({ include: [ { model: this.db.company, as: 'company', where: { deletedAt: null }, include: [this.db.companyType, this.db.companyImage] }, this.db.workTime, this.db.deliveryType, this.db.address, this.db.shippingRate, this.db.room ] }) logger .child({ service: 'BranchService', method: 'Run' }) .info(`Branches loaded: ${this.branches?.length || 0}.`) } async Stop() { this.interval = null this.branches = [] logger.child({ service: 'BranchService', method: 'Stop' }).info('Success') } async LoadBranch(uid) { return await this.db.branch.findOne({ where: { uid }, include: [ { model: this.db.company, as: 'company', where: { deletedAt: null }, include: [this.db.companyType, this.db.companyImage] }, this.db.workTime, this.db.deliveryType, this.db.address, this.db.shippingRate, this.db.room ] }) } async GetBranch(uid) { let branch = this.branches.find((x) => x.uid === uid) if (!branch) { branch = await this.LoadBranch(uid) if (!branch) { logger .child({ service: 'BranchService', method: 'GetBranch', uid: uid }) .info('Not found') return } await this.AddBranch(branch) logger .child({ service: 'BranchService', method: 'GetBranch', uid: branch.uid }) .info('Success') } return branch } async AddBranch(branch) { if (branch) { const idx = this.branches.findIndex((p) => p.uid == branch.uid) if (idx === -1) { this.branches.push(branch) logger .child({ service: 'BranchService', method: 'AddBranch', uid: branch.uid }) .info('Success') }else{ this.branches[idx] = branch } } } async ReloadBranch(uid) { let branch = await this.LoadBranch(uid) await this.AddBranch(branch) return branch } async RemoveBranch(uid) { const idx = this.branches.findIndex((p) => p.uid == uid) if (idx !== -1) { this.branches.splice(idx, 1) logger .child({ service: 'BranchService', method: 'RemoveBranch', uid: uid }) .info('Success') } } async GetBranches() { logger.child({ service: 'BranchService', method: 'GetBranches' }).info('Success') return this.branches } async GetBranchesByArea({ active, working, latitude, longtitude, companyTypeId, page, limit, sort }) { let listAll = [] for (let branch of this.branches) { if(!branch.active) continue; if(branch.company != null && !branch.company.enabled) continue; if(branch.company != null && !branch.company.active) continue; let found = true; if(companyTypeId > 0){ found = false; } if(branch.company!=null && branch.company.companyTypes!=null){ for(let ct of branch.company.companyTypes){ if(ct.id == companyTypeId){ found = true; } } } if (!found || branch.area == null || branch.area == '') continue let polygon = JSON.parse(branch.area) let area = [] if (isArray(polygon) && polygon[0].length > 0) { for (let p of polygon) { if (isSet(p) && typeof p === 'string') { const point = p.split(',') area.push([parseFloat(point[0]), parseFloat(point[1])]) } } if(latitude != '' && latitude != '0.0'){ let res = PointInPolygon([latitude, longtitude], area) if (res) { const workTime = await this.GetBranchWorkTime(branch) branch.dataValues.working = workTime.working branch.dataValues.working_text = workTime.text listAll.push(branch) } }else{ // Если нет координат отдаем по лимиту const workTime = await this.GetBranchWorkTime(branch) branch.dataValues.working = workTime.working branch.dataValues.working_text = workTime.text listAll.push(branch) } } } if (listAll.length > 0) { if (isSet(active)) { listAll = listAll.filter((p) => p.active == (active === 'true' || active == 1)) } if (isSet(working)) { listAll = listAll.filter( (p) => p.dataValues.working == (working === 'true' || working == 1) ) } listAll.sort((a, b) => { return ( (a.id == b.id ? 0 : a.id > b.id ? 1 : -1) * (sort.toLowerCase() === 'desc' ? -1 : 1) ) }) return listAll.slice(page - 1 * limit, limit * page) } return listAll } async GetBranchByDistance({ latitude, longtitude }) {} GetBranchWorkTime(branch) { switch (branch.work_mode) { case '24hours': return { working: true, text: '24/7' } break case 'graphic': const now = new Date() const day = now.getDay() const hour = now.getHours() const minute = now.getMinutes() let text = '-' let working = false for (let wt of branch.workTimes) { if (wt.dayOfWeek == day) { const start_time = wt.startTime.split(':') const finish_time = wt.finishTime.split(':') const start_time_break = wt.startTimeBreak.split(':') const finish_time_break = wt.finishTimeBreak.split(':') var Time = function (timeString) { var t = timeString.split(':') this.hour = parseInt(t[0]) this.minutes = parseInt(t[1]) this.isBiggerThan = function (other) { return ( this.hour > other.hour || (this.hour === other.hour && this.minutes > other.minutes) ) } } var timeIsBetween = function (start, end, check) { return start.hour <= end.hour ? check.isBiggerThan(start) && !check.isBiggerThan(end) : (check.isBiggerThan(start) && check.isBiggerThan(end)) || (!check.isBiggerThan(start) && !check.isBiggerThan(end)) } var openTime = new Time(wt.startTime) var closeTime = new Time(wt.finishTime) var breakTime = new Time(wt.startTimeBreak) var unbreakTime = new Time(wt.finishTimeBreak) var checkTime = new Time(hour + ':' + minute) var isBetweenOpen = timeIsBetween(openTime, closeTime, checkTime) var isBetweenBreak = timeIsBetween(breakTime, unbreakTime, checkTime) text = wt.startTime + '-' + wt.finishTime // "isBetween: "+isBetweenOpen+" isBreak: "+isBetweenBreak if (isBetweenOpen && !isBetweenBreak) working = true } } return { working: working, text: text } default: return { working: false, text: '' } } } }
Save
cmd:
run