Refactor to accommodate authentication, transaction and pagination states together (#4636)

* fix: refactor to accommodate authentication, transaction and pagination together into ctx.state

* feat: allow passing response type to APIContext
This commit is contained in:
Apoorv Mishra
2023-01-04 23:51:44 +05:30
committed by GitHub
parent bb568d2e62
commit f4461573de
31 changed files with 753 additions and 675 deletions

View File

@@ -4,16 +4,16 @@ import auth from "@server/middlewares/authentication";
import { Team, NotificationSetting } from "@server/models";
import { authorize } from "@server/policies";
import { presentNotificationSetting } from "@server/presenters";
import { ContextWithState } from "@server/types";
import { APIContext } from "@server/types";
import { assertPresent, assertUuid } from "@server/validation";
const router = new Router();
router.post("notificationSettings.create", auth(), async (ctx) => {
router.post("notificationSettings.create", auth(), async (ctx: APIContext) => {
const { event } = ctx.request.body;
assertPresent(event, "event is required");
const { user } = ctx.state;
const { user } = ctx.state.auth;
authorize(user, "createNotificationSetting", user.team);
const [setting] = await NotificationSetting.findOrCreate({
where: {
@@ -28,8 +28,8 @@ router.post("notificationSettings.create", auth(), async (ctx) => {
};
});
router.post("notificationSettings.list", auth(), async (ctx) => {
const { user } = ctx.state;
router.post("notificationSettings.list", auth(), async (ctx: APIContext) => {
const { user } = ctx.state.auth;
const settings = await NotificationSetting.findAll({
where: {
userId: user.id,
@@ -41,11 +41,11 @@ router.post("notificationSettings.list", auth(), async (ctx) => {
};
});
router.post("notificationSettings.delete", auth(), async (ctx) => {
router.post("notificationSettings.delete", auth(), async (ctx: APIContext) => {
const { id } = ctx.request.body;
assertUuid(id, "id is required");
const { user } = ctx.state;
const { user } = ctx.state.auth;
const setting = await NotificationSetting.findByPk(id);
authorize(user, "delete", setting);
@@ -56,7 +56,7 @@ router.post("notificationSettings.delete", auth(), async (ctx) => {
};
});
const handleUnsubscribe = async (ctx: ContextWithState) => {
const handleUnsubscribe = async (ctx: APIContext) => {
const { id, token } = (ctx.method === "POST"
? ctx.request.body
: ctx.request.query) as {