Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
2.0 kB
1
Indexable
Never
app.put('/api/sdh/de-tai', app.permission.check('sdhDeTai:write'), async (req, res) => {
    try {
      const { data, id } = req.body;
      const deTaiData = {
        'tenDeTai': data.tenDeTai,
        'mssv': data.mssv,
        'ghiChu': data.ghiChu,
        'noiDung': data.noiDung
      };

      const soQuyetDinhData = {
        'ten': data.tenSqd,
        'time': Date.now(),
        'idDeTai': id,
        'id': data.soQuyetDinh
      };

      for (let i = 0; i < data.cbhd.length; i++) {
        const item = data.cbhd[i];
        if (!item.shcc && item.hoTen && item.trinhDo) {
          const [firstName, ...lastNameParts] = item.hoTen.split(' ');
          const lastName = lastNameParts.join(' ');
          const temp = await app.model.sdhCanBo.create({
            'ho': firstName,
            'ten': lastName,
            'trinhDo': item.trinhDo
          });
          data.cbhd[i] = { ...res, shcc: temp.id, ...data.cbhd[i], belongTo: 'SDHCB' };
          delete data.cbhd[i].id;
        }
      }

      await app.model.sdhCbhd.delete({ mssv: data.mssv });
      await Promise.all([
        app.model.sdhSoQuyetDinh.update({ idDeTai: id, active: 0 }, { active: 1 }),
        app.model.sdhDeTai.update({ id }, deTaiData),

        app.model.sdhSoQuyetDinh.create(soQuyetDinhData),
        ...data.cbhd.flatMap(item => [
          app.model.sdhCbhd.create({
            'vaiTro': item.vaiTro,
            'idCanBo': item.shcc.split(' - ').length > 1 ? item.shcc.split(' - ')[0] : item.shcc,
            'belongTo': item.shcc.split(' - ').length > 1 ? item.shcc.split(' - ')[1] : item.shcc,
            'mssv': data.mssv
          }),
          app.model.sdhSoQuyetDinhDetail.create({
            'soQuyetDinh': soQuyetDinhData.id,
            'tenDeTai': data.tenDeTai,
          })
        ])
      ]);

      res.send({ 'message': 'success' });
    } catch (error) {
      app.consoleError(req, error);
      res.send({ error });
    }
  });
Leave a Comment