puyoskey/packages/backend/src/server/api/endpoints/users/groups/joined.ts
Johann150 78df3dc484
enhance: improve documentation for /users/ endpoints (#8790)
* docs: category & description for reset password

* docs: category & description for testing

* docs: descriptions for groups endpoints

* docs: descriptions for drive file endpoints

* docs: descriptions for sw endpoints

* docs: descriptions for user list endpoints

* docs: descriptions & result type for gallery posts

* docs: descriptions & result type for user endpoints

* docs: add return type for stats
2022-06-10 14:25:20 +09:00

45 lines
1,023 B
TypeScript

import define from '../../../define.js';
import { UserGroups, UserGroupJoinings } from '@/models/index.js';
import { Not, In } from 'typeorm';
export const meta = {
tags: ['groups', 'account'],
requireCredential: true,
kind: 'read:user-groups',
description: 'List the groups that the authenticated user is a member of.',
res: {
type: 'array',
optional: false, nullable: false,
items: {
type: 'object',
optional: false, nullable: false,
ref: 'UserGroup',
},
},
} as const;
export const paramDef = {
type: 'object',
properties: {},
required: [],
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const ownedGroups = await UserGroups.findBy({
userId: me.id,
});
const joinings = await UserGroupJoinings.findBy({
userId: me.id,
...(ownedGroups.length > 0 ? {
userGroupId: Not(In(ownedGroups.map(x => x.id))),
} : {}),
});
return await Promise.all(joinings.map(x => UserGroups.pack(x.userGroupId)));
});