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:
@@ -9,6 +9,7 @@ import Integration, {
|
||||
} from "@server/models/Integration";
|
||||
import { authorize } from "@server/policies";
|
||||
import { presentIntegration } from "@server/presenters";
|
||||
import { APIContext } from "@server/types";
|
||||
import {
|
||||
assertSort,
|
||||
assertUuid,
|
||||
@@ -20,116 +21,133 @@ import pagination from "./middlewares/pagination";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post("integrations.list", auth(), pagination(), async (ctx) => {
|
||||
let { direction } = ctx.request.body;
|
||||
const { user } = ctx.state;
|
||||
const { type, sort = "updatedAt" } = ctx.request.body;
|
||||
if (direction !== "ASC") {
|
||||
direction = "DESC";
|
||||
}
|
||||
assertSort(sort, Integration);
|
||||
router.post(
|
||||
"integrations.list",
|
||||
auth(),
|
||||
pagination(),
|
||||
async (ctx: APIContext) => {
|
||||
let { direction } = ctx.request.body;
|
||||
const { user } = ctx.state.auth;
|
||||
const { type, sort = "updatedAt" } = ctx.request.body;
|
||||
if (direction !== "ASC") {
|
||||
direction = "DESC";
|
||||
}
|
||||
assertSort(sort, Integration);
|
||||
|
||||
let where: WhereOptions<Integration> = {
|
||||
teamId: user.teamId,
|
||||
};
|
||||
let where: WhereOptions<Integration> = {
|
||||
teamId: user.teamId,
|
||||
};
|
||||
|
||||
if (type) {
|
||||
assertIn(type, Object.values(IntegrationType));
|
||||
where = {
|
||||
...where,
|
||||
type,
|
||||
if (type) {
|
||||
assertIn(type, Object.values(IntegrationType));
|
||||
where = {
|
||||
...where,
|
||||
type,
|
||||
};
|
||||
}
|
||||
|
||||
const integrations = await Integration.findAll({
|
||||
where,
|
||||
order: [[sort, direction]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data: integrations.map(presentIntegration),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
const integrations = await Integration.findAll({
|
||||
where,
|
||||
order: [[sort, direction]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
router.post(
|
||||
"integrations.create",
|
||||
auth({ admin: true }),
|
||||
async (ctx: APIContext) => {
|
||||
const { type, service, settings } = ctx.request.body;
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data: integrations.map(presentIntegration),
|
||||
};
|
||||
});
|
||||
assertIn(type, Object.values(IntegrationType));
|
||||
|
||||
router.post("integrations.create", auth({ admin: true }), async (ctx) => {
|
||||
const { type, service, settings } = ctx.request.body;
|
||||
const { user } = ctx.state.auth;
|
||||
authorize(user, "createIntegration", user.team);
|
||||
|
||||
assertIn(type, Object.values(IntegrationType));
|
||||
assertIn(service, Object.values(UserCreatableIntegrationService));
|
||||
|
||||
const { user } = ctx.state;
|
||||
authorize(user, "createIntegration", user.team);
|
||||
if (has(settings, "url")) {
|
||||
assertUrl(settings.url);
|
||||
}
|
||||
|
||||
assertIn(service, Object.values(UserCreatableIntegrationService));
|
||||
const integration = await Integration.create({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
service,
|
||||
settings,
|
||||
type,
|
||||
});
|
||||
|
||||
if (has(settings, "url")) {
|
||||
assertUrl(settings.url);
|
||||
ctx.body = {
|
||||
data: presentIntegration(integration),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
const integration = await Integration.create({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
service,
|
||||
settings,
|
||||
type,
|
||||
});
|
||||
router.post(
|
||||
"integrations.update",
|
||||
auth({ admin: true }),
|
||||
async (ctx: APIContext) => {
|
||||
const { id, events = [], settings } = ctx.request.body;
|
||||
assertUuid(id, "id is required");
|
||||
|
||||
ctx.body = {
|
||||
data: presentIntegration(integration),
|
||||
};
|
||||
});
|
||||
const { user } = ctx.state.auth;
|
||||
const integration = await Integration.findByPk(id);
|
||||
authorize(user, "update", integration);
|
||||
|
||||
router.post("integrations.update", auth({ admin: true }), async (ctx) => {
|
||||
const { id, events = [], settings } = ctx.request.body;
|
||||
assertUuid(id, "id is required");
|
||||
assertArray(events, "events must be an array");
|
||||
|
||||
const { user } = ctx.state;
|
||||
const integration = await Integration.findByPk(id);
|
||||
authorize(user, "update", integration);
|
||||
if (has(settings, "url")) {
|
||||
assertUrl(settings.url);
|
||||
}
|
||||
|
||||
assertArray(events, "events must be an array");
|
||||
if (integration.type === IntegrationType.Post) {
|
||||
integration.events = events.filter((event: string) =>
|
||||
["documents.update", "documents.publish"].includes(event)
|
||||
);
|
||||
}
|
||||
|
||||
if (has(settings, "url")) {
|
||||
assertUrl(settings.url);
|
||||
integration.settings = settings;
|
||||
|
||||
await integration.save();
|
||||
|
||||
ctx.body = {
|
||||
data: presentIntegration(integration),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
if (integration.type === IntegrationType.Post) {
|
||||
integration.events = events.filter((event: string) =>
|
||||
["documents.update", "documents.publish"].includes(event)
|
||||
);
|
||||
router.post(
|
||||
"integrations.delete",
|
||||
auth({ admin: true }),
|
||||
async (ctx: APIContext) => {
|
||||
const { id } = ctx.request.body;
|
||||
assertUuid(id, "id is required");
|
||||
|
||||
const { user } = ctx.state.auth;
|
||||
const integration = await Integration.findByPk(id);
|
||||
authorize(user, "delete", integration);
|
||||
|
||||
await integration.destroy();
|
||||
await Event.create({
|
||||
name: "integrations.delete",
|
||||
modelId: integration.id,
|
||||
teamId: integration.teamId,
|
||||
actorId: user.id,
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
integration.settings = settings;
|
||||
|
||||
await integration.save();
|
||||
|
||||
ctx.body = {
|
||||
data: presentIntegration(integration),
|
||||
};
|
||||
});
|
||||
|
||||
router.post("integrations.delete", auth({ admin: true }), async (ctx) => {
|
||||
const { id } = ctx.request.body;
|
||||
assertUuid(id, "id is required");
|
||||
|
||||
const { user } = ctx.state;
|
||||
const integration = await Integration.findByPk(id);
|
||||
authorize(user, "delete", integration);
|
||||
|
||||
await integration.destroy();
|
||||
await Event.create({
|
||||
name: "integrations.delete",
|
||||
modelId: integration.id,
|
||||
teamId: integration.teamId,
|
||||
actorId: user.id,
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
};
|
||||
});
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user