{"size": 1548, "ext": "js", "max_stars_repo_path": "src/api/src/http/submit-completed-templates/_save-submission.js", "max_stars_repo_name": "SAEON/national-climate-change-systems", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/api/src/http/submit-completed-templates/_save-submission.js", "max_issues_repo_name": "SAEON/national-climate-change-systems", "max_issues_repo_issues_event_min_datetime": "2021-07-27T14:16:47.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T12:30:27.000Z", "max_forks_repo_path": "src/api/src/http/submit-completed-templates/_save-submission.js", "max_forks_repo_name": "SAEON/national-climate-change-systems", "max_forks_repo_forks_event_min_datetime": "2021-08-31T10:44:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-31T10:44:59.000Z"}
{"max_stars_count": null, "max_issues_count": 30, "max_forks_count": 1, "avg_line_length": 30.96, "max_line_length": 79, "alphanum_fraction": 0.4799741602}
import { pool } from '../../mssql/pool.js'

export default async (userId, { project, mitigation = {}, adaptation = {} }) =>
  await pool
    .connect()
    .then(pool =>
      pool
        .request()
        .input('project', JSON.stringify(project))
        .input('mitigation', JSON.stringify(mitigation))
        .input('adaptation', JSON.stringify(adaptation))
        .input('submissionStatus', JSON.stringify({ term: 'pending' }))
        .input('createdBy', userId)
        .input('createdAt', new Date().toISOString()).query(`
            merge Submissions t
            using (
              select
                @project project,
                @mitigation mitigation,
                @adaptation adaptation,
                1 isSubmitted,
                @submissionStatus submissionStatus,
                @createdBy createdBy,
                @createdAt createdAt
            ) s on t.id = null
            when not matched then insert (
              project,
              mitigation,
              adaptation,
              isSubmitted,
              submissionStatus,
              createdBy,
              createdAt
            )
            values (
              s.project,
              s.mitigation,
              s.adaptation,
              s.isSubmitted,
              s.submissionStatus,
              s.createdBy,
              s.createdAt
            )
            output
              $action,
              inserted.id,
              inserted._projectTitle;`)
    )
    .then(result => ({ ...result.recordset[0] }))
