Untitled

 avatar
user_4058768
plain_text
16 days ago
3.7 kB
1
Indexable
Never
module.exports = app => {
  const menu = {
    parentMenu: app.parentMenu.sdh,
    menus: {
      7642: {
        title: 'Quản lý đề tài (2)', link: '/user/sau-dai-hoc/de-tai', parentKey: 7500
      }
    }
  };

  app.permission.add(
    { name: 'sdhDeTai:read', menu },
    { name: 'sdhDeTai:write' },
    { name: 'sdhDeTai:delete' },
  );

  app.get('/user/sau-dai-hoc/de-tai', app.permission.check('sdhDeTai:read'), app.templates.admin);

  //APIs----------------------------------------------------------------------------------------------------------------------------------
  app.get('/api/sdh/de-tai', app.permission.check('sdhDeTai:read'), async (req, res) => {
    try {
      const items = await app.model.sdhDeTai.getAll({}, '*', 'ID');
      res.send({ items });
    } catch (error) {
      res.send({ error });
    }
  });

  app.get('/api/sdh/de-tai/page/:pageNumber/:pageSize', app.permission.check('sdhDeTai:read'), async (req, res) => {
    try {
      const { pageNumber, pageSize } = req.params;
      let { filter } = req.query || {};
      filter = filter || {};

      const { rows: list, pagenumber, pagesize, pagetotal, totalitem, newestsoquyetdinh } = await app.model.sdhDeTai.searchPage(Number(pageNumber), Number(pageSize), app.utils.stringify(filter));
      list.forEach(item => {
        if (item.canBo) {
          console.log('Original canBo:', item.canBo);
          try {
            let cleanedString = item.canBo
              .replace(/\\"/g, '"')
              .replace(/""/g, '"');

            item.canBo = JSON.parse(cleanedString);
          } catch (err) {
            console.error('Error parsing canBo JSON:', err);
            console.error('Problematic JSON string:', item.canBo);
            item.canBo = [];
          }
        } else {
          item.canBo = [];
        }
      });

      res.send({ page: { list, pageNumber: pagenumber, pageSize: pagesize, pageTotal: pagetotal, totalItem: totalitem, newestSoQuyetDinh: newestsoquyetdinh } });
    } catch (error) {
      app.consoleError(error);
      res.send({ error });
    }
  });


  // app.post('/api/sdh/de-tai', app.permission.check('sdhDeTai:write'), async (req, res) => {
  //   try {
  //     const { data } = req.body;
  //     const deTaiItem = await app.model.sdhDeTai.create(data);

  //     const soQuyetDinhData = {
  //       'ten': 'Duyệt đề tài',
  //       'time': Date.now(),
  //       'idDeTai': deTaiItem.id
  //     };

  //     const soQuyetDinhItem = await app.model.sdhSoQuyetDinh.create(soQuyetDinhData);
  //     const item = {
  //       deTai: deTaiItem,
  //       soQuyetDinh: soQuyetDinhItem
  //     };

  //     res.send({ item });
  //   } catch (error) {
  //     app.consoleError(req, error);
  //     res.send({ error });
  //   }
  // });
  app.post('/api/sdh/de-tai', app.permission.check('sdhDeTai:write'), async (req, res) => {
    try {
      const { data, id } = req.body;
      if (data.mode == 1) {
        await app.model.sdhSoQuyetDinh.update({ idDeTai: id }, data);
      }
      const deTaiItem = await app.model.sdhDeTai.create(data);

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

      const soQuyetDinhItem = await app.model.sdhSoQuyetDinh.create(soQuyetDinhData);
      const item = {
        deTai: deTaiItem,
        soQuyetDinh: soQuyetDinhItem
      };

      res.send({ item });
    } catch (error) {
      app.consoleError(req, error);
      res.send({ error });
    }
  });

};
Leave a Comment