import { DataSource, Repository } from 'typeorm';
export async function handler(dataSource: DataSource, { user }) {
  const data = await dataSource.query(`
  SELECT
	count( csc.id ) AS coursewareCount,
	SUM( csc.study_time ) AS studyTime,
	(
	SELECT
		count( course_section_content.id ) AS section 
	FROM
		course_list
		LEFT JOIN course_section_content ON course_list.id = course_section_content.course_id 
	WHERE
		course_list.del_flag = 0 
		AND course_list.study_org_id = ${user.orgId}
	) AS section,
	
	(
	SELECT
		COUNT(qfc.id) AS section 
	FROM
		course_list
		LEFT JOIN questions_for_course qfc ON course_list.id = qfc.course_id AND qfc.del_flag = 0	
		WHERE
		course_list.del_flag = 0 
		AND course_list.study_org_id = ${user.orgId}
	) AS exercises 
	
FROM
	course_list
	LEFT JOIN course_section_courseware csc ON course_list.id = csc.course_id 
	AND csc.del_flag = 0 
WHERE
	course_list.del_flag = 0 
	AND course_list.study_org_id = ${user.orgId}
      `);
  return data[0];
}
