Skip to content

Commit b93f20b

Browse files
committed
feat: extend store to work with alerts, tasks and results
1 parent 2ae55af commit b93f20b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/store/index.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,45 @@ const getAllComplianceChecks = knex => async () => {
5151
return knex('compliance_checks').select().returning('*')
5252
}
5353

54+
const getCheckByCodeName = knex => async (codeName) => {
55+
debug(`Getting check by code name (${codeName})...`)
56+
return knex('compliance_checks').where({ code_name: codeName }).first()
57+
}
58+
59+
const getAllProjects = knex => async () => {
60+
debug('Getting all projects...')
61+
return knex('projects').select().returning('*')
62+
}
63+
64+
const deleteAlertsByComplianceCheckId = knex => async (complianceCheckId) => {
65+
debug(`Deleting alerts by compliance_check_id (${complianceCheckId})...`)
66+
return knex('compliance_checks_alerts').where({ compliance_check_id: complianceCheckId }).delete()
67+
}
68+
69+
const deleteTasksByComplianceCheckId = knex => async (complianceCheckId) => {
70+
debug(`Deleting tasks by compliance_check_id (${complianceCheckId})...`)
71+
return knex('compliance_checks_tasks').where({ compliance_check_id: complianceCheckId }).delete()
72+
}
73+
74+
const addAlert = knex => async (alert) => {
75+
debug('Inserting alert...')
76+
return knex('compliance_checks_alerts').insert(alert).returning('*')
77+
}
78+
79+
const addTask = knex => async (task) => {
80+
debug('Inserting task...')
81+
return knex('compliance_checks_tasks').insert(task).returning('*')
82+
}
83+
84+
const upsertComplianceCheckResult = knex => async (result) => {
85+
const existingComplianceCheck = await knex('compliance_checks_results').where({ compliance_check_id: result.compliance_check_id }).first()
86+
if (existingComplianceCheck) {
87+
return knex('compliance_checks_results').where({ compliance_check_id: result.compliance_check_id }).update(result).returning('*')
88+
} else {
89+
return knex('compliance_checks_results').insert(result).returning('*')
90+
}
91+
}
92+
5493
const initializeStore = (knex) => {
5594
debug('Initializing store...')
5695
return {
@@ -59,7 +98,14 @@ const initializeStore = (knex) => {
5998
getAllGithubOrganizations: getAllGithubOrganizations(knex),
6099
updateGithubOrganization: updateGithubOrganization(knex),
61100
upsertGithubRepository: upsertGithubRepository(knex),
62-
getAllComplianceChecks: getAllComplianceChecks(knex)
101+
getAllComplianceChecks: getAllComplianceChecks(knex),
102+
getCheckByCodeName: getCheckByCodeName(knex),
103+
getAllProjects: getAllProjects(knex),
104+
deleteTasksByComplianceCheckId: deleteTasksByComplianceCheckId(knex),
105+
deleteAlertsByComplianceCheckId: deleteAlertsByComplianceCheckId(knex),
106+
addAlert: addAlert(knex),
107+
addTask: addTask(knex),
108+
upsertComplianceCheckResult: upsertComplianceCheckResult(knex)
63109
}
64110
}
65111

0 commit comments

Comments
 (0)