Files
Curse/packages/backend/src/server/api/endpoints/users/groups/create.ts
syuilo 1c67c26bd8 refactor: migrate to typeorm 3.0 (#8443)
* wip

* wip

* wip

* Update following.ts

* wip

* wip

* wip

* Update resolve-user.ts

* maxQueryExecutionTime

* wip

* wip
2022-03-26 15:34:00 +09:00

48 lines
1.1 KiB
TypeScript

import define from '../../../define.js';
import { UserGroups, UserGroupJoinings } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js';
import { UserGroup } from '@/models/entities/user-group.js';
import { UserGroupJoining } from '@/models/entities/user-group-joining.js';
export const meta = {
tags: ['groups'],
requireCredential: true,
kind: 'write:user-groups',
res: {
type: 'object',
optional: false, nullable: false,
ref: 'UserGroup',
},
} as const;
export const paramDef = {
type: 'object',
properties: {
name: { type: 'string', minLength: 1, maxLength: 100 },
},
required: ['name'],
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const userGroup = await UserGroups.insert({
id: genId(),
createdAt: new Date(),
userId: user.id,
name: ps.name,
} as UserGroup).then(x => UserGroups.findOneByOrFail(x.identifiers[0]));
// Push the owner
await UserGroupJoinings.insert({
id: genId(),
createdAt: new Date(),
userId: user.id,
userGroupId: userGroup.id,
} as UserGroupJoining);
return await UserGroups.pack(userGroup);
});