helper function to get user from context

This commit is contained in:
Moriz Wahl 2022-04-11 16:15:57 +02:00
parent b0606424d4
commit 6cc8410720
4 changed files with 16 additions and 19 deletions

View File

@ -1,4 +1,4 @@
import { Context } from '@/server/context'
import { Context, getUser } from '@/server/context'
import { Resolver, Query, Args, Ctx, Authorized, Arg } from 'type-graphql'
import CONFIG from '@/config'
import { GdtEntryList } from '@model/GdtEntryList'
@ -16,8 +16,7 @@ export class GdtResolver {
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
@Ctx() context: Context,
): Promise<GdtEntryList> {
const userEntity = context.user
if (!userEntity) throw new Error('No user given!')
const userEntity = getUser(context)
try {
const resultGDT = await apiGet(
@ -35,8 +34,7 @@ export class GdtResolver {
@Authorized([RIGHTS.GDT_BALANCE])
@Query(() => Number)
async gdtBalance(@Ctx() context: Context): Promise<number | null> {
const { user } = context
if (!user) throw new Error('No user given!')
const user = getUser(context)
try {
const resultGDTSum = await apiPost(`${CONFIG.GDT_API_URL}/GdtEntries/sumPerEmailApi`, {
email: user.email,

View File

@ -1,4 +1,4 @@
import { Context } from '@/server/context'
import { Context, getUser } from '@/server/context'
import { Resolver, Args, Arg, Authorized, Ctx, Mutation, Query, Int } from 'type-graphql'
import { TransactionLink } from '@model/TransactionLink'
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
@ -38,8 +38,7 @@ export class TransactionLinkResolver {
@Args() { amount, memo }: TransactionLinkArgs,
@Ctx() context: Context,
): Promise<TransactionLink> {
const { user } = context
if (!user) throw new Error('No user given!')
const user = getUser(context)
const createdDate = new Date()
const validUntil = transactionLinkExpireDate(createdDate)
@ -73,8 +72,7 @@ export class TransactionLinkResolver {
@Arg('id', () => Int) id: number,
@Ctx() context: Context,
): Promise<boolean> {
const { user } = context
if (!user) throw new Error('No user given!')
const user = getUser(context)
const transactionLink = await dbTransactionLink.findOne({ id })
if (!transactionLink) {
@ -115,8 +113,7 @@ export class TransactionLinkResolver {
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
@Ctx() context: Context,
): Promise<TransactionLink[]> {
const { user } = context
if (!user) throw new Error('No user given!')
const user = getUser(context)
// const now = new Date()
const transactionLinks = await dbTransactionLink.find({
where: {
@ -139,8 +136,7 @@ export class TransactionLinkResolver {
@Arg('code', () => String) code: string,
@Ctx() context: Context,
): Promise<boolean> {
const { user } = context
if (!user) throw new Error('No user given!')
const user = getUser(context)
const transactionLink = await dbTransactionLink.findOneOrFail({ code })
const linkedUser = await dbUser.findOneOrFail({ id: transactionLink.userId })

View File

@ -1,5 +1,5 @@
import fs from 'fs'
import { Context } from '@/server/context'
import { Context, getUser } from '@/server/context'
import { Resolver, Query, Args, Arg, Authorized, Ctx, UseMiddleware, Mutation } from 'type-graphql'
import { getConnection, getCustomRepository } from '@dbTools/typeorm'
import CONFIG from '@/config'
@ -192,8 +192,7 @@ export class UserResolver {
@UseMiddleware(klicktippNewsletterStateMiddleware)
async verifyLogin(@Ctx() context: Context): Promise<User> {
// TODO refactor and do not have duplicate code with login(see below)
const userEntity = context.user
if (!userEntity) throw new Error('No user given!')
const userEntity = getUser(context)
const user = new User(userEntity)
// user.pubkey = userEntity.pubKey.toString('hex')
// Elopage Status & Stored PublisherId
@ -541,8 +540,7 @@ export class UserResolver {
}: UpdateUserInfosArgs,
@Ctx() context: Context,
): Promise<boolean> {
const userEntity = context.user
if (!userEntity) throw new Error('No user given!')
const userEntity = getUser(context)
if (firstName) {
userEntity.firstName = firstName

View File

@ -23,4 +23,9 @@ const context = (args: any): Context => {
return context
}
export const getUser = (context: Context): dbUser => {
if (context.user) return context.user
throw new Error('No user given in context!')
}
export default context