Merge remote-tracking branch 'origin/master' into 1798-feature-gradidoid-1-adapt-and-migrate-database-schema

This commit is contained in:
Claus-Peter Hübner 2022-07-19 21:11:28 +02:00
commit 4951a684c6
7 changed files with 207 additions and 34 deletions

View File

@ -26,6 +26,7 @@ export enum RIGHTS {
LIST_TRANSACTION_LINKS = 'LIST_TRANSACTION_LINKS', LIST_TRANSACTION_LINKS = 'LIST_TRANSACTION_LINKS',
GDT_BALANCE = 'GDT_BALANCE', GDT_BALANCE = 'GDT_BALANCE',
CREATE_CONTRIBUTION = 'CREATE_CONTRIBUTION', CREATE_CONTRIBUTION = 'CREATE_CONTRIBUTION',
DELETE_CONTRIBUTION = 'DELETE_CONTRIBUTION',
LIST_CONTRIBUTIONS = 'LIST_CONTRIBUTIONS', LIST_CONTRIBUTIONS = 'LIST_CONTRIBUTIONS',
LIST_ALL_CONTRIBUTIONS = 'LIST_ALL_CONTRIBUTIONS', LIST_ALL_CONTRIBUTIONS = 'LIST_ALL_CONTRIBUTIONS',
UPDATE_CONTRIBUTION = 'UPDATE_CONTRIBUTION', UPDATE_CONTRIBUTION = 'UPDATE_CONTRIBUTION',

View File

@ -24,6 +24,7 @@ export const ROLE_USER = new Role('user', [
RIGHTS.LIST_TRANSACTION_LINKS, RIGHTS.LIST_TRANSACTION_LINKS,
RIGHTS.GDT_BALANCE, RIGHTS.GDT_BALANCE,
RIGHTS.CREATE_CONTRIBUTION, RIGHTS.CREATE_CONTRIBUTION,
RIGHTS.DELETE_CONTRIBUTION,
RIGHTS.LIST_CONTRIBUTIONS, RIGHTS.LIST_CONTRIBUTIONS,
RIGHTS.LIST_ALL_CONTRIBUTIONS, RIGHTS.LIST_ALL_CONTRIBUTIONS,
RIGHTS.UPDATE_CONTRIBUTION, RIGHTS.UPDATE_CONTRIBUTION,

View File

@ -48,13 +48,13 @@ export class Contribution {
@ObjectType() @ObjectType()
export class ContributionListResult { export class ContributionListResult {
constructor(count: number, list: Contribution[]) { constructor(count: number, list: Contribution[]) {
this.linkCount = count this.contributionCount = count
this.linkList = list this.contributionList = list
} }
@Field(() => Int) @Field(() => Int)
linkCount: number contributionCount: number
@Field(() => [Contribution]) @Field(() => [Contribution])
linkList: Contribution[] contributionList: Contribution[]
} }

View File

@ -4,7 +4,9 @@
import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg'
import { import {
adminUpdateContribution, adminUpdateContribution,
confirmContribution,
createContribution, createContribution,
deleteContribution,
updateContribution, updateContribution,
} from '@/seeds/graphql/mutations' } from '@/seeds/graphql/mutations'
import { listAllContributions, listContributions, login } from '@/seeds/graphql/queries' import { listAllContributions, listContributions, login } from '@/seeds/graphql/queries'
@ -193,7 +195,9 @@ describe('ContributionResolver', () => {
).resolves.toEqual( ).resolves.toEqual(
expect.objectContaining({ expect.objectContaining({
data: { data: {
listContributions: expect.arrayContaining([ listContributions: {
contributionCount: 2,
contributionList: expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
id: expect.any(Number), id: expect.any(Number),
memo: 'Herzlich Willkommen bei Gradido!', memo: 'Herzlich Willkommen bei Gradido!',
@ -206,6 +210,7 @@ describe('ContributionResolver', () => {
}), }),
]), ]),
}, },
},
}), }),
) )
}) })
@ -226,7 +231,9 @@ describe('ContributionResolver', () => {
).resolves.toEqual( ).resolves.toEqual(
expect.objectContaining({ expect.objectContaining({
data: { data: {
listContributions: expect.arrayContaining([ listContributions: {
contributionCount: 1,
contributionList: expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
id: expect.any(Number), id: expect.any(Number),
memo: 'Test env contribution', memo: 'Test env contribution',
@ -234,6 +241,7 @@ describe('ContributionResolver', () => {
}), }),
]), ]),
}, },
},
}), }),
) )
}) })
@ -481,6 +489,11 @@ describe('ContributionResolver', () => {
}) })
}) })
afterAll(async () => {
await cleanDB()
resetToken()
})
it('returns allCreation', async () => { it('returns allCreation', async () => {
await expect( await expect(
query({ query({
@ -496,8 +509,8 @@ describe('ContributionResolver', () => {
expect.objectContaining({ expect.objectContaining({
data: { data: {
listAllContributions: { listAllContributions: {
linkCount: 2, contributionCount: 2,
linkList: expect.arrayContaining([ contributionList: expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
id: expect.any(Number), id: expect.any(Number),
memo: 'Herzlich Willkommen bei Gradido!', memo: 'Herzlich Willkommen bei Gradido!',
@ -516,4 +529,129 @@ describe('ContributionResolver', () => {
}) })
}) })
}) })
describe('deleteContribution', () => {
describe('unauthenticated', () => {
it('returns an error', async () => {
await expect(
query({
query: deleteContribution,
variables: {
id: -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('401 Unauthorized')],
}),
)
})
})
describe('authenticated', () => {
beforeAll(async () => {
await userFactory(testEnv, bibiBloxberg)
await userFactory(testEnv, peterLustig)
await query({
query: login,
variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' },
})
result = await mutate({
mutation: createContribution,
variables: {
amount: 100.0,
memo: 'Test env contribution',
creationDate: new Date().toString(),
},
})
})
afterAll(async () => {
await cleanDB()
resetToken()
})
describe('wrong contribution id', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: deleteContribution,
variables: {
id: -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Contribution not found for given id.')],
}),
)
})
})
describe('other user sends a deleteContribtuion', () => {
it('returns an error', async () => {
await query({
query: login,
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
})
await expect(
mutate({
mutation: deleteContribution,
variables: {
id: result.data.createContribution.id,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Can not delete contribution of another user')],
}),
)
})
})
describe('User deletes own contribution', () => {
it('deletes successfully', async () => {
await expect(
mutate({
mutation: deleteContribution,
variables: {
id: result.data.createContribution.id,
},
}),
).resolves.toBeTruthy()
})
})
describe('User deletes already confirmed contribution', () => {
it('throws an error', async () => {
await query({
query: login,
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
})
await mutate({
mutation: confirmContribution,
variables: {
id: result.data.createContribution.id,
},
})
await query({
query: login,
variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' },
})
await expect(
mutate({
mutation: deleteContribution,
variables: {
id: result.data.createContribution.id,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('A confirmed contribution can not be deleted')],
}),
)
})
})
})
})
}) })

View File

@ -38,22 +38,43 @@ export class ContributionResolver {
return new UnconfirmedContribution(contribution, user, creations) return new UnconfirmedContribution(contribution, user, creations)
} }
@Authorized([RIGHTS.DELETE_CONTRIBUTION])
@Mutation(() => Boolean)
async deleteContribution(
@Arg('id', () => Int) id: number,
@Ctx() context: Context,
): Promise<boolean> {
const user = getUser(context)
const contribution = await dbContribution.findOne(id)
if (!contribution) {
throw new Error('Contribution not found for given id.')
}
if (contribution.userId !== user.id) {
throw new Error('Can not delete contribution of another user')
}
if (contribution.confirmedAt) {
throw new Error('A confirmed contribution can not be deleted')
}
const res = await contribution.softRemove()
return !!res
}
@Authorized([RIGHTS.LIST_CONTRIBUTIONS]) @Authorized([RIGHTS.LIST_CONTRIBUTIONS])
@Query(() => [Contribution]) @Query(() => ContributionListResult)
async listContributions( async listContributions(
@Args() @Args()
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated, { currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
@Arg('filterConfirmed', () => Boolean) @Arg('filterConfirmed', () => Boolean)
filterConfirmed: boolean | null, filterConfirmed: boolean | null,
@Ctx() context: Context, @Ctx() context: Context,
): Promise<Contribution[]> { ): Promise<ContributionListResult> {
const user = getUser(context) const user = getUser(context)
const where: { const where: {
userId: number userId: number
confirmedBy?: FindOperator<number> | null confirmedBy?: FindOperator<number> | null
} = { userId: user.id } } = { userId: user.id }
if (filterConfirmed) where.confirmedBy = IsNull() if (filterConfirmed) where.confirmedBy = IsNull()
const contributions = await dbContribution.find({ const [contributions, count] = await dbContribution.findAndCount({
where, where,
order: { order: {
createdAt: order, createdAt: order,
@ -62,7 +83,10 @@ export class ContributionResolver {
skip: (currentPage - 1) * pageSize, skip: (currentPage - 1) * pageSize,
take: pageSize, take: pageSize,
}) })
return contributions.map((contribution) => new Contribution(contribution, new User(user))) return new ContributionListResult(
count,
contributions.map((contribution) => new Contribution(contribution, new User(user))),
)
} }
@Authorized([RIGHTS.LIST_ALL_CONTRIBUTIONS]) @Authorized([RIGHTS.LIST_ALL_CONTRIBUTIONS])

View File

@ -255,3 +255,9 @@ export const updateContribution = gql`
} }
} }
` `
export const deleteContribution = gql`
mutation ($id: Int!) {
deleteContribution(id: $id)
}
`

View File

@ -185,18 +185,21 @@ export const listContributions = gql`
order: $order order: $order
filterConfirmed: $filterConfirmed filterConfirmed: $filterConfirmed
) { ) {
contributionCount
contributionList {
id id
amount amount
memo memo
} }
} }
}
` `
export const listAllContributions = ` export const listAllContributions = `
query ($currentPage: Int = 1, $pageSize: Int = 5, $order: Order = DESC) { query ($currentPage: Int = 1, $pageSize: Int = 5, $order: Order = DESC) {
listAllContributions(currentPage: $currentPage, pageSize: $pageSize, order: $order) { listAllContributions(currentPage: $currentPage, pageSize: $pageSize, order: $order) {
linkCount contributionCount
linkList { contributionList {
id id
firstName firstName
lastName lastName