Upsert.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const API = require("../../../../lib/API");
  2. const AccessControl = require("../../../../lib/AccessControl");
  3. const { BaseStdResponse } = require("../../../../BaseStdResponse");
  4. const VersionService = require("../../../../lib/DownloadVersionService");
  5. class AdminDownloadVersionUpsert extends API {
  6. constructor() {
  7. super();
  8. this.setPath('/Admin/DownloadVersion');
  9. this.setMethod('POST');
  10. }
  11. async onRequest(req, res) {
  12. const { uuid, session, client_type, version_name, download_url } = req.body;
  13. if ([uuid, session, client_type, version_name, download_url].some(v => v === '' || v === null || v === undefined))
  14. return res.json({ ...BaseStdResponse.MISSING_PARAMETER });
  15. if (!await AccessControl.checkSession(uuid, session))
  16. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED });
  17. const permission = await AccessControl.getPermission(uuid);
  18. if (!permission.includes('admin') && !permission.includes('service'))
  19. return res.json({ ...BaseStdResponse.PERMISSION_DENIED });
  20. try {
  21. const { id, result } = await VersionService.saveVersion(req.body);
  22. if (!result || result.affectedRows === 0)
  23. return res.json({ ...BaseStdResponse.ERR, msg: '保存下载版本失败' });
  24. return res.json({ ...BaseStdResponse.OK, id });
  25. } catch (err) {
  26. this.logger.error(`保存下载版本失败:${err.stack || err}`);
  27. return res.json({ ...BaseStdResponse.ERR, msg: '保存下载版本失败' });
  28. }
  29. }
  30. }
  31. module.exports.AdminDownloadVersionUpsert = AdminDownloadVersionUpsert;