Skip to content

Commit

Permalink
Skip cleanup of things belonging to PRO users (#32)
Browse files Browse the repository at this point in the history
* skip cleaning up devices of pro plan customers

* use serverless/[email protected]
  • Loading branch information
csuermann authored Jan 11, 2025
1 parent 4989864 commit b3ba997
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sandbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install dependencies
run: pnpm install
- name: Serverless deploy to ${{ matrix.region }}
uses: serverless/github-action@v3
uses: serverless/github-action@v3.2
with:
args: deploy --region ${{ matrix.region }}
env:
Expand All @@ -49,6 +49,6 @@ jobs:
STRIPE_WEBHOOK_SECRET: ${{ secrets.STRIPE_WEBHOOK_SECRET_SANDBOX }}
MOMENTO_TOKEN: ${{ secrets.MOMENTO_TOKEN_SANDBOX }}
- name: Serverless doctor advice
uses: serverless/github-action@v3
uses: serverless/github-action@v3.2
with:
args: doctor
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 b3ba997

Please sign in to comment.