Skip to content

Commit

Permalink
add /user/:userId/info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
csuermann committed Oct 7, 2023
1 parent 0c972a2 commit 3f2f85b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions Authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface UserRecord {
allowedDeviceCount?: number
plan?: string
stripeCustomerId?: string
deleteAtUnixTime: number
}

export type PartialUserRecord = Pick<UserRecord, 'userId'> & Partial<UserRecord>
Expand Down
42 changes: 42 additions & 0 deletions admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,48 @@ app.post('/thing/:thingName/rediscover', async function (req, res) {
}
})

app.get('/user/:userId/info', async function (req, res) {
try {
const userRecord = await getUserRecord(req.params.userId)
const account = {
...userRecord,
userId: req.params.userId,
SK: undefined,
PK: undefined,
accessToken: undefined,
refreshToken: undefined,
deleteAtUnixTime: new Date(
userRecord.deleteAtUnixTime * 1000
).toISOString(),
}

const devices = (await getDevicesOfUser(req.params.userId)).reduce(
(acc, device) => {
const key = device.thingId
if (!acc[key]) {
acc[key] = []
}
acc[key].push({
...device,
SK: undefined,
PK: undefined,
thingId: undefined,
})
return acc
},
{}
)

res.send({
account,
devices,
})
} catch (err) {
console.log(err)
res.status(500).send(err.message)
}
})

app.post('/user/:userId/restartThings', async function (req, res) {
try {
const thingIds = await getThingsOfUser(req.params.userId)
Expand Down

0 comments on commit 3f2f85b

Please sign in to comment.