-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpostCancelCorner.js
More file actions
64 lines (53 loc) · 1.6 KB
/
postCancelCorner.js
File metadata and controls
64 lines (53 loc) · 1.6 KB
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
import { https, logger } from 'firebase-functions/v2';
import { getFirestore } from 'firebase-admin/firestore';
import { getStorage } from 'firebase-admin/storage';
import { safelyInitializeApp } from '../firebase.js';
const config = safelyInitializeApp();
const db = getFirestore();
export const cancelCorner = async (data, uid) => {
logger.info('validating corner cancellation', data, uid, {
structuredData: true,
});
if (!data.key) {
throw new https.HttpsError(
'invalid-argument',
'corner submission data is invalid',
);
}
try {
const reference = db.collection('submissions').doc(data.key);
const snapshot = await reference.get();
logger.debug('cancelling submission', data.key, {
structuredData: true,
});
await reference.set(
{ status: { user: { cancelled: new Date() } } },
{ merge: true },
);
await removeStorage(uid, snapshot.data());
} catch (error) {
logger.error('error finding submission', error, {
structuredData: true,
});
throw new https.HttpsError('internal', 'The submission was not cancelled');
}
return 1;
};
export const removeStorage = async (user, submission) => {
const bucket = getStorage().bucket(config.storageBucket);
const prefix = `submitters/${user}/${submission.type}/${submission.blm_point_id}/`;
logger.debug('deleting submission files', prefix, {
structuredData: true,
});
await bucket.deleteFiles(
{
force: true,
prefix,
},
(error) => {
logger.warn('had trouble deleting file', error, {
structuredData: true,
});
},
);
};