Skip to content

Commit

Permalink
Finished updating unsubscribe to log the event
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Jackson committed Nov 29, 2024
1 parent 4dac825 commit b7b9b40
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions packages/server/util/unsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function unsubscribe(phone, reason, dbClientIn) {
if (res.rowCount > 0) { // Otherwise there's nothing to do
const { id } = res.rows[0];
const phone4 = phone.substring(phone.length - 4);
console.log('Start the transaction');

pgClient.query('BEGIN');
try {
res = await pgClient.query(
Expand All @@ -35,20 +35,18 @@ async function unsubscribe(phone, reason, dbClientIn) {
[id],
);
const defendants = res.rows;
console.log('Defendants: ', defendants);
for (let i = 0; i < defendants.length; i += 1) {
const d = defendants[i];
console.log('D: ', d);
res = await pgClient.query(
`SELECT * FROM ${schema}.subscriptions WHERE defendant_id = $1`,
[d.id],
);
console.log('Got the result of all subs for this defendant: ', res);

if (res.rowCount === 1) { // Delete if this is the only subscriber
await pgClient.query(`DELETE FROM ${schema}.cases WHERE defendant_id = $1`, [d.id]);
await pgClient.query(`DELETE FROM ${schema}.defendants WHERE id = $1`, [d.id]);
}
console.log('Now log it');

// Log it
res = await pgClient.query(
`
Expand All @@ -58,12 +56,9 @@ async function unsubscribe(phone, reason, dbClientIn) {
`,
[phone4, d.long_id, reason, d.original_subscribe_date, d.last_valid_cases_date],
);
console.log('It is now logged');
}
console.log('Delete the final stuff');
await pgClient.query(`DELETE FROM ${schema}.subscriptions WHERE subscriber_id = $1`, [id]);
await pgClient.query(`DELETE FROM ${schema}.subscribers WHERE id = $1`, [id]);
console.log('Now commit');
pgClient.query('COMMIT');
} catch (err) {
logger.error(`Error unsubscribing phone ending in ${phone.substring(phone.length - 4)} - rolling back: ${err}`);
Expand Down

0 comments on commit b7b9b40

Please sign in to comment.