-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsents.mjs
45 lines (38 loc) · 1.12 KB
/
consents.mjs
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
/*
* consents.js
*/
import db from "./db.mjs";
export const getTermsConsent = async (ipAddress, version) => {
if (!ipAddress || !version) return false;
const params = [ipAddress, parseInt(version, 10)];
const res = await db.findOne(
`SELECT "consent" FROM term_consents_ip WHERE "ip_addr" = $1 AND "version" = $2`,
params);
if (!res) {
await db.insert(
`
INSERT INTO term_consents_ip ("ip_addr", "version", "consent")
VALUES ($1, $2, false)
ON CONFLICT ON CONSTRAINT term_consents_ip_ip_addr_key DO
UPDATE SET version = $2, consent = false, ts = (now() at time zone 'utc')
`,
params);
return false;
}
return res.consent;
};
export const setTermsConsent = (ipAddress, version, consent) => {
const params = [ipAddress, parseInt(version), Boolean(consent)];
return db.insert(
`
INSERT INTO term_consents_ip ("ip_addr", "version", "consent")
VALUES ($1, $2, $3)
ON CONFLICT ON CONSTRAINT term_consents_ip_ip_addr_key DO
UPDATE SET "version" = $2, "consent" = $3
`,
params);
}
export default {
getTermsConsent,
setTermsConsent,
};