Dwayne Harris
5 years ago
18 changed files with 460 additions and 217 deletions
-
14package-lock.json
-
1package.json
-
4src/awards.ts
-
4src/lib/authentication.ts
-
62src/lib/collections.ts
-
10src/lib/crypto.ts
-
29src/lib/database.ts
-
53src/lib/errors.ts
-
55src/plugins/api/authentication.ts
-
89src/plugins/api/groups.ts
-
20src/plugins/api/index.ts
-
152src/plugins/api/posts.ts
-
72src/plugins/api/users.ts
-
1src/schemas.ts
-
5src/server.ts
-
80src/types/collections.ts
-
16src/types/index.ts
-
10tsconfig.json
@ -1,37 +1,53 @@ |
|||
import { CosmosClient } from '@azure/cosmos' |
|||
import { Logger } from 'fastify' |
|||
import uniq from 'lodash/uniq' |
|||
import { IDatabaseItem } from '../types' |
|||
import { DatabaseItem } from '../types' |
|||
|
|||
import { containerFor, createQuerySpec, queryItems } from './database' |
|||
import { IUser, IUserSubscription, IUserBlock } from '../types/collections' |
|||
import { User, UserSubscription, UserBlock } from '../types/collections' |
|||
|
|||
export async function getUsers(client: CosmosClient, ids: string[], logger?: Logger): Promise<IUser[]> { |
|||
return await queryItems<IUser>(containerFor(client, 'Users'), createQuerySpec( |
|||
'SELECT u.id, u.name, u.imageUrl, u.coverImageUrl, u.group, u.created FROM Users u WHERE ARRAY_CONTAINS(@ids, u.id)', { |
|||
ids: uniq(ids), |
|||
}), logger) |
|||
export async function getUsers(client: CosmosClient, ids: string[], logger?: Logger): Promise<User[]> { |
|||
return await queryItems<User>({ |
|||
container: containerFor(client, 'Users'), |
|||
query: createQuerySpec( |
|||
'SELECT u.id, u.name, u.imageUrl, u.coverImageUrl, u.group, u.created FROM Users u WHERE ARRAY_CONTAINS(@ids, u.id)', |
|||
{ |
|||
ids: uniq(ids), |
|||
} |
|||
), |
|||
logger, |
|||
}) |
|||
} |
|||
|
|||
export async function getUsersFromItems<T extends IDatabaseItem>(client: CosmosClient, items: T[]): Promise<IUser[]> { |
|||
export async function getUsersFromItems<T extends DatabaseItem>(client: CosmosClient, items: T[], logger?: Logger): Promise<User[]> { |
|||
return await getUsers(client, items.map(i => i.id)) |
|||
} |
|||
|
|||
export async function getApprovedSubscriptions(client: CosmosClient, from: string, to: string, logger?: Logger): Promise<IUserSubscription[]> { |
|||
return await queryItems<IUserSubscription>(containerFor(client, 'Users'), createQuerySpec(`
|
|||
SELECT u.id FROM Users u WHERE |
|||
u.subscriberId = @to |
|||
u.pk = @from AND |
|||
u.t = 'subscription' AND |
|||
u.pending = false |
|||
`, { from, to }), logger)
|
|||
export async function getApprovedSubscriptions(client: CosmosClient, from: string, to: string, logger?: Logger): Promise<UserSubscription[]> { |
|||
return await queryItems<UserSubscription>({ |
|||
container: containerFor(client, 'Users'), |
|||
query: createQuerySpec( |
|||
`SELECT u.id FROM Users u WHERE
|
|||
u.subscriberId = @to |
|||
u.pk = @from AND |
|||
u.t = 'subscription' AND |
|||
u.pending = false`,
|
|||
{ from, to } |
|||
), |
|||
logger, |
|||
}) |
|||
} |
|||
|
|||
export async function getUserBlocks(client: CosmosClient, from: string, to: string[], logger?: Logger): Promise<IUserBlock[]> { |
|||
return await queryItems<IUserBlock>(containerFor(client, 'Users'), createQuerySpec(`
|
|||
SELECT u.id FROM Users u WHERE |
|||
u.pk = @from |
|||
AND u.t = 'block' |
|||
AND ARRAY_CONTAINS(@to, u.blockedId) |
|||
`, { from, to }), logger)
|
|||
export async function getUserBlocks(client: CosmosClient, from: string, to: string[], logger?: Logger): Promise<UserBlock[]> { |
|||
return await queryItems<UserBlock>({ |
|||
container: containerFor(client, 'Users'), |
|||
query: createQuerySpec( |
|||
`SELECT u.id FROM Users u WHERE
|
|||
u.pk = @from |
|||
AND u.t = 'block' |
|||
AND ARRAY_CONTAINS(@to, u.blockedId)`,
|
|||
{ from, to } |
|||
), |
|||
logger, |
|||
}) |
|||
} |
@ -1,49 +1,50 @@ |
|||
import { FastifyReply, JSONSchema } from 'fastify' |
|||
import { FastifyReply } from 'fastify' |
|||
import { ServerResponse } from 'http' |
|||
import { HttpError, HttpFormError } from '../types' |
|||
|
|||
interface IHttpError { |
|||
statusCode: number |
|||
error: string |
|||
field?: string |
|||
export function badRequestError(reply: FastifyReply<ServerResponse>, message: string = 'Bad Request', errors?: HttpFormError[]): HttpError { |
|||
reply.code(400) |
|||
|
|||
return { |
|||
message, |
|||
errors, |
|||
} |
|||
} |
|||
|
|||
export function badRequestError(reply: FastifyReply<ServerResponse>, error: string = 'Bad Request', field?: string): IHttpError { |
|||
const statusCode = 400 |
|||
reply.code(statusCode) |
|||
export function badRequestFormError(reply: FastifyReply<ServerResponse>, field: string, message: string): HttpError { |
|||
reply.code(400) |
|||
|
|||
return { |
|||
statusCode, |
|||
error, |
|||
field, |
|||
message: 'Bad Request', |
|||
errors: [ |
|||
{ |
|||
field, |
|||
message, |
|||
} |
|||
] |
|||
} |
|||
} |
|||
|
|||
export function unauthorizedError(reply: FastifyReply<ServerResponse>): IHttpError { |
|||
const statusCode = 401 |
|||
reply.code(statusCode) |
|||
export function unauthorizedError(reply: FastifyReply<ServerResponse>): HttpError { |
|||
reply.code(401) |
|||
|
|||
return { |
|||
statusCode, |
|||
error: 'Unauthorized', |
|||
message: 'Unauthorized', |
|||
} |
|||
} |
|||
|
|||
export function notFoundError(reply: FastifyReply<ServerResponse>): IHttpError { |
|||
const statusCode = 404 |
|||
reply.code(statusCode) |
|||
export function notFoundError(reply: FastifyReply<ServerResponse>): HttpError { |
|||
reply.code(404) |
|||
|
|||
return { |
|||
statusCode, |
|||
error: 'Not Found' |
|||
message: 'Not Found', |
|||
} |
|||
} |
|||
|
|||
export function serverError(reply: FastifyReply<ServerResponse>, error: string = 'Server Error'): IHttpError { |
|||
const statusCode = 500 |
|||
reply.code(statusCode) |
|||
export function serverError(reply: FastifyReply<ServerResponse>, message: string = 'Server Error'): HttpError { |
|||
reply.code(500) |
|||
|
|||
return { |
|||
statusCode, |
|||
error, |
|||
message, |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue