Skip to content

Commit dacc8bd

Browse files
committed
Refactor alert report model and update related routes for improved report handling
1 parent 31c6132 commit dacc8bd

5 files changed

Lines changed: 82 additions & 40 deletions

File tree

src/models/alert-report.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { database } from '..'
2+
3+
const tableName = 'alert_reports'
4+
5+
export interface AlertReport {
6+
id: number
7+
report_number: string
8+
created_on: string
9+
starts_on: string
10+
ends_on: string
11+
emitted_on: string
12+
estofex_sent: boolean
13+
pretemp_sent: boolean
14+
is_critic: boolean
15+
}
16+
17+
type EditableAlertReport = Omit<AlertReport, 'id'>
18+
19+
export const getLastAlertReport = async (): Promise<AlertReport> => {
20+
const query = `SELECT * FROM ${tableName} ORDER BY id DESC LIMIT 1`
21+
22+
const reports = await database.query<AlertReport>(query)
23+
24+
if (reports.length === 0) {
25+
throw new Error('No last alert report found')
26+
}
27+
28+
return reports[0]
29+
}
30+
31+
export const createAlertReport = async (report: EditableAlertReport): Promise<AlertReport> => {
32+
return database.create<AlertReport>(tableName, report)
33+
}
34+
35+
export const updateLastAlertReport = async (report: Partial<EditableAlertReport>, id: number): Promise<AlertReport> => {
36+
return database.edit<AlertReport>(tableName, report, id)
37+
}

src/models/last-alert-report.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/routes/forecast-reports.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { checkEstofexReport } from '../utilites/estofex'
33
import { getEstofexImage, getEstofexReport } from '../services/estofex'
44
import { getTomorrowPretempReport } from '../services/pretemp'
55
import { sendPhotoMessage } from '../services/telegram'
6-
import { getLastAlertReport, updateLastAlertReport } from '../models/last-alert-report'
6+
import { getLastAlertReport, updateLastAlertReport } from '../models/alert-report'
77

88
export const registerForecastReportsRoutes = (fastify) => {
99
fastify.route({
@@ -24,9 +24,12 @@ export const registerForecastReportsRoutes = (fastify) => {
2424

2525
await sendPhotoMessage(config.chat_id, tomorrowReport, 'Nuovo report Pretemp disponibile')
2626

27-
await updateLastAlertReport({
28-
pretemp_sent: true,
29-
})
27+
await updateLastAlertReport(
28+
{
29+
pretemp_sent: true,
30+
},
31+
lastAlertReport.id
32+
)
3033

3134
reply.status(204).send(undefined)
3235
},
@@ -53,9 +56,12 @@ export const registerForecastReportsRoutes = (fastify) => {
5356

5457
await sendPhotoMessage(config.chat_id, estofexImage, 'Nuovo report Estofex disponibile')
5558

56-
await updateLastAlertReport({
57-
estofex_sent: true,
58-
})
59+
await updateLastAlertReport(
60+
{
61+
estofex_sent: true,
62+
},
63+
lastAlertReport.id
64+
)
5965

6066
reply.status(204).send(undefined)
6167
},

src/routes/meteo-alerts.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { sendNewTomorrowAlertMessage } from '../utilites/telegram'
2-
import { getLastAlertReport, updateLastAlertReport } from '../models/last-alert-report'
2+
import { createAlertReport, getLastAlertReport } from '../models/alert-report'
33
import { getTomorrowMeteoAlert } from '../services/meteo-alerts'
44
import { parseMeteoAlert } from '../utilites/meteo-alerts'
55

@@ -18,14 +18,20 @@ export const registerMeteoAlertsRoutes = (fastify) => {
1818

1919
const lastAlertReport = await getLastAlertReport()
2020

21-
if (lastAlertReport.report_id !== parsedAlert.id) {
21+
if (lastAlertReport.report_number !== parsedAlert.id) {
2222
if (parsedAlert.isCritic) {
2323
sendNewTomorrowAlertMessage(parsedAlert)
2424
}
2525

26-
await updateLastAlertReport({
27-
report_id: parsedAlert.id,
26+
await createAlertReport({
27+
report_number: parsedAlert.id,
2828
is_critic: parsedAlert.isCritic,
29+
estofex_sent: false,
30+
pretemp_sent: false,
31+
created_on: new Date().toISOString(),
32+
starts_on: parsedAlert.dataInizio,
33+
ends_on: parsedAlert.dataFine,
34+
emitted_on: parsedAlert.dataEmissione,
2935
})
3036
}
3137

src/services/postgresql.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ export default class PostgreSQL {
9393
}
9494
}
9595

96+
public async create<T = any>(tableName: string, object: Omit<T, 'id'>) {
97+
const keys = Object.keys(object)
98+
.map((key) => checkAndTransformKey(key))
99+
.join(', ')
100+
101+
const values: any[] = Object.values(object)
102+
103+
const query = `INSERT INTO ${checkAndTransformKey(
104+
tableName
105+
)} (${keys}) VALUES (${values.map((_, i) => `$${i + 1}`).join(', ')}) RETURNING *`
106+
107+
const rows = await this.query<T>(query, values)
108+
109+
return rows[0]
110+
}
111+
96112
public async edit<T>(tableName: string, object: Omit<Partial<T>, 'id'>, objectId: number) {
97113
const keys = Object.keys(object).map((key, index) => `${checkAndTransformKey(key)} = $${index + 1}`)
98114

@@ -104,6 +120,12 @@ export default class PostgreSQL {
104120

105121
return rows[0]
106122
}
123+
124+
public async delete(tableName: string, itemId: number) {
125+
const query = `DELETE FROM ${checkAndTransformKey(tableName)} WHERE id = $1`
126+
127+
await this.query<void>(query, [itemId])[0]
128+
}
107129
}
108130

109131
// Add backtick to sql reserved keywords

0 commit comments

Comments
 (0)