const axios = require('axios'); const models = require('vn-loopback/server/server').models; module.exports = Self => { Self.remoteMethod('getVideoList', { description: 'Get video list of expedition id', accessType: 'READ', accepts: [ { arg: 'id', type: 'number', required: true, description: 'Expedition id' }, { arg: 'from', type: 'number', required: false, }, { arg: 'to', type: 'number', required: false, } ], returns: { type: ['object'], root: true }, http: { path: `/getVideoList`, verb: 'GET', }, }); Self.getVideoList = async(id, from, to, options) => { const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const packingSiteConfig = await models.PackingSiteConfig.findOne({}, myOptions); const query = ` SELECT e.id, ps.monitorId, e.created FROM expedition e JOIN host h ON Convert(h.code USING utf8mb3) COLLATE utf8mb3_unicode_ci = e.hostFk JOIN packingSite ps ON ps.hostFk = h.id WHERE e.id = ?;`; const [expedition] = await models.PackingSiteConfig.rawSql(query, [id]); if (!from && !expedition) return []; let start = new Date(expedition.created); let end = new Date(start.getTime() + (packingSiteConfig.avgBoxingTime * 1000)); if (from && to) { start.setHours(from, 0, 0); end.setHours(to, 0, 0); } const offset = start.getTimezoneOffset(); start = new Date(start.getTime() - (offset * 60 * 1000)); end = new Date(end.getTime() - (offset * 60 * 1000)); const videoUrl = `/${packingSiteConfig.shinobiToken}/videos/${packingSiteConfig.shinobiGroupKey}/${expedition.monitorId}`; const timeUrl = `?start=${start.toISOString().split('.')[0]}&end=${end.toISOString().split('.')[0]}`; const url = `${packingSiteConfig.shinobiUrl}${videoUrl}${timeUrl}`; let response; try { response = await axios.get(url); } catch (e) { return []; } return response.data.videos.map(video => video.filename); }; };