import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user }) {
  const datas = await dataSource.query(`
	select
	count(q.id) as total,
	qt.name
from
	question q
left join question_type qt on
	q.type = qt.id
where q.del_flag =0 ${
    user.baseRole === 2 ? "and q.creator = '" + user.userId + "'" : ''
  }
group by
	q.type

  `);
  const total = datas.reduce((prev, item) => {
    return prev + parseInt(item.total);
  }, 0);
  const formatDatas = datas.map((item) => {
    return {
      item: item.name,
      count: +item.total,
      percent: +item.total / total,
      total: total,
    };
  });
  return formatDatas;
}
