Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
heyvard committed May 25, 2024
1 parent e78cdce commit 2d5b552
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
32 changes: 32 additions & 0 deletions api/v1/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ApiHandlerOpts } from '../../types/apiHandlerOpts'
import { allowCors } from '../../cors/corsHelper'
import { auth } from '../../auth/authHandler'

const handler = async function handler(opts: ApiHandlerOpts): Promise<void> {
const { user, res, client } = opts

if (!user) {
res.status(401)
return
}

if (!user.admin) {
res.status(403)
return
}

const users = (
await client.query(
`
SELECT u.id,
u.email,
u.display_name,
u.paid,
u.admin,
u.active
FROM users u`,
)
).rows
res.status(200).json(users)
}
export default allowCors(auth(handler))
21 changes: 21 additions & 0 deletions api/v1/users/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ const handler = async function handler(opts: ApiHandlerOpts): Promise<void> {
)
}

if (typeof reqBody.admin !== 'undefined') {
await client.query(
`
UPDATE users
SET admin = $1
WHERE id = $2;
`,
[reqBody.admin, id],
)
}

if (typeof reqBody.active !== 'undefined') {
await client.query(
`
UPDATE users
SET active = $1
WHERE id = $2;
`,
[reqBody.active, id],
)
}
res.status(200).json(reqBody)
}
export default allowCors(auth(handler))

0 comments on commit 2d5b552

Please sign in to comment.