generated from ctc-uci/npo-backend-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeScheduler.js
71 lines (64 loc) · 2 KB
/
nodeScheduler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const aws = require('aws-sdk');
const path = require('path');
const { db } = require('./server/db');
const s3 = new aws.S3({
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
signatureVersion: 'v4',
});
const DeleteS3Object = async (imageUrl) => {
try {
const imageName = path.basename(imageUrl);
const params = {
Bucket: process.env.S3_BUCKET_NAME,
Key: imageName,
};
await s3.deleteObject(params).promise();
} catch (error) {
// console.log('Error deleting s3 object: ', error);
}
};
const deleteRoutes = async () => {
try {
await db.query(
`DELETE FROM routes AS r WHERE (r.date < CURRENT_DATE - 15) AND NOT EXISTS (SELECT * FROM donations AS d WHERE d.route_id = r.id AND d.status != 'archived')`,
);
} catch (err) {
// console.error('Error deleting routes: ', err);
}
};
const deletePictures = async () => {
try {
const pictures = await db.query(
`SELECT image_url FROM pictures WHERE ( donation_id IN (SELECT id FROM donations WHERE status = 'archived' AND last_edited_date < CURRENT_DATE - 30))`,
);
pictures.forEach((picture) => {
// delete corresponding s3 object
DeleteS3Object(picture.image_url);
});
// deletes the pictures from PNP database
await db.query(
`DELETE FROM pictures WHERE ( donation_id IN (SELECT id FROM donations WHERE status = 'archived' AND last_edited_date < CURRENT_DATE - 30))`,
);
} catch (err) {
// console.error('Error deleting pictures: ', err);
}
};
const updateSchedulingStatus = async () => {
try {
await db.query(
`UPDATE donations
SET status = 'reschedule'
WHERE route_id IN (
SELECT id
FROM routes
WHERE date = CURRENT_DATE
)
AND status = 'scheduling'`,
);
} catch (err) {
// console.error('Error updating status: ', err);
}
};
module.exports = { DeleteS3Object, deleteRoutes, deletePictures, updateSchedulingStatus };