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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user