UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

84 lines (83 loc) 3.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = handle; const types_1 = require("../util/types"); const util_1 = require("../../../util"); const storage_1 = require("../../../storage"); function getHumanETA(ms) { const seconds = Math.floor(ms / 1000); let minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); if (seconds < 60) { return 'Less than a minute remaining'; } minutes = minutes % 60; const timeParts = []; if (hours) { timeParts.push(`${hours} ${hours > 1 ? 'hours' : 'hour'}`); } if (minutes) { timeParts.push(`${minutes} ${minutes > 1 ? 'minutes' : 'minute'}`); } return `About ${timeParts.join(' and ')} remaining`; } function handle() { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { const isApi = req.isApiCall; const id = req.query.jobId || req.body.jobId; if (!id) { if (isApi) { return res.status(400).json({ error: { message: 'jobId is required', }, }); } return res.status(400).send('jobId is required'); } req.logInfo(`Get job info for id ${id}`); const job = yield (0, storage_1.getStorage)().getJob({ id }); if (isApi && !job) { return res.status(404).json({ error: { message: `Job with ID ${id} not found`, }, }); } if (job && job.status === types_1.JobStatus.IN_PROGRESS && job.progress > 5 && job.updatedAt) { job.eta = ((Date.now() - job.createdAt) / job.progress) * (100 - job.progress); job.info = getHumanETA(job.eta) + (job.info ? `\n${job.info}` : ''); } if (isApi && job) { const baseJob = { id: job.id, info: job.info, data: job.data, attempt: job.attempt, payload: job.payload, eta: job.eta, progress: job.progress, status: job.status, title: job.title, type: job.type, createdAt: new Date(+job.createdAt).toISOString(), updatedAt: job.updatedAt ? new Date(+job.updatedAt).toISOString() : null, finishedAt: job.finishedAt ? new Date(+job.finishedAt).toISOString() : null, }; req.logInfo(`Returning filtered job info ${JSON.stringify(baseJob, null, 2)}`); res.send(baseJob); return; } req.logInfo(`Returning job info ${JSON.stringify(job, null, 2)}`); res.send(job); })); }