126 lines
6.5 KiB
TypeScript
126 lines
6.5 KiB
TypeScript
import { Resolver, Query, Args, Ctx, Authorized } from 'type-graphql';
|
|
import { Mutation, Arg } from 'type-graphql';
|
|
|
|
import { ApolloContext } from '@seed/interfaces/context';
|
|
import { getOneGeneric, editOneGeneric, getManyGenericWithArgs } from '@seed/graphql/BaseService';
|
|
|
|
import { deleteOneGeneric, signinGenericNoPassword } from './services/AccountService';
|
|
|
|
import AccountModel, { EditAccountInput, AccountArgs } from '@src/accounts/account.model';
|
|
import { AccountTypeEnum } from '@src/accounts/account.components';
|
|
import { NewAccountInputAdmin } from './account.model';
|
|
import { AvailableTranslation } from '@src/__components/components';
|
|
import { FirebaseTokenResult, SimpleResult } from '@seed/interfaces/components';
|
|
import { LoginInput, ResetPasswordInput } from './account.components';
|
|
import Firebase from '@seed/services/auth/FirebaseService';
|
|
import AccountBaseResolver from './account.resolver';
|
|
import { sendEmail } from '@seed/services/email/EmailService';
|
|
import FirebaseAuth from '@seed/services/auth/FirebaseAuthService';
|
|
import { ApolloError } from 'apollo-server-lambda';
|
|
import { newError } from '@seed/helpers/Error';
|
|
|
|
@Resolver(AccountModel)
|
|
export default class AccountAdminBaseResolver {
|
|
/*
|
|
███████╗██╗███████╗██╗ ██████╗ ███████╗
|
|
██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝
|
|
█████╗ ██║█████╗ ██║ ██║ ██║███████╗
|
|
██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║
|
|
██║ ██║███████╗███████╗██████╔╝███████║
|
|
╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝
|
|
*/
|
|
|
|
/*
|
|
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
|
|
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
|
|
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
|
|
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
|
|
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
|
|
*/
|
|
@Query(() => AccountModel)
|
|
@Authorized()
|
|
async me(@Ctx() ctx: ApolloContext): Promise<AccountModel> {
|
|
return ctx.ctx.user as AccountModel;
|
|
}
|
|
|
|
/*
|
|
* Auth side
|
|
*/
|
|
@Query(() => FirebaseTokenResult)
|
|
async login(
|
|
@Arg('creds', { nullable: true }) creds?: LoginInput,
|
|
@Arg('refreshToken', { nullable: true }) refreshToken?: string,
|
|
): Promise<FirebaseTokenResult> {
|
|
if (creds) return FirebaseAuth.getInstance().loginWithEmailPassword(creds);
|
|
if (refreshToken) return FirebaseAuth.getInstance().loginWithRefreshToken(refreshToken);
|
|
|
|
throw newError(3000);
|
|
}
|
|
|
|
@Query(() => AccountModel)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async accountsGetOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<AccountModel> {
|
|
const model = new AccountModel();
|
|
return getOneGeneric(model, id, ctx);
|
|
}
|
|
|
|
@Query(() => [AccountModel])
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async accountsGetMany(@Args() accountArgs: AccountArgs, @Ctx() ctx: ApolloContext): Promise<AccountModel[]> {
|
|
const model = new AccountModel();
|
|
return getManyGenericWithArgs(model, accountArgs, ctx);
|
|
}
|
|
|
|
/*
|
|
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
|
|
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
|
|
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
|
|
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
|
|
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
|
|
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
|
|
*/
|
|
|
|
/*
|
|
* Admin side
|
|
*/
|
|
|
|
@Mutation(() => AccountModel)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async accountsAddOne(@Arg('input') input: NewAccountInputAdmin, @Ctx() ctx: ApolloContext): Promise<AccountModel> {
|
|
const model = new AccountModel();
|
|
const result = await signinGenericNoPassword(model, input.type, input, ctx);
|
|
const resetPasswordLink = await Firebase.getInstance().getResetPasswordLink(result.email);
|
|
|
|
await sendEmail('adminAccountAddOne', result.language || AvailableTranslation.en, result.email, { ...result, resetPasswordLink });
|
|
|
|
return result as any;
|
|
}
|
|
|
|
@Mutation(() => AccountModel)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async accountsEditOne(@Arg('id') id: string, @Arg('input') input: EditAccountInput, @Ctx() ctx: ApolloContext): Promise<AccountModel> {
|
|
const model = new AccountModel();
|
|
return editOneGeneric(model, id, input, ctx);
|
|
}
|
|
|
|
@Mutation(() => AccountModel)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async accountsDeleteOne(
|
|
@Arg('id') id: string,
|
|
@Arg('userType', () => AccountTypeEnum) userType: AccountTypeEnum,
|
|
@Ctx() ctx: ApolloContext,
|
|
): Promise<AccountModel> {
|
|
const model = new AccountModel();
|
|
return deleteOneGeneric(model, userType, id, ctx);
|
|
}
|
|
|
|
/*
|
|
* ACTIONS
|
|
*/
|
|
@Mutation(() => SimpleResult)
|
|
async resetPassword(@Arg('input') input: ResetPasswordInput, @Ctx() ctx: ApolloContext): Promise<SimpleResult> {
|
|
return new AccountBaseResolver().resetPassword(input, ctx);
|
|
}
|
|
}
|