2022-04-18 09:30:57 -05:00
|
|
|
import { Applications } from '@logto/schemas';
|
2021-08-26 09:50:27 -05:00
|
|
|
import { object, string } from 'zod';
|
2021-08-29 22:30:54 -05:00
|
|
|
|
2021-08-14 08:39:37 -05:00
|
|
|
import koaGuard from '@/middleware/koa-guard';
|
2022-01-11 03:37:09 -05:00
|
|
|
import koaPagination from '@/middleware/koa-pagination';
|
2021-10-11 04:55:17 -05:00
|
|
|
import { buildOidcClientMetadata } from '@/oidc/utils';
|
2021-08-26 11:33:13 -05:00
|
|
|
import {
|
|
|
|
deleteApplicationById,
|
2021-09-01 05:23:53 -05:00
|
|
|
findApplicationById,
|
2021-09-01 07:51:34 -05:00
|
|
|
findAllApplications,
|
2021-08-26 11:33:13 -05:00
|
|
|
insertApplication,
|
|
|
|
updateApplicationById,
|
2022-01-11 03:37:09 -05:00
|
|
|
findTotalNumberOfApplications,
|
2021-08-26 11:33:13 -05:00
|
|
|
} from '@/queries/application';
|
2021-08-17 11:24:00 -05:00
|
|
|
import { buildIdGenerator } from '@/utils/id';
|
|
|
|
|
2021-09-01 04:35:23 -05:00
|
|
|
import { AuthedRouter } from './types';
|
|
|
|
|
2021-08-17 11:24:00 -05:00
|
|
|
const applicationId = buildIdGenerator(21);
|
2021-08-14 08:39:37 -05:00
|
|
|
|
2021-09-01 04:35:23 -05:00
|
|
|
export default function applicationRoutes<T extends AuthedRouter>(router: T) {
|
2022-01-11 03:37:09 -05:00
|
|
|
router.get('/applications', koaPagination(), async (ctx, next) => {
|
|
|
|
const { limit, offset } = ctx.pagination;
|
|
|
|
|
2022-04-18 09:30:57 -05:00
|
|
|
const [{ count }, applications] = await Promise.all([
|
2022-01-11 03:37:09 -05:00
|
|
|
findTotalNumberOfApplications(),
|
|
|
|
findAllApplications(limit, offset),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Return totalCount to pagination middleware
|
|
|
|
ctx.pagination.totalCount = count;
|
2022-04-18 09:30:57 -05:00
|
|
|
ctx.body = applications;
|
2022-01-11 03:37:09 -05:00
|
|
|
|
2021-09-01 07:51:34 -05:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2021-08-14 08:39:37 -05:00
|
|
|
router.post(
|
2021-11-17 22:26:51 -05:00
|
|
|
'/applications',
|
2021-08-14 08:39:37 -05:00
|
|
|
koaGuard({
|
2022-01-17 23:44:08 -05:00
|
|
|
body: Applications.createGuard
|
2021-08-26 09:50:27 -05:00
|
|
|
.omit({ id: true, createdAt: true })
|
|
|
|
.partial()
|
2022-01-17 23:44:08 -05:00
|
|
|
.merge(Applications.createGuard.pick({ name: true, type: true })),
|
2021-08-14 08:39:37 -05:00
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
2022-04-14 02:54:21 -05:00
|
|
|
const { oidcClientMetadata, ...rest } = ctx.guard.body;
|
2021-08-14 08:39:37 -05:00
|
|
|
|
2022-04-18 09:30:57 -05:00
|
|
|
ctx.body = await insertApplication({
|
|
|
|
id: applicationId(),
|
|
|
|
oidcClientMetadata: buildOidcClientMetadata(oidcClientMetadata),
|
|
|
|
...rest,
|
|
|
|
});
|
2022-01-27 06:26:34 -05:00
|
|
|
|
2021-08-14 08:39:37 -05:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
2021-08-23 11:11:25 -05:00
|
|
|
|
2021-09-01 05:23:53 -05:00
|
|
|
router.get(
|
2021-11-17 22:26:51 -05:00
|
|
|
'/applications/:id',
|
2021-09-01 05:23:53 -05:00
|
|
|
koaGuard({
|
|
|
|
params: object({ id: string().min(1) }),
|
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const {
|
|
|
|
params: { id },
|
|
|
|
} = ctx.guard;
|
|
|
|
|
2022-04-18 09:30:57 -05:00
|
|
|
ctx.body = await findApplicationById(id);
|
2022-01-27 06:26:34 -05:00
|
|
|
|
2021-09-01 05:23:53 -05:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-08-26 11:33:13 -05:00
|
|
|
router.patch(
|
2021-11-17 22:26:51 -05:00
|
|
|
'/applications/:id',
|
2021-08-26 11:33:13 -05:00
|
|
|
koaGuard({
|
|
|
|
params: object({ id: string().min(1) }),
|
2022-06-21 04:00:49 -05:00
|
|
|
body: Applications.createGuard.omit({ id: true, createdAt: true }).deepPartial(),
|
2021-08-26 11:33:13 -05:00
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const {
|
|
|
|
params: { id },
|
|
|
|
body,
|
|
|
|
} = ctx.guard;
|
|
|
|
|
2022-04-18 09:30:57 -05:00
|
|
|
ctx.body = await updateApplicationById(id, {
|
|
|
|
...body,
|
|
|
|
});
|
2022-01-27 06:26:34 -05:00
|
|
|
|
2021-08-26 11:33:13 -05:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-08-23 11:11:25 -05:00
|
|
|
router.delete(
|
2021-11-17 22:26:51 -05:00
|
|
|
'/applications/:id',
|
2021-08-23 11:11:25 -05:00
|
|
|
koaGuard({ params: object({ id: string().min(1) }) }),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const { id } = ctx.guard.params;
|
|
|
|
// Note: will need delete cascade when application is joint with other tables
|
2022-01-13 03:02:28 -05:00
|
|
|
await findApplicationById(id);
|
2021-08-23 11:11:25 -05:00
|
|
|
await deleteApplicationById(id);
|
|
|
|
ctx.status = 204;
|
2022-01-27 06:26:34 -05:00
|
|
|
|
2021-08-23 11:11:25 -05:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
2021-08-14 08:39:37 -05:00
|
|
|
}
|