-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE][SSO] Migrate to Knex
DELETE dashboard/users
endpoints (#1490)
- Loading branch information
1 parent
ec6ec7c
commit a8e042b
Showing
6 changed files
with
47 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 16 additions & 19 deletions
35
services/sso/src/controllers/dashboard/users/batchDelete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,32 @@ | ||
import { Context, Middleware } from 'koa' | ||
import { NotFoundError } from '../../../utils/errors' | ||
import { client } from '../../../db/client' | ||
import db from '../../../db/knexClient' | ||
|
||
export const dashboardBatchDelete: Middleware = async (ctx: Context) => { | ||
const { ids } = ctx.request.body | ||
if (ids.length === 0) { | ||
if (!Array.isArray(ids) || ids.length === 0) { | ||
throw new NotFoundError('No user found') | ||
} | ||
const verifyQuery = ` | ||
SELECT id, deleted_at FROM "user" WHERE id = ANY($1::text[]); | ||
` | ||
const verifyResult = await client.query(verifyQuery, [ids]) | ||
const existingIds = verifyResult.rows.map((row) => row.id) | ||
|
||
const verifyResult = await db('user') | ||
.select('id', 'deleted_at') | ||
.whereIn('id', ids) | ||
|
||
const existingIds = verifyResult.map((row) => row.id) | ||
|
||
if (existingIds.length === 0) { | ||
throw new NotFoundError('No user found') | ||
} | ||
|
||
const toBeDeletedUser: string[] = [] | ||
verifyResult.rows.forEach((row) => { | ||
if (row.deleted_at === null) { | ||
toBeDeletedUser.push(row.id) | ||
} | ||
}) | ||
const toBeDeletedUser: string[] = verifyResult | ||
.filter((row) => row.deleted_at === null) | ||
.map((row) => row.id) | ||
|
||
if (toBeDeletedUser.length > 0) { | ||
const deleteQuery = ` | ||
UPDATE "user" | ||
SET deleted_at = CURRENT_TIMESTAMP | ||
WHERE id = ANY($1::text[]) | ||
` | ||
await client.query(deleteQuery, [toBeDeletedUser]) | ||
await db('user') | ||
.whereIn('id', toBeDeletedUser) | ||
.update({ deleted_at: db.fn.now() }) | ||
} | ||
|
||
ctx.status = 204 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters