Dwayne Harris
5 years ago
5 changed files with 377 additions and 36 deletions
-
37src/plugins/api/authentication.ts
-
104src/plugins/api/groups.ts
-
60src/plugins/api/posts.ts
-
124src/plugins/api/profile.ts
-
88src/types/collections.ts
@ -0,0 +1,104 @@ |
|||
import fastify, { |
|||
Plugin, |
|||
DefaultQuery, |
|||
DefaultParams, |
|||
RouteShorthandOptions, |
|||
DefaultHeaders, |
|||
} from 'fastify' |
|||
|
|||
import { Server, IncomingMessage, ServerResponse } from 'http' |
|||
|
|||
import { unauthorizedError, badRequestError } from '../../lib/errors' |
|||
import { containerFor, normalize } from '../../lib/database' |
|||
import { IUser, IGroup, IGroupMembership } from '../../types/collections' |
|||
|
|||
interface PluginOptions { |
|||
|
|||
} |
|||
|
|||
function createRoute(server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse>) { |
|||
interface Body { |
|||
name: string |
|||
about?: string |
|||
open: boolean |
|||
requiresApproval: boolean |
|||
} |
|||
|
|||
const options: RouteShorthandOptions = { |
|||
schema: { |
|||
body: { |
|||
type: 'object', |
|||
required: ['name', 'open', 'requiresApproval'], |
|||
properties: { |
|||
name: { type: 'string' }, |
|||
about: { type: 'string' }, |
|||
open: { type: 'boolean' }, |
|||
requiresApproval: { type: 'boolean' }, |
|||
}, |
|||
}, |
|||
response: { |
|||
200: { |
|||
type: 'object', |
|||
properties: { |
|||
id: { type: 'string' }, |
|||
}, |
|||
}, |
|||
}, |
|||
}, |
|||
} |
|||
|
|||
server.post<DefaultQuery, DefaultParams, DefaultHeaders, Body>('/api/group', options, async (request, reply) => { |
|||
if (!request.user) return unauthorizedError(reply) |
|||
|
|||
const userContainer = await containerFor(server.database.client, 'Users') |
|||
const userItem = userContainer.item(request.user.id, request.user.id) |
|||
const { resource: user } = await userItem.read<IUser>() |
|||
|
|||
const groupContainer = await containerFor(server.database.client, 'Groups') |
|||
|
|||
if (user.group !== '') return badRequestError(reply, 'Invalid operation') |
|||
const { name, about, open, requiresApproval } = request.body |
|||
const normalizedName = normalize(name) |
|||
|
|||
const group: IGroup = { |
|||
id: normalizedName, |
|||
partitionKey: normalizedName, |
|||
type: 'group', |
|||
userId: request.user.id, |
|||
name, |
|||
about, |
|||
open, |
|||
requiresApproval, |
|||
members: 1, |
|||
created: Date.now(), |
|||
} |
|||
|
|||
const membership: IGroupMembership = { |
|||
id: request.user.id, |
|||
partitionKey: normalizedName, |
|||
type: 'membership', |
|||
userId: request.user.id, |
|||
pending: false, |
|||
membership: 'admin', |
|||
created: Date.now(), |
|||
} |
|||
|
|||
await groupContainer.items.create(group) |
|||
await groupContainer.items.create(membership) |
|||
|
|||
await userItem.replace<IUser>({ |
|||
...user, |
|||
group: group.id, |
|||
}) |
|||
|
|||
return { |
|||
id: group.id, |
|||
} |
|||
}) |
|||
} |
|||
|
|||
const plugin: Plugin<Server, IncomingMessage, ServerResponse, PluginOptions> = async server => { |
|||
createRoute(server) |
|||
} |
|||
|
|||
export default plugin |
Write
Preview
Loading…
Cancel
Save
Reference in new issue