mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
unit tests of AdminResolver now with Users and UserContacts
This commit is contained in:
parent
faa0500f10
commit
eedaf9e6e3
@ -1117,7 +1117,9 @@ describe('AdminResolver', () => {
|
|||||||
}),
|
}),
|
||||||
).resolves.toEqual(
|
).resolves.toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
errors: [new GraphQLError('Could not find user with email: bob@baumeister.de')],
|
errors: [
|
||||||
|
new GraphQLError('Could not find UserContact with email: bob@baumeister.de'),
|
||||||
|
],
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -75,24 +75,24 @@ export class AdminResolver {
|
|||||||
{ searchText, currentPage = 1, pageSize = 25, filters }: SearchUsersArgs,
|
{ searchText, currentPage = 1, pageSize = 25, filters }: SearchUsersArgs,
|
||||||
): Promise<SearchUsersResult> {
|
): Promise<SearchUsersResult> {
|
||||||
const userRepository = getCustomRepository(UserRepository)
|
const userRepository = getCustomRepository(UserRepository)
|
||||||
|
/*
|
||||||
const filterCriteria: ObjectLiteral[] = []
|
const filterCriteria: ObjectLiteral[] = []
|
||||||
if (filters) {
|
if (filters) {
|
||||||
if (filters.byActivated !== null) {
|
if (filters.byActivated !== null) {
|
||||||
filterCriteria.push({ emailChecked: filters.byActivated })
|
filterCriteria.push({ 'emailContact.emailChecked': filters.byActivated })
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filters.byDeleted !== null) {
|
if (filters.byDeleted !== null) {
|
||||||
filterCriteria.push({ deletedAt: filters.byDeleted ? Not(IsNull()) : IsNull() })
|
filterCriteria.push({ deletedAt: filters.byDeleted ? Not(IsNull()) : IsNull() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
const userFields = [
|
const userFields = [
|
||||||
'id',
|
'id',
|
||||||
'firstName',
|
'firstName',
|
||||||
'lastName',
|
'lastName',
|
||||||
'email',
|
'emailId',
|
||||||
'emailChecked',
|
'emailContact',
|
||||||
'deletedAt',
|
'deletedAt',
|
||||||
'isAdmin',
|
'isAdmin',
|
||||||
]
|
]
|
||||||
@ -101,7 +101,7 @@ export class AdminResolver {
|
|||||||
return 'user.' + fieldName
|
return 'user.' + fieldName
|
||||||
}),
|
}),
|
||||||
searchText,
|
searchText,
|
||||||
filterCriteria,
|
filters,
|
||||||
currentPage,
|
currentPage,
|
||||||
pageSize,
|
pageSize,
|
||||||
)
|
)
|
||||||
@ -249,7 +249,11 @@ export class AdminResolver {
|
|||||||
logger.info(
|
logger.info(
|
||||||
`adminCreateContribution(email=${email}, amount=${amount}, memo=${memo}, creationDate=${creationDate})`,
|
`adminCreateContribution(email=${email}, amount=${amount}, memo=${memo}, creationDate=${creationDate})`,
|
||||||
)
|
)
|
||||||
const emailContact = await UserContact.findOne({ email }, { withDeleted: true })
|
const emailContact = await UserContact.findOne({
|
||||||
|
where: { email },
|
||||||
|
withDeleted: true,
|
||||||
|
relations: ['user'],
|
||||||
|
})
|
||||||
if (!emailContact) {
|
if (!emailContact) {
|
||||||
logger.error(`Could not find user with email: ${email}`)
|
logger.error(`Could not find user with email: ${email}`)
|
||||||
throw new Error(`Could not find user with email: ${email}`)
|
throw new Error(`Could not find user with email: ${email}`)
|
||||||
@ -258,6 +262,10 @@ export class AdminResolver {
|
|||||||
logger.error('This emailContact was deleted. Cannot create a contribution.')
|
logger.error('This emailContact was deleted. Cannot create a contribution.')
|
||||||
throw new Error('This emailContact was deleted. Cannot create a contribution.')
|
throw new Error('This emailContact was deleted. Cannot create a contribution.')
|
||||||
}
|
}
|
||||||
|
if (emailContact.user.deletedAt) {
|
||||||
|
logger.error('This user was deleted. Cannot create a contribution.')
|
||||||
|
throw new Error('This user was deleted. Cannot create a contribution.')
|
||||||
|
}
|
||||||
if (!emailContact.emailChecked) {
|
if (!emailContact.emailChecked) {
|
||||||
logger.error('Contribution could not be saved, Email is not activated')
|
logger.error('Contribution could not be saved, Email is not activated')
|
||||||
throw new Error('Contribution could not be saved, Email is not activated')
|
throw new Error('Contribution could not be saved, Email is not activated')
|
||||||
@ -317,12 +325,16 @@ export class AdminResolver {
|
|||||||
@Args() { id, email, amount, memo, creationDate }: AdminUpdateContributionArgs,
|
@Args() { id, email, amount, memo, creationDate }: AdminUpdateContributionArgs,
|
||||||
@Ctx() context: Context,
|
@Ctx() context: Context,
|
||||||
): Promise<AdminUpdateContribution> {
|
): Promise<AdminUpdateContribution> {
|
||||||
const emailContact = await UserContact.findOne({ email }, { withDeleted: true })
|
const emailContact = await UserContact.findOne({
|
||||||
|
where: { email },
|
||||||
|
withDeleted: true,
|
||||||
|
relations: ['user'],
|
||||||
|
})
|
||||||
if (!emailContact) {
|
if (!emailContact) {
|
||||||
logger.error(`Could not find UserContact with email: ${email}`)
|
logger.error(`Could not find UserContact with email: ${email}`)
|
||||||
throw new Error(`Could not find UserContact with email: ${email}`)
|
throw new Error(`Could not find UserContact with email: ${email}`)
|
||||||
}
|
}
|
||||||
const user = await dbUser.findOne({ id: emailContact.userId }, { withDeleted: true })
|
const user = emailContact.user
|
||||||
if (!user) {
|
if (!user) {
|
||||||
logger.error(`Could not find User to emailContact: ${email}`)
|
logger.error(`Could not find User to emailContact: ${email}`)
|
||||||
throw new Error(`Could not find User to emailContact: ${email}`)
|
throw new Error(`Could not find User to emailContact: ${email}`)
|
||||||
@ -388,7 +400,11 @@ export class AdminResolver {
|
|||||||
|
|
||||||
const userIds = contributions.map((p) => p.userId)
|
const userIds = contributions.map((p) => p.userId)
|
||||||
const userCreations = await getUserCreations(userIds)
|
const userCreations = await getUserCreations(userIds)
|
||||||
const users = await dbUser.find({ where: { id: In(userIds) }, withDeleted: true })
|
const users = await dbUser.find({
|
||||||
|
where: { id: In(userIds) },
|
||||||
|
withDeleted: true,
|
||||||
|
relations: ['emailContact'],
|
||||||
|
})
|
||||||
|
|
||||||
return contributions.map((contribution) => {
|
return contributions.map((contribution) => {
|
||||||
const user = users.find((u) => u.id === contribution.userId)
|
const user = users.find((u) => u.id === contribution.userId)
|
||||||
|
|||||||
@ -24,15 +24,16 @@ export const creationFactory = async (
|
|||||||
logger.trace('creationFactory...')
|
logger.trace('creationFactory...')
|
||||||
await query({ query: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } })
|
await query({ query: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } })
|
||||||
logger.trace('creationFactory... after login')
|
logger.trace('creationFactory... after login')
|
||||||
|
|
||||||
// TODO it would be nice to have this mutation return the id
|
// TODO it would be nice to have this mutation return the id
|
||||||
await mutate({ mutation: adminCreateContribution, variables: { ...creation } })
|
await mutate({ mutation: adminCreateContribution, variables: { ...creation } })
|
||||||
logger.trace('creationFactory... after adminCreateContribution')
|
logger.trace('creationFactory... after adminCreateContribution')
|
||||||
|
|
||||||
const userContact = await UserContact.findOneOrFail({ where: { email: creation.email } })
|
const userContact = await UserContact.findOneOrFail({
|
||||||
|
where: { email: creation.email },
|
||||||
|
relations: ['user'],
|
||||||
|
})
|
||||||
logger.trace('creationFactory... after UserContact.findOneOrFail userContact=', userContact)
|
logger.trace('creationFactory... after UserContact.findOneOrFail userContact=', userContact)
|
||||||
const user = await User.findOneOrFail({ where: { id: userContact.userId } })
|
const user = userContact.user
|
||||||
logger.trace('creationFactory... after User.findOneOrFail user=', user)
|
|
||||||
|
|
||||||
const pendingCreation = await Contribution.findOneOrFail({
|
const pendingCreation = await Contribution.findOneOrFail({
|
||||||
where: { userId: user.id, amount: creation.amount },
|
where: { userId: user.id, amount: creation.amount },
|
||||||
@ -42,12 +43,10 @@ export const creationFactory = async (
|
|||||||
'creationFactory... after Contribution.findOneOrFail pendingCreation=',
|
'creationFactory... after Contribution.findOneOrFail pendingCreation=',
|
||||||
pendingCreation,
|
pendingCreation,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (creation.confirmed) {
|
if (creation.confirmed) {
|
||||||
logger.trace('creationFactory... creation.confirmed=', creation.confirmed)
|
logger.trace('creationFactory... creation.confirmed=', creation.confirmed)
|
||||||
await mutate({ mutation: confirmContribution, variables: { id: pendingCreation.id } })
|
await mutate({ mutation: confirmContribution, variables: { id: pendingCreation.id } })
|
||||||
logger.trace('creationFactory... after confirmContribution')
|
logger.trace('creationFactory... after confirmContribution')
|
||||||
|
|
||||||
const confirmedCreation = await Contribution.findOneOrFail({ id: pendingCreation.id })
|
const confirmedCreation = await Contribution.findOneOrFail({ id: pendingCreation.id })
|
||||||
logger.trace(
|
logger.trace(
|
||||||
'creationFactory... after Contribution.findOneOrFail confirmedCreation=',
|
'creationFactory... after Contribution.findOneOrFail confirmedCreation=',
|
||||||
@ -61,6 +60,7 @@ export const creationFactory = async (
|
|||||||
order: { balanceDate: 'DESC' },
|
order: { balanceDate: 'DESC' },
|
||||||
})
|
})
|
||||||
logger.trace('creationFactory... after Transaction.findOneOrFail transaction=', transaction)
|
logger.trace('creationFactory... after Transaction.findOneOrFail transaction=', transaction)
|
||||||
|
|
||||||
if (transaction.decay.equals(0) && transaction.creationDate) {
|
if (transaction.decay.equals(0) && transaction.creationDate) {
|
||||||
confirmedCreation.contributionDate = new Date(
|
confirmedCreation.contributionDate = new Date(
|
||||||
nMonthsBefore(transaction.creationDate, creation.moveCreationDate),
|
nMonthsBefore(transaction.creationDate, creation.moveCreationDate),
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { Brackets, EntityRepository, ObjectLiteral, Repository } from '@dbTools/typeorm'
|
import SearchUsersFilters from '@/graphql/arg/SearchUsersFilters'
|
||||||
|
import { Brackets, EntityRepository, IsNull, Not, Repository } from '@dbTools/typeorm'
|
||||||
import { User as DbUser } from '@entity/User'
|
import { User as DbUser } from '@entity/User'
|
||||||
|
|
||||||
@EntityRepository(DbUser)
|
@EntityRepository(DbUser)
|
||||||
@ -21,17 +22,18 @@ export class UserRepository extends Repository<DbUser> {
|
|||||||
async findBySearchCriteriaPagedFiltered(
|
async findBySearchCriteriaPagedFiltered(
|
||||||
select: string[],
|
select: string[],
|
||||||
searchCriteria: string,
|
searchCriteria: string,
|
||||||
filterCriteria: ObjectLiteral[],
|
filters: SearchUsersFilters,
|
||||||
currentPage: number,
|
currentPage: number,
|
||||||
pageSize: number,
|
pageSize: number,
|
||||||
): Promise<[DbUser[], number]> {
|
): Promise<[DbUser[], number]> {
|
||||||
const query = this.createQueryBuilder('user')
|
const query = this.createQueryBuilder('user')
|
||||||
.select(select)
|
.select(select)
|
||||||
|
.leftJoinAndSelect('user.emailContact', 'emailContact')
|
||||||
.withDeleted()
|
.withDeleted()
|
||||||
.where(
|
.where(
|
||||||
new Brackets((qb) => {
|
new Brackets((qb) => {
|
||||||
qb.where(
|
qb.where(
|
||||||
'user.firstName like :name or user.lastName like :lastName or user.email like :email',
|
'user.firstName like :name or user.lastName like :lastName or emailContact.email like :email',
|
||||||
{
|
{
|
||||||
name: `%${searchCriteria}%`,
|
name: `%${searchCriteria}%`,
|
||||||
lastName: `%${searchCriteria}%`,
|
lastName: `%${searchCriteria}%`,
|
||||||
@ -40,9 +42,23 @@ export class UserRepository extends Repository<DbUser> {
|
|||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
/*
|
||||||
filterCriteria.forEach((filter) => {
|
filterCriteria.forEach((filter) => {
|
||||||
query.andWhere(filter)
|
query.andWhere(filter)
|
||||||
})
|
})
|
||||||
|
*/
|
||||||
|
if (filters) {
|
||||||
|
if (filters.byActivated !== null) {
|
||||||
|
query.andWhere('emailContact.emailChecked = :value', { value: filters.byActivated })
|
||||||
|
// filterCriteria.push({ 'emailContact.emailChecked': filters.byActivated })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filters.byDeleted !== null) {
|
||||||
|
// filterCriteria.push({ deletedAt: filters.byDeleted ? Not(IsNull()) : IsNull() })
|
||||||
|
query.andWhere({ deletedAt: filters.byDeleted ? Not(IsNull()) : IsNull() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return query
|
return query
|
||||||
.take(pageSize)
|
.take(pageSize)
|
||||||
.skip((currentPage - 1) * pageSize)
|
.skip((currentPage - 1) * pageSize)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user