chore: Typescript database models (#2886)

closes #2798
This commit is contained in:
Tom Moor
2022-01-06 18:24:28 -08:00
committed by GitHub
parent d3cbf250e6
commit b20a341f0c
207 changed files with 5624 additions and 5315 deletions

View File

@@ -1,19 +1,19 @@
import invariant from "invariant";
import Router from "koa-router";
import { Op } from "sequelize";
import { MAX_AVATAR_DISPLAY } from "@shared/constants";
import auth from "@server/middlewares/authentication";
import { User, Event, Group, GroupUser } from "@server/models";
import policy from "@server/policies";
import { authorize } from "@server/policies";
import {
presentGroup,
presentPolicies,
presentUser,
presentGroupMembership,
} from "@server/presenters";
import { Op } from "@server/sequelize";
import { assertPresent, assertUuid, assertSort } from "@server/validation";
import pagination from "./middlewares/pagination";
const { authorize } = policy;
const router = new Router();
router.post("groups.list", auth(), pagination(), async (ctx) => {
@@ -22,7 +22,7 @@ router.post("groups.list", auth(), pagination(), async (ctx) => {
if (direction !== "ASC") direction = "DESC";
assertSort(sort, Group);
const user = ctx.state.user;
const { user } = ctx.state;
const groups = await Group.findAll({
where: {
teamId: user.teamId,
@@ -37,10 +37,8 @@ router.post("groups.list", auth(), pagination(), async (ctx) => {
data: {
groups: groups.map(presentGroup),
groupMemberships: groups
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'g' implicitly has an 'any' type.
.map((g) =>
g.groupMemberships
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'membership' implicitly has an 'any' typ... Remove this comment to see the full error message
.filter((membership) => !!membership.user)
.slice(0, MAX_AVATAR_DISPLAY)
)
@@ -55,9 +53,10 @@ router.post("groups.info", auth(), async (ctx) => {
const { id } = ctx.body;
assertUuid(id, "id is required");
const user = ctx.state.user;
const { user } = ctx.state;
const group = await Group.findByPk(id);
authorize(user, "read", group);
ctx.body = {
data: presentGroup(group),
policies: presentPolicies(user, [group]),
@@ -68,15 +67,18 @@ router.post("groups.create", auth(), async (ctx) => {
const { name } = ctx.body;
assertPresent(name, "name is required");
const user = ctx.state.user;
const { user } = ctx.state;
authorize(user, "createGroup", user.team);
let group = await Group.create({
const g = await Group.create({
name,
teamId: user.teamId,
createdById: user.id,
});
// reload to get default scope
group = await Group.findByPk(group.id);
const group = await Group.findByPk(g.id);
invariant(group, "group not found");
await Event.create({
name: "groups.create",
actorId: user.id,
@@ -87,6 +89,7 @@ router.post("groups.create", auth(), async (ctx) => {
},
ip: ctx.request.ip,
});
ctx.body = {
data: presentGroup(group),
policies: presentPolicies(user, [group]),
@@ -98,9 +101,10 @@ router.post("groups.update", auth(), async (ctx) => {
assertPresent(name, "name is required");
assertUuid(id, "id is required");
const user = ctx.state.user;
const { user } = ctx.state;
const group = await Group.findByPk(id);
authorize(user, "update", group);
group.name = name;
if (group.changed()) {
@@ -130,6 +134,7 @@ router.post("groups.delete", auth(), async (ctx) => {
const { user } = ctx.state;
const group = await Group.findByPk(id);
authorize(user, "delete", group);
await group.destroy();
await Event.create({
name: "groups.delete",
@@ -141,6 +146,7 @@ router.post("groups.delete", auth(), async (ctx) => {
},
ip: ctx.request.ip,
});
ctx.body = {
success: true,
};
@@ -150,7 +156,7 @@ router.post("groups.memberships", auth(), pagination(), async (ctx) => {
const { id, query } = ctx.body;
assertUuid(id, "id is required");
const user = ctx.state.user;
const { user } = ctx.state;
const group = await Group.findByPk(id);
authorize(user, "read", group);
let userWhere;
@@ -179,11 +185,11 @@ router.post("groups.memberships", auth(), pagination(), async (ctx) => {
},
],
});
ctx.body = {
pagination: ctx.state.pagination,
data: {
groupMemberships: memberships.map(presentGroupMembership),
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'membership' implicitly has an 'any' typ... Remove this comment to see the full error message
users: memberships.map((membership) => presentUser(membership.user)),
},
};
@@ -196,8 +202,10 @@ router.post("groups.add_user", auth(), async (ctx) => {
const user = await User.findByPk(userId);
authorize(ctx.state.user, "read", user);
let group = await Group.findByPk(id);
authorize(ctx.state.user, "update", group);
let membership = await GroupUser.findOne({
where: {
groupId: id,
@@ -206,7 +214,7 @@ router.post("groups.add_user", auth(), async (ctx) => {
});
if (!membership) {
await group.addUser(user, {
await group.$add("user", user, {
through: {
createdById: ctx.state.user.id,
},
@@ -218,8 +226,12 @@ router.post("groups.add_user", auth(), async (ctx) => {
userId,
},
});
invariant(membership, "membership not found");
// reload to get default scope
group = await Group.findByPk(id);
invariant(group, "group not found");
await Event.create({
name: "groups.add_user",
userId,
@@ -249,9 +261,11 @@ router.post("groups.remove_user", auth(), async (ctx) => {
let group = await Group.findByPk(id);
authorize(ctx.state.user, "update", group);
const user = await User.findByPk(userId);
authorize(ctx.state.user, "read", user);
await group.removeUser(user);
await group.$remove("user", user);
await Event.create({
name: "groups.remove_user",
userId,
@@ -263,8 +277,11 @@ router.post("groups.remove_user", auth(), async (ctx) => {
},
ip: ctx.request.ip,
});
// reload to get default scope
group = await Group.findByPk(id);
invariant(group, "group not found");
ctx.body = {
data: {
groups: [presentGroup(group)],