salix/modules/ticket/back/methods/boxing/getVideoList.js

79 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

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: {
2022-09-07 11:58:39 +00:00
type: ['object'],
root: true
},
http: {
path: `/getVideoList`,
verb: 'GET',
},
});
Self.getVideoList = async(id, from, to, options) => {
const myOptions = {};
2022-09-05 10:06:14 +00:00
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]);
2022-09-07 11:58:39 +00:00
if (!from && !expedition) return [];
let start = new Date(expedition.created);
let end = new Date(start.getTime() + (packingSiteConfig.avgBoxingTime * 1000));
2023-04-26 10:03:17 +00:00
if (from != undefined && to != undefined) {
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));
2022-09-06 13:08:28 +00:00
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}`;
2022-09-05 10:06:14 +00:00
let response;
2022-09-05 10:06:14 +00:00
try {
response = await axios.get(url);
} catch (e) {
return [];
}
return response.data.videos.map(video => video.filename);
};
};