Skip to content

Commit

Permalink
skip cleaning up devices of pro plan customers
Browse files Browse the repository at this point in the history
  • Loading branch information
csuermann committed Jan 7, 2025
1 parent 1258a93 commit 68013e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cleanup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AWS = require('aws-sdk')
import { getDevicesOfThing } from './db'
import { getDevicesOfThing, getUserRecord, postponeTokenDeletion } from './db'
import { handleBackchannelBulkUndiscover } from './handler'

AWS.config.update({ region: process.env.VSH_IOT_REGION })
Expand Down Expand Up @@ -148,6 +148,16 @@ async function totallyCleanUpThing(thingName) {
const data: any = await describeThing(thingName)
const { userId } = data.attributes

//skip cleaning up things belonging to pro plan customers:
const account = await getUserRecord(userId)
if (account.plan === 'pro') {
await postponeTokenDeletion(userId, 60)

throw new Error(
`skipping PRO plan customer ${userId}, thingId ${thingName}`
)
}

const devices = await getDevicesOfThing(userId, thingName)
console.log('devices::', devices.length)

Expand Down
27 changes: 27 additions & 0 deletions db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ export function upsertTokens(
})
}

export function postponeTokenDeletion(
userId: string,
daysFromNow: number
): Promise<any> {
const params = {
TableName: 'VSH',
Key: {
PK: `USER#${userId}`,
SK: 'TOKEN',
},
UpdateExpression: 'set deleteAtUnixTime = :ttl',
ExpressionAttributeValues: {
':ttl': dayjs().add(daysFromNow, 'day').unix(),
},
}

return new Promise((resolve, reject) => {
docClient.update(params, function (err, data) {
if (err) {
return reject(err)
} else {
return resolve(data)
}
})
})
}

export function updateUserRecord(partialUser: PartialUserRecord): Promise<any> {
const updateRec = { ...partialUser }

Expand Down

0 comments on commit 68013e7

Please sign in to comment.