Merge pull request #1949 from gradido/contributions-table

refactor: Admin Pending Creations Table to Contributions Table
This commit is contained in:
Moriz Wahl 2022-06-15 23:34:43 +02:00 committed by GitHub
commit c92a5e815f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 492 additions and 323 deletions

View File

@ -1,14 +1,14 @@
import { mount } from '@vue/test-utils'
import CreationFormular from './CreationFormular.vue'
import { createPendingCreation } from '../graphql/createPendingCreation'
import { createPendingCreations } from '../graphql/createPendingCreations'
import { adminCreateContribution } from '../graphql/adminCreateContribution'
import { adminCreateContributions } from '../graphql/adminCreateContributions'
import { toastErrorSpy, toastSuccessSpy } from '../../test/testSetup'
const localVue = global.localVue
const apolloMutateMock = jest.fn().mockResolvedValue({
data: {
createPendingCreation: [0, 0, 0],
adminCreateContribution: [0, 0, 0],
},
})
const stateCommitMock = jest.fn()
@ -110,7 +110,7 @@ describe('CreationFormular', () => {
it('sends ... to apollo', () => {
expect(apolloMutateMock).toBeCalledWith(
expect.objectContaining({
mutation: createPendingCreation,
mutation: adminCreateContribution,
variables: {
email: 'benjamin@bluemchen.de',
creationDate: getCreationDate(2),
@ -334,10 +334,10 @@ describe('CreationFormular', () => {
jest.clearAllMocks()
apolloMutateMock.mockResolvedValue({
data: {
createPendingCreations: {
adminCreateContributions: {
success: true,
successfulCreation: ['bob@baumeister.de', 'bibi@bloxberg.de'],
failedCreation: [],
successfulContribution: ['bob@baumeister.de', 'bibi@bloxberg.de'],
failedContribution: [],
},
},
})
@ -355,7 +355,7 @@ describe('CreationFormular', () => {
it('calls the API', () => {
expect(apolloMutateMock).toBeCalledWith(
expect.objectContaining({
mutation: createPendingCreations,
mutation: adminCreateContributions,
variables: {
pendingCreations: [
{
@ -390,10 +390,10 @@ describe('CreationFormular', () => {
jest.clearAllMocks()
apolloMutateMock.mockResolvedValue({
data: {
createPendingCreations: {
adminCreateContributions: {
success: true,
successfulCreation: [],
failedCreation: ['bob@baumeister.de', 'bibi@bloxberg.de'],
successfulContribution: [],
failedContribution: ['bob@baumeister.de', 'bibi@bloxberg.de'],
},
},
})

View File

@ -85,8 +85,8 @@
</div>
</template>
<script>
import { createPendingCreation } from '../graphql/createPendingCreation'
import { createPendingCreations } from '../graphql/createPendingCreations'
import { adminCreateContribution } from '../graphql/adminCreateContribution'
import { adminCreateContributions } from '../graphql/adminCreateContributions'
import { creationMonths } from '../mixins/creationMonths'
export default {
name: 'CreationFormular',
@ -158,25 +158,25 @@ export default {
})
this.$apollo
.mutate({
mutation: createPendingCreations,
mutation: adminCreateContributions,
variables: {
pendingCreations: submitObj,
},
fetchPolicy: 'no-cache',
})
.then((result) => {
const failedCreations = []
const failedContributions = []
this.$store.commit(
'openCreationsPlus',
result.data.createPendingCreations.successfulCreation.length,
result.data.adminCreateContributions.successfulContribution.length,
)
if (result.data.createPendingCreations.failedCreation.length > 0) {
result.data.createPendingCreations.failedCreation.forEach((email) => {
failedCreations.push(email)
if (result.data.adminCreateContributions.failedContribution.length > 0) {
result.data.adminCreateContributions.failedContribution.forEach((email) => {
failedContributions.push(email)
})
}
this.$emit('remove-all-bookmark')
this.$emit('toast-failed-creations', failedCreations)
this.$emit('toast-failed-creations', failedContributions)
})
.catch((error) => {
this.toastError(error.message)
@ -190,11 +190,11 @@ export default {
}
this.$apollo
.mutate({
mutation: createPendingCreation,
mutation: adminCreateContribution,
variables: submitObj,
})
.then((result) => {
this.$emit('update-user-data', this.item, result.data.createPendingCreation)
this.$emit('update-user-data', this.item, result.data.adminCreateContribution)
this.$store.commit('openCreationsPlus', 1)
this.toastSuccess(
this.$t('creation_form.toasted', {

View File

@ -6,7 +6,7 @@ const localVue = global.localVue
const apolloMutateMock = jest.fn().mockResolvedValue({
data: {
updatePendingCreation: {
adminUpdateContribution: {
creation: [0, 0, 0],
amount: 500,
date: new Date(),

View File

@ -73,7 +73,7 @@
</div>
</template>
<script>
import { updatePendingCreation } from '../graphql/updatePendingCreation'
import { adminUpdateContribution } from '../graphql/adminUpdateContribution'
import { creationMonths } from '../mixins/creationMonths'
export default {
@ -113,7 +113,7 @@ export default {
submitCreation() {
this.$apollo
.mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: this.item.id,
email: this.item.email,
@ -123,11 +123,11 @@ export default {
},
})
.then((result) => {
this.$emit('update-user-data', this.item, result.data.updatePendingCreation.creation)
this.$emit('update-user-data', this.item, result.data.adminUpdateContribution.creation)
this.$emit('update-creation-data', {
amount: Number(result.data.updatePendingCreation.amount),
date: result.data.updatePendingCreation.date,
memo: result.data.updatePendingCreation.memo,
amount: Number(result.data.adminUpdateContribution.amount),
date: result.data.adminUpdateContribution.date,
memo: result.data.adminUpdateContribution.memo,
row: this.row,
})
this.toastSuccess(

View File

@ -0,0 +1,12 @@
import gql from 'graphql-tag'
export const adminCreateContribution = gql`
mutation ($email: String!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
adminCreateContribution(
email: $email
amount: $amount
memo: $memo
creationDate: $creationDate
)
}
`

View File

@ -0,0 +1,11 @@
import gql from 'graphql-tag'
export const adminCreateContributions = gql`
mutation ($pendingCreations: [AdminCreateContributionArgs!]!) {
adminCreateContributions(pendingCreations: $pendingCreations) {
success
successfulContribution
failedContribution
}
}
`

View File

@ -0,0 +1,7 @@
import gql from 'graphql-tag'
export const adminDeleteContribution = gql`
mutation ($id: Int!) {
adminDeleteContribution(id: $id)
}
`

View File

@ -1,8 +1,8 @@
import gql from 'graphql-tag'
export const updatePendingCreation = gql`
export const adminUpdateContribution = gql`
mutation ($id: Int!, $email: String!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
updatePendingCreation(
adminUpdateContribution(
id: $id
email: $email
amount: $amount

View File

@ -0,0 +1,7 @@
import gql from 'graphql-tag'
export const confirmContribution = gql`
mutation ($id: Int!) {
confirmContribution(id: $id)
}
`

View File

@ -1,7 +0,0 @@
import gql from 'graphql-tag'
export const confirmPendingCreation = gql`
mutation ($id: Int!) {
confirmPendingCreation(id: $id)
}
`

View File

@ -1,7 +0,0 @@
import gql from 'graphql-tag'
export const createPendingCreation = gql`
mutation ($email: String!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
createPendingCreation(email: $email, amount: $amount, memo: $memo, creationDate: $creationDate)
}
`

View File

@ -1,11 +0,0 @@
import gql from 'graphql-tag'
export const createPendingCreations = gql`
mutation ($pendingCreations: [CreatePendingCreationArgs!]!) {
createPendingCreations(pendingCreations: $pendingCreations) {
success
successfulCreation
failedCreation
}
}
`

View File

@ -1,7 +0,0 @@
import gql from 'graphql-tag'
export const deletePendingCreation = gql`
mutation ($id: Int!) {
deletePendingCreation(id: $id)
}
`

View File

@ -1,8 +1,8 @@
import gql from 'graphql-tag'
export const getPendingCreations = gql`
export const listUnconfirmedContributions = gql`
query {
getPendingCreations {
listUnconfirmedContributions {
id
firstName
lastName

View File

@ -1,7 +1,7 @@
import { mount } from '@vue/test-utils'
import CreationConfirm from './CreationConfirm.vue'
import { deletePendingCreation } from '../graphql/deletePendingCreation'
import { confirmPendingCreation } from '../graphql/confirmPendingCreation'
import { adminDeleteContribution } from '../graphql/adminDeleteContribution'
import { confirmContribution } from '../graphql/confirmContribution'
import { toastErrorSpy, toastSuccessSpy } from '../../test/testSetup'
const localVue = global.localVue
@ -9,7 +9,7 @@ const localVue = global.localVue
const storeCommitMock = jest.fn()
const apolloQueryMock = jest.fn().mockResolvedValue({
data: {
getPendingCreations: [
listUnconfirmedContributions: [
{
id: 1,
firstName: 'Bibi',
@ -84,9 +84,9 @@ describe('CreationConfirm', () => {
await wrapper.findAll('tr').at(1).findAll('button').at(0).trigger('click')
})
it('calls the deletePendingCreation mutation', () => {
it('calls the adminDeleteContribution mutation', () => {
expect(apolloMutateMock).toBeCalledWith({
mutation: deletePendingCreation,
mutation: adminDeleteContribution,
variables: { id: 1 },
})
})
@ -141,9 +141,9 @@ describe('CreationConfirm', () => {
await wrapper.find('#overlay').findAll('button').at(1).trigger('click')
})
it('calls the confirmPendingCreation mutation', () => {
it('calls the confirmContribution mutation', () => {
expect(apolloMutateMock).toBeCalledWith({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: { id: 2 },
})
})

View File

@ -15,9 +15,9 @@
<script>
import Overlay from '../components/Overlay.vue'
import OpenCreationsTable from '../components/Tables/OpenCreationsTable.vue'
import { getPendingCreations } from '../graphql/getPendingCreations'
import { deletePendingCreation } from '../graphql/deletePendingCreation'
import { confirmPendingCreation } from '../graphql/confirmPendingCreation'
import { listUnconfirmedContributions } from '../graphql/listUnconfirmedContributions'
import { adminDeleteContribution } from '../graphql/adminDeleteContribution'
import { confirmContribution } from '../graphql/confirmContribution'
export default {
name: 'CreationConfirm',
@ -36,7 +36,7 @@ export default {
removeCreation(item) {
this.$apollo
.mutate({
mutation: deletePendingCreation,
mutation: adminDeleteContribution,
variables: {
id: item.id,
},
@ -52,7 +52,7 @@ export default {
confirmCreation() {
this.$apollo
.mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: this.item.id,
},
@ -70,13 +70,13 @@ export default {
getPendingCreations() {
this.$apollo
.query({
query: getPendingCreations,
query: listUnconfirmedContributions,
fetchPolicy: 'network-only',
})
.then((result) => {
this.$store.commit('resetOpenCreations')
this.pendingCreations = result.data.getPendingCreations
this.$store.commit('setOpenCreations', result.data.getPendingCreations.length)
this.pendingCreations = result.data.listUnconfirmedContributions
this.$store.commit('setOpenCreations', result.data.listUnconfirmedContributions.length)
})
.catch((error) => {
this.toastError(error.message)

View File

@ -5,7 +5,7 @@ const localVue = global.localVue
const apolloQueryMock = jest.fn().mockResolvedValue({
data: {
getPendingCreations: [
listUnconfirmedContributions: [
{
pending: true,
},
@ -46,7 +46,7 @@ describe('Overview', () => {
wrapper = Wrapper()
})
it('calls getPendingCreations', () => {
it('calls listUnconfirmedContributions', () => {
expect(apolloQueryMock).toBeCalled()
})

View File

@ -31,7 +31,7 @@
</div>
</template>
<script>
import { getPendingCreations } from '../graphql/getPendingCreations'
import { listUnconfirmedContributions } from '../graphql/listUnconfirmedContributions'
export default {
name: 'overview',
@ -39,11 +39,11 @@ export default {
async getPendingCreations() {
this.$apollo
.query({
query: getPendingCreations,
query: listUnconfirmedContributions,
fetchPolicy: 'network-only',
})
.then((result) => {
this.$store.commit('setOpenCreations', result.data.getPendingCreations.length)
this.$store.commit('setOpenCreations', result.data.listUnconfirmedContributions.length)
})
},
},

View File

@ -27,11 +27,12 @@ export enum RIGHTS {
GDT_BALANCE = 'GDT_BALANCE',
// Admin
SEARCH_USERS = 'SEARCH_USERS',
CREATE_PENDING_CREATION = 'CREATE_PENDING_CREATION',
UPDATE_PENDING_CREATION = 'UPDATE_PENDING_CREATION',
SEARCH_PENDING_CREATION = 'SEARCH_PENDING_CREATION',
DELETE_PENDING_CREATION = 'DELETE_PENDING_CREATION',
CONFIRM_PENDING_CREATION = 'CONFIRM_PENDING_CREATION',
ADMIN_CREATE_CONTRIBUTION = 'ADMIN_CREATE_CONTRIBUTION',
ADMIN_CREATE_CONTRIBUTIONS = 'ADMIN_CREATE_CONTRIBUTIONS',
ADMIN_UPDATE_CONTRIBUTION = 'ADMIN_UPDATE_CONTRIBUTION',
ADMIN_DELETE_CONTRIBUTION = 'ADMIN_DELETE_CONTRIBUTION',
LIST_UNCONFIRMED_CONTRIBUTIONS = 'LIST_UNCONFIRMED_CONTRIBUTIONS',
CONFIRM_CONTRIBUTION = 'CONFIRM_CONTRIBUTION',
SEND_ACTIVATION_EMAIL = 'SEND_ACTIVATION_EMAIL',
DELETE_USER = 'DELETE_USER',
UNDELETE_USER = 'UNDELETE_USER',

View File

@ -10,7 +10,7 @@ Decimal.set({
})
const constants = {
DB_VERSION: '0038-add_contribution_links_table',
DB_VERSION: '0039-contributions_table',
DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0
LOG4JS_CONFIG: 'log4js-config.json',
// default log level on production should be info

View File

@ -3,7 +3,7 @@ import Decimal from 'decimal.js-light'
@InputType()
@ArgsType()
export default class CreatePendingCreationArgs {
export default class AdminCreateContributionArgs {
@Field(() => String)
email: string

View File

@ -2,7 +2,7 @@ import { ArgsType, Field, Int } from 'type-graphql'
import Decimal from 'decimal.js-light'
@ArgsType()
export default class UpdatePendingCreationArgs {
export default class AdminUpdateContributionArgs {
@Field(() => Int)
id: number

View File

@ -1,19 +1,19 @@
import { ObjectType, Field } from 'type-graphql'
@ObjectType()
export class CreatePendingCreations {
export class AdminCreateContributions {
constructor() {
this.success = false
this.successfulCreation = []
this.failedCreation = []
this.successfulContribution = []
this.failedContribution = []
}
@Field(() => Boolean)
success: boolean
@Field(() => [String])
successfulCreation: string[]
successfulContribution: string[]
@Field(() => [String])
failedCreation: string[]
failedContribution: string[]
}

View File

@ -2,7 +2,7 @@ import { ObjectType, Field } from 'type-graphql'
import Decimal from 'decimal.js-light'
@ObjectType()
export class UpdatePendingCreation {
export class AdminUpdateContribution {
@Field(() => Date)
date: Date

View File

@ -2,7 +2,7 @@ import { ObjectType, Field, Int } from 'type-graphql'
import Decimal from 'decimal.js-light'
@ObjectType()
export class PendingCreation {
export class UnconfirmedContribution {
@Field(() => String)
firstName: string

View File

@ -15,17 +15,17 @@ import { garrickOllivander } from '@/seeds/users/garrick-ollivander'
import {
deleteUser,
unDeleteUser,
createPendingCreation,
createPendingCreations,
updatePendingCreation,
deletePendingCreation,
confirmPendingCreation,
adminCreateContribution,
adminCreateContributions,
adminUpdateContribution,
adminDeleteContribution,
confirmContribution,
createContributionLink,
deleteContributionLink,
updateContributionLink,
} from '@/seeds/graphql/mutations'
import {
getPendingCreations,
listUnconfirmedContributions,
login,
searchUsers,
listTransactionLinksAdmin,
@ -36,7 +36,7 @@ import { User } from '@entity/User'
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
import Decimal from 'decimal.js-light'
import { AdminPendingCreation } from '@entity/AdminPendingCreation'
import { Contribution } from '@entity/Contribution'
import { Transaction as DbTransaction } from '@entity/Transaction'
import { ContributionLink as DbContributionLink } from '@entity/ContributionLink'
@ -66,7 +66,7 @@ afterAll(async () => {
let admin: User
let user: User
let creation: AdminPendingCreation | void
let creation: Contribution | void
describe('AdminResolver', () => {
describe('delete user', () => {
@ -502,9 +502,9 @@ describe('AdminResolver', () => {
}
describe('unauthenticated', () => {
describe('createPendingCreation', () => {
describe('adminCreateContribution', () => {
it('returns an error', async () => {
await expect(mutate({ mutation: createPendingCreation, variables })).resolves.toEqual(
await expect(mutate({ mutation: adminCreateContribution, variables })).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('401 Unauthorized')],
}),
@ -512,11 +512,11 @@ describe('AdminResolver', () => {
})
})
describe('createPendingCreations', () => {
describe('adminCreateContributions', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: createPendingCreations,
mutation: adminCreateContributions,
variables: { pendingCreations: [variables] },
}),
).resolves.toEqual(
@ -527,11 +527,11 @@ describe('AdminResolver', () => {
})
})
describe('updatePendingCreation', () => {
describe('adminUpdateContribution', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: 1,
email: 'bibi@bloxberg.de',
@ -548,11 +548,11 @@ describe('AdminResolver', () => {
})
})
describe('getPendingCreations', () => {
describe('listUnconfirmedContributions', () => {
it('returns an error', async () => {
await expect(
query({
query: getPendingCreations,
query: listUnconfirmedContributions,
}),
).resolves.toEqual(
expect.objectContaining({
@ -562,11 +562,11 @@ describe('AdminResolver', () => {
})
})
describe('deletePendingCreation', () => {
describe('adminDeleteContribution', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
mutation: adminDeleteContribution,
variables: {
id: 1,
},
@ -579,11 +579,11 @@ describe('AdminResolver', () => {
})
})
describe('confirmPendingCreation', () => {
describe('confirmContribution', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: 1,
},
@ -612,9 +612,9 @@ describe('AdminResolver', () => {
resetToken()
})
describe('createPendingCreation', () => {
describe('adminCreateContribution', () => {
it('returns an error', async () => {
await expect(mutate({ mutation: createPendingCreation, variables })).resolves.toEqual(
await expect(mutate({ mutation: adminCreateContribution, variables })).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('401 Unauthorized')],
}),
@ -622,11 +622,11 @@ describe('AdminResolver', () => {
})
})
describe('createPendingCreations', () => {
describe('adminCreateContributions', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: createPendingCreations,
mutation: adminCreateContributions,
variables: { pendingCreations: [variables] },
}),
).resolves.toEqual(
@ -637,11 +637,11 @@ describe('AdminResolver', () => {
})
})
describe('updatePendingCreation', () => {
describe('adminUpdateContribution', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: 1,
email: 'bibi@bloxberg.de',
@ -658,11 +658,11 @@ describe('AdminResolver', () => {
})
})
describe('getPendingCreations', () => {
describe('listUnconfirmedContributions', () => {
it('returns an error', async () => {
await expect(
query({
query: getPendingCreations,
query: listUnconfirmedContributions,
}),
).resolves.toEqual(
expect.objectContaining({
@ -672,11 +672,11 @@ describe('AdminResolver', () => {
})
})
describe('deletePendingCreation', () => {
describe('adminDeleteContribution', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
mutation: adminDeleteContribution,
variables: {
id: 1,
},
@ -689,11 +689,11 @@ describe('AdminResolver', () => {
})
})
describe('confirmPendingCreation', () => {
describe('confirmContribution', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: 1,
},
@ -721,7 +721,7 @@ describe('AdminResolver', () => {
resetToken()
})
describe('createPendingCreation', () => {
describe('adminCreateContribution', () => {
beforeAll(async () => {
const now = new Date()
creation = await creationFactory(testEnv, {
@ -734,7 +734,9 @@ describe('AdminResolver', () => {
describe('user to create for does not exist', () => {
it('throws an error', async () => {
await expect(mutate({ mutation: createPendingCreation, variables })).resolves.toEqual(
await expect(
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Could not find user with email: bibi@bloxberg.de')],
}),
@ -749,9 +751,13 @@ describe('AdminResolver', () => {
})
it('throws an error', async () => {
await expect(mutate({ mutation: createPendingCreation, variables })).resolves.toEqual(
await expect(
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('This user was deleted. Cannot make a creation.')],
errors: [
new GraphQLError('This user was deleted. Cannot create a contribution.'),
],
}),
)
})
@ -764,9 +770,13 @@ describe('AdminResolver', () => {
})
it('throws an error', async () => {
await expect(mutate({ mutation: createPendingCreation, variables })).resolves.toEqual(
await expect(
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Creation could not be saved, Email is not activated')],
errors: [
new GraphQLError('Contribution could not be saved, Email is not activated'),
],
}),
)
})
@ -781,7 +791,7 @@ describe('AdminResolver', () => {
describe('date of creation is not a date string', () => {
it('throws an error', async () => {
await expect(
mutate({ mutation: createPendingCreation, variables }),
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [
@ -801,7 +811,7 @@ describe('AdminResolver', () => {
1,
).toString()
await expect(
mutate({ mutation: createPendingCreation, variables }),
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [
@ -821,7 +831,7 @@ describe('AdminResolver', () => {
1,
).toString()
await expect(
mutate({ mutation: createPendingCreation, variables }),
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [
@ -836,7 +846,7 @@ describe('AdminResolver', () => {
it('throws an error', async () => {
variables.creationDate = new Date().toString()
await expect(
mutate({ mutation: createPendingCreation, variables }),
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [
@ -853,11 +863,11 @@ describe('AdminResolver', () => {
it('returns an array of the open creations for the last three months', async () => {
variables.amount = new Decimal(200)
await expect(
mutate({ mutation: createPendingCreation, variables }),
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
data: {
createPendingCreation: [1000, 1000, 800],
adminCreateContribution: [1000, 1000, 800],
},
}),
)
@ -868,7 +878,7 @@ describe('AdminResolver', () => {
it('returns an array of the open creations for the last three months', async () => {
variables.amount = new Decimal(1000)
await expect(
mutate({ mutation: createPendingCreation, variables }),
mutate({ mutation: adminCreateContribution, variables }),
).resolves.toEqual(
expect.objectContaining({
errors: [
@ -883,7 +893,7 @@ describe('AdminResolver', () => {
})
})
describe('createPendingCreations', () => {
describe('adminCreateContributions', () => {
// at this point we have this data in DB:
// bibi@bloxberg.de: [1000, 1000, 800]
// peter@lustig.de: [1000, 600, 1000]
@ -908,16 +918,16 @@ describe('AdminResolver', () => {
it('returns success, two successful creation and three failed creations', async () => {
await expect(
mutate({
mutation: createPendingCreations,
mutation: adminCreateContributions,
variables: { pendingCreations: massCreationVariables },
}),
).resolves.toEqual(
expect.objectContaining({
data: {
createPendingCreations: {
adminCreateContributions: {
success: true,
successfulCreation: ['bibi@bloxberg.de', 'peter@lustig.de'],
failedCreation: [
successfulContribution: ['bibi@bloxberg.de', 'peter@lustig.de'],
failedContribution: [
'stephen@hawking.uk',
'garrick@ollivander.com',
'bob@baumeister.de',
@ -929,7 +939,7 @@ describe('AdminResolver', () => {
})
})
describe('updatePendingCreation', () => {
describe('adminUpdateContribution', () => {
// at this I expect to have this data in DB:
// bibi@bloxberg.de: [1000, 1000, 300]
// peter@lustig.de: [1000, 600, 500]
@ -940,7 +950,7 @@ describe('AdminResolver', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: 1,
email: 'bob@baumeister.de',
@ -961,7 +971,7 @@ describe('AdminResolver', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: 1,
email: 'stephen@hawking.uk',
@ -982,7 +992,7 @@ describe('AdminResolver', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: -1,
email: 'bibi@bloxberg.de',
@ -993,7 +1003,7 @@ describe('AdminResolver', () => {
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('No creation found to given id.')],
errors: [new GraphQLError('No contribution found to given id.')],
}),
)
})
@ -1003,7 +1013,7 @@ describe('AdminResolver', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: creation ? creation.id : -1,
email: 'bibi@bloxberg.de',
@ -1016,7 +1026,7 @@ describe('AdminResolver', () => {
expect.objectContaining({
errors: [
new GraphQLError(
'user of the pending creation and send user does not correspond',
'user of the pending contribution and send user does not correspond',
),
],
}),
@ -1028,7 +1038,7 @@ describe('AdminResolver', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: creation ? creation.id : -1,
email: 'peter@lustig.de',
@ -1053,7 +1063,7 @@ describe('AdminResolver', () => {
it('returns update creation object', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: creation ? creation.id : -1,
email: 'peter@lustig.de',
@ -1065,7 +1075,7 @@ describe('AdminResolver', () => {
).resolves.toEqual(
expect.objectContaining({
data: {
updatePendingCreation: {
adminUpdateContribution: {
date: expect.any(String),
memo: 'Danke Peter!',
amount: '300',
@ -1081,7 +1091,7 @@ describe('AdminResolver', () => {
it('returns update creation object', async () => {
await expect(
mutate({
mutation: updatePendingCreation,
mutation: adminUpdateContribution,
variables: {
id: creation ? creation.id : -1,
email: 'peter@lustig.de',
@ -1093,7 +1103,7 @@ describe('AdminResolver', () => {
).resolves.toEqual(
expect.objectContaining({
data: {
updatePendingCreation: {
adminUpdateContribution: {
date: expect.any(String),
memo: 'Das war leider zu Viel!',
amount: '200',
@ -1106,16 +1116,16 @@ describe('AdminResolver', () => {
})
})
describe('getPendingCreations', () => {
describe('listUnconfirmedContributions', () => {
it('returns four pending creations', async () => {
await expect(
query({
query: getPendingCreations,
query: listUnconfirmedContributions,
}),
).resolves.toEqual(
expect.objectContaining({
data: {
getPendingCreations: expect.arrayContaining([
listUnconfirmedContributions: expect.arrayContaining([
{
id: expect.any(Number),
firstName: 'Peter',
@ -1167,19 +1177,19 @@ describe('AdminResolver', () => {
})
})
describe('deletePendingCreation', () => {
describe('adminDeleteContribution', () => {
describe('creation id does not exist', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
mutation: adminDeleteContribution,
variables: {
id: -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Creation not found for given id.')],
errors: [new GraphQLError('Contribution not found for given id.')],
}),
)
})
@ -1189,33 +1199,33 @@ describe('AdminResolver', () => {
it('returns true', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
mutation: adminDeleteContribution,
variables: {
id: creation ? creation.id : -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
data: { deletePendingCreation: true },
data: { adminDeleteContribution: true },
}),
)
})
})
})
describe('confirmPendingCreation', () => {
describe('confirmContribution', () => {
describe('creation does not exits', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Creation not found to given id.')],
errors: [new GraphQLError('Contribution not found to given id.')],
}),
)
})
@ -1235,14 +1245,14 @@ describe('AdminResolver', () => {
it('thows an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: creation ? creation.id : -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Moderator can not confirm own pending creation')],
errors: [new GraphQLError('Moderator can not confirm own contribution')],
}),
)
})
@ -1262,14 +1272,14 @@ describe('AdminResolver', () => {
it('returns true', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: creation ? creation.id : -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
data: { confirmPendingCreation: true },
data: { confirmContribution: true },
}),
)
})
@ -1287,8 +1297,8 @@ describe('AdminResolver', () => {
})
describe('confirm two creations one after the other quickly', () => {
let c1: AdminPendingCreation | void
let c2: AdminPendingCreation | void
let c1: Contribution | void
let c2: Contribution | void
beforeAll(async () => {
const now = new Date()
@ -1309,25 +1319,25 @@ describe('AdminResolver', () => {
// In the futrue this should not throw anymore
it('throws an error for the second confirmation', async () => {
const r1 = mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: c1 ? c1.id : -1,
},
})
const r2 = mutate({
mutation: confirmPendingCreation,
mutation: confirmContribution,
variables: {
id: c2 ? c2.id : -1,
},
})
await expect(r1).resolves.toEqual(
expect.objectContaining({
data: { confirmPendingCreation: true },
data: { confirmContribution: true },
}),
)
await expect(r2).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Unable to confirm creation.')],
errors: [new GraphQLError('Creation was not successful.')],
}),
)
})

View File

@ -12,15 +12,15 @@ import {
FindOperator,
} from '@dbTools/typeorm'
import { UserAdmin, SearchUsersResult } from '@model/UserAdmin'
import { PendingCreation } from '@model/PendingCreation'
import { CreatePendingCreations } from '@model/CreatePendingCreations'
import { UpdatePendingCreation } from '@model/UpdatePendingCreation'
import { UnconfirmedContribution } from '@model/UnconfirmedContribution'
import { AdminCreateContributions } from '@model/AdminCreateContributions'
import { AdminUpdateContribution } from '@model/AdminUpdateContribution'
import { ContributionLink } from '@model/ContributionLink'
import { ContributionLinkList } from '@model/ContributionLinkList'
import { RIGHTS } from '@/auth/RIGHTS'
import { UserRepository } from '@repository/User'
import CreatePendingCreationArgs from '@arg/CreatePendingCreationArgs'
import UpdatePendingCreationArgs from '@arg/UpdatePendingCreationArgs'
import AdminCreateContributionArgs from '@arg/AdminCreateContributionArgs'
import AdminUpdateContributionArgs from '@arg/AdminUpdateContributionArgs'
import SearchUsersArgs from '@arg/SearchUsersArgs'
import ContributionLinkArgs from '@arg/ContributionLinkArgs'
import { Transaction as DbTransaction } from '@entity/Transaction'
@ -30,7 +30,7 @@ import { TransactionLink, TransactionLinkResult } from '@model/TransactionLink'
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
import { TransactionRepository } from '@repository/Transaction'
import { calculateDecay } from '@/util/decay'
import { AdminPendingCreation } from '@entity/AdminPendingCreation'
import { Contribution } from '@entity/Contribution'
import { hasElopageBuys } from '@/util/hasElopageBuys'
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
import { User as dbUser } from '@entity/User'
@ -169,72 +169,76 @@ export class AdminResolver {
return null
}
@Authorized([RIGHTS.CREATE_PENDING_CREATION])
@Authorized([RIGHTS.ADMIN_CREATE_CONTRIBUTION])
@Mutation(() => [Number])
async createPendingCreation(
@Args() { email, amount, memo, creationDate }: CreatePendingCreationArgs,
async adminCreateContribution(
@Args() { email, amount, memo, creationDate }: AdminCreateContributionArgs,
@Ctx() context: Context,
): Promise<Decimal[]> {
logger.trace('adminCreateContribution...')
const user = await dbUser.findOne({ email }, { withDeleted: true })
if (!user) {
throw new Error(`Could not find user with email: ${email}`)
}
if (user.deletedAt) {
throw new Error('This user was deleted. Cannot make a creation.')
throw new Error('This user was deleted. Cannot create a contribution.')
}
if (!user.emailChecked) {
throw new Error('Creation could not be saved, Email is not activated')
throw new Error('Contribution could not be saved, Email is not activated')
}
const moderator = getUser(context)
logger.trace('moderator: ', moderator.id)
const creations = await getUserCreation(user.id)
logger.trace('creations', creations)
const creationDateObj = new Date(creationDate)
if (isCreationValid(creations, amount, creationDateObj)) {
const adminPendingCreation = AdminPendingCreation.create()
adminPendingCreation.userId = user.id
adminPendingCreation.amount = amount
adminPendingCreation.created = new Date()
adminPendingCreation.date = creationDateObj
adminPendingCreation.memo = memo
adminPendingCreation.moderator = moderator.id
if (isContributionValid(creations, amount, creationDateObj)) {
const contribution = Contribution.create()
contribution.userId = user.id
contribution.amount = amount
contribution.createdAt = new Date()
contribution.contributionDate = creationDateObj
contribution.memo = memo
contribution.moderatorId = moderator.id
await AdminPendingCreation.save(adminPendingCreation)
logger.trace('contribution to save', contribution)
await Contribution.save(contribution)
}
return getUserCreation(user.id)
}
@Authorized([RIGHTS.CREATE_PENDING_CREATION])
@Mutation(() => CreatePendingCreations)
async createPendingCreations(
@Arg('pendingCreations', () => [CreatePendingCreationArgs])
pendingCreations: CreatePendingCreationArgs[],
@Authorized([RIGHTS.ADMIN_CREATE_CONTRIBUTIONS])
@Mutation(() => AdminCreateContributions)
async adminCreateContributions(
@Arg('pendingCreations', () => [AdminCreateContributionArgs])
contributions: AdminCreateContributionArgs[],
@Ctx() context: Context,
): Promise<CreatePendingCreations> {
): Promise<AdminCreateContributions> {
let success = false
const successfulCreation: string[] = []
const failedCreation: string[] = []
for (const pendingCreation of pendingCreations) {
await this.createPendingCreation(pendingCreation, context)
const successfulContribution: string[] = []
const failedContribution: string[] = []
for (const contribution of contributions) {
await this.adminCreateContribution(contribution, context)
.then(() => {
successfulCreation.push(pendingCreation.email)
successfulContribution.push(contribution.email)
success = true
})
.catch(() => {
failedCreation.push(pendingCreation.email)
failedContribution.push(contribution.email)
})
}
return {
success,
successfulCreation,
failedCreation,
successfulContribution,
failedContribution,
}
}
@Authorized([RIGHTS.UPDATE_PENDING_CREATION])
@Mutation(() => UpdatePendingCreation)
async updatePendingCreation(
@Args() { id, email, amount, memo, creationDate }: UpdatePendingCreationArgs,
@Authorized([RIGHTS.ADMIN_UPDATE_CONTRIBUTION])
@Mutation(() => AdminUpdateContribution)
async adminUpdateContribution(
@Args() { id, email, amount, memo, creationDate }: AdminUpdateContributionArgs,
@Ctx() context: Context,
): Promise<UpdatePendingCreation> {
): Promise<AdminUpdateContribution> {
const user = await dbUser.findOne({ email }, { withDeleted: true })
if (!user) {
throw new Error(`Could not find user with email: ${email}`)
@ -245,59 +249,65 @@ export class AdminResolver {
const moderator = getUser(context)
const pendingCreationToUpdate = await AdminPendingCreation.findOne({ id })
const contributionToUpdate = await Contribution.findOne({
where: { id, confirmedAt: IsNull() },
})
if (!pendingCreationToUpdate) {
throw new Error('No creation found to given id.')
if (!contributionToUpdate) {
throw new Error('No contribution found to given id.')
}
if (pendingCreationToUpdate.userId !== user.id) {
throw new Error('user of the pending creation and send user does not correspond')
if (contributionToUpdate.userId !== user.id) {
throw new Error('user of the pending contribution and send user does not correspond')
}
const creationDateObj = new Date(creationDate)
let creations = await getUserCreation(user.id)
if (pendingCreationToUpdate.date.getMonth() === creationDateObj.getMonth()) {
creations = updateCreations(creations, pendingCreationToUpdate)
if (contributionToUpdate.contributionDate.getMonth() === creationDateObj.getMonth()) {
creations = updateCreations(creations, contributionToUpdate)
}
// all possible cases not to be true are thrown in this function
isCreationValid(creations, amount, creationDateObj)
pendingCreationToUpdate.amount = amount
pendingCreationToUpdate.memo = memo
pendingCreationToUpdate.date = new Date(creationDate)
pendingCreationToUpdate.moderator = moderator.id
isContributionValid(creations, amount, creationDateObj)
contributionToUpdate.amount = amount
contributionToUpdate.memo = memo
contributionToUpdate.contributionDate = new Date(creationDate)
contributionToUpdate.moderatorId = moderator.id
await AdminPendingCreation.save(pendingCreationToUpdate)
const result = new UpdatePendingCreation()
await Contribution.save(contributionToUpdate)
const result = new AdminUpdateContribution()
result.amount = amount
result.memo = pendingCreationToUpdate.memo
result.date = pendingCreationToUpdate.date
result.memo = contributionToUpdate.memo
result.date = contributionToUpdate.contributionDate
result.creation = await getUserCreation(user.id)
return result
}
@Authorized([RIGHTS.SEARCH_PENDING_CREATION])
@Query(() => [PendingCreation])
async getPendingCreations(): Promise<PendingCreation[]> {
const pendingCreations = await AdminPendingCreation.find()
if (pendingCreations.length === 0) {
@Authorized([RIGHTS.LIST_UNCONFIRMED_CONTRIBUTIONS])
@Query(() => [UnconfirmedContribution])
async listUnconfirmedContributions(): Promise<UnconfirmedContribution[]> {
const contributions = await Contribution.find({ where: { confirmedAt: IsNull() } })
if (contributions.length === 0) {
return []
}
const userIds = pendingCreations.map((p) => p.userId)
const userIds = contributions.map((p) => p.userId)
const userCreations = await getUserCreations(userIds)
const users = await dbUser.find({ where: { id: In(userIds) }, withDeleted: true })
return pendingCreations.map((pendingCreation) => {
const user = users.find((u) => u.id === pendingCreation.userId)
const creation = userCreations.find((c) => c.id === pendingCreation.userId)
return contributions.map((contribution) => {
const user = users.find((u) => u.id === contribution.userId)
const creation = userCreations.find((c) => c.id === contribution.userId)
return {
...pendingCreation,
amount: pendingCreation.amount,
id: contribution.id,
userId: contribution.userId,
date: contribution.contributionDate,
memo: contribution.memo,
amount: contribution.amount,
moderator: contribution.moderatorId,
firstName: user ? user.firstName : '',
lastName: user ? user.lastName : '',
email: user ? user.email : '',
@ -306,69 +316,93 @@ export class AdminResolver {
})
}
@Authorized([RIGHTS.DELETE_PENDING_CREATION])
@Authorized([RIGHTS.ADMIN_DELETE_CONTRIBUTION])
@Mutation(() => Boolean)
async deletePendingCreation(@Arg('id', () => Int) id: number): Promise<boolean> {
const pendingCreation = await AdminPendingCreation.findOne(id)
if (!pendingCreation) {
throw new Error('Creation not found for given id.')
async adminDeleteContribution(@Arg('id', () => Int) id: number): Promise<boolean> {
const contribution = await Contribution.findOne(id)
if (!contribution) {
throw new Error('Contribution not found for given id.')
}
const res = await AdminPendingCreation.delete(pendingCreation)
const res = await contribution.softRemove()
return !!res
}
@Authorized([RIGHTS.CONFIRM_PENDING_CREATION])
@Authorized([RIGHTS.CONFIRM_CONTRIBUTION])
@Mutation(() => Boolean)
async confirmPendingCreation(
async confirmContribution(
@Arg('id', () => Int) id: number,
@Ctx() context: Context,
): Promise<boolean> {
const pendingCreation = await AdminPendingCreation.findOne(id)
if (!pendingCreation) {
throw new Error('Creation not found to given id.')
const contribution = await Contribution.findOne(id)
if (!contribution) {
throw new Error('Contribution not found to given id.')
}
const moderatorUser = getUser(context)
if (moderatorUser.id === pendingCreation.userId)
throw new Error('Moderator can not confirm own pending creation')
if (moderatorUser.id === contribution.userId)
throw new Error('Moderator can not confirm own contribution')
const user = await dbUser.findOneOrFail({ id: pendingCreation.userId }, { withDeleted: true })
if (user.deletedAt) throw new Error('This user was deleted. Cannot confirm a creation.')
const user = await dbUser.findOneOrFail({ id: contribution.userId }, { withDeleted: true })
if (user.deletedAt) throw new Error('This user was deleted. Cannot confirm a contribution.')
const creations = await getUserCreation(pendingCreation.userId, false)
if (!isCreationValid(creations, pendingCreation.amount, pendingCreation.date)) {
const creations = await getUserCreation(contribution.userId, false)
if (!isContributionValid(creations, contribution.amount, contribution.contributionDate)) {
throw new Error('Creation is not valid!!')
}
const receivedCallDate = new Date()
const transactionRepository = getCustomRepository(TransactionRepository)
const lastTransaction = await transactionRepository.findLastForUser(pendingCreation.userId)
const queryRunner = getConnection().createQueryRunner()
await queryRunner.connect()
await queryRunner.startTransaction('READ UNCOMMITTED')
try {
const lastTransaction = await queryRunner.manager
.createQueryBuilder()
.select('transaction')
.from(DbTransaction, 'transaction')
.where('transaction.userId = :id', { id: contribution.userId })
.orderBy('transaction.balanceDate', 'DESC')
.getOne()
logger.info('lastTransaction ID', lastTransaction ? lastTransaction.id : 'undefined')
let newBalance = new Decimal(0)
let decay: Decay | null = null
if (lastTransaction) {
decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, receivedCallDate)
newBalance = decay.balance
let newBalance = new Decimal(0)
let decay: Decay | null = null
if (lastTransaction) {
decay = calculateDecay(
lastTransaction.balance,
lastTransaction.balanceDate,
receivedCallDate,
)
newBalance = decay.balance
}
newBalance = newBalance.add(contribution.amount.toString())
const transaction = new DbTransaction()
transaction.typeId = TransactionTypeId.CREATION
transaction.memo = contribution.memo
transaction.userId = contribution.userId
transaction.previous = lastTransaction ? lastTransaction.id : null
transaction.amount = contribution.amount
transaction.creationDate = contribution.contributionDate
transaction.balance = newBalance
transaction.balanceDate = receivedCallDate
transaction.decay = decay ? decay.decay : new Decimal(0)
transaction.decayStart = decay ? decay.start : null
await queryRunner.manager.insert(DbTransaction, transaction)
contribution.confirmedAt = receivedCallDate
contribution.confirmedBy = moderatorUser.id
contribution.transactionId = transaction.id
await queryRunner.manager.update(Contribution, { id: contribution.id }, contribution)
await queryRunner.commitTransaction()
logger.info('creation commited successfuly.')
} catch (e) {
await queryRunner.rollbackTransaction()
logger.error(`Creation was not successful: ${e}`)
throw new Error(`Creation was not successful.`)
} finally {
await queryRunner.release()
}
newBalance = newBalance.add(pendingCreation.amount.toString())
const transaction = new DbTransaction()
transaction.typeId = TransactionTypeId.CREATION
transaction.memo = pendingCreation.memo
transaction.userId = pendingCreation.userId
transaction.previous = lastTransaction ? lastTransaction.id : null
transaction.amount = pendingCreation.amount
transaction.creationDate = pendingCreation.date
transaction.balance = newBalance
transaction.balanceDate = receivedCallDate
transaction.decay = decay ? decay.decay : new Decimal(0)
transaction.decayStart = decay ? decay.start : null
await transaction.save().catch(() => {
throw new Error('Unable to confirm creation.')
})
await AdminPendingCreation.delete(pendingCreation)
return true
}
@ -567,24 +601,29 @@ interface CreationMap {
}
async function getUserCreation(id: number, includePending = true): Promise<Decimal[]> {
logger.trace('getUserCreation', id, includePending)
const creations = await getUserCreations([id], includePending)
return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE
}
async function getUserCreations(ids: number[], includePending = true): Promise<CreationMap[]> {
logger.trace('getUserCreations:', ids, includePending)
const months = getCreationMonths()
logger.trace('getUserCreations months', months)
const queryRunner = getConnection().createQueryRunner()
await queryRunner.connect()
const dateFilter = 'last_day(curdate() - interval 3 month) + interval 1 day'
logger.trace('getUserCreations dateFilter', dateFilter)
const unionString = includePending
? `
UNION
SELECT date AS date, amount AS amount, userId AS userId FROM admin_pending_creations
WHERE userId IN (${ids.toString()})
AND date >= ${dateFilter}`
SELECT contribution_date AS date, amount AS amount, user_id AS userId FROM contributions
WHERE user_id IN (${ids.toString()})
AND contribution_date >= ${dateFilter}
AND confirmed_at IS NULL AND deleted_at IS NULL`
: ''
const unionQuery = await queryRunner.manager.query(`
@ -614,17 +653,18 @@ async function getUserCreations(ids: number[], includePending = true): Promise<C
})
}
function updateCreations(creations: Decimal[], pendingCreation: AdminPendingCreation): Decimal[] {
const index = getCreationIndex(pendingCreation.date.getMonth())
function updateCreations(creations: Decimal[], contribution: Contribution): Decimal[] {
const index = getCreationIndex(contribution.contributionDate.getMonth())
if (index < 0) {
throw new Error('You cannot create GDD for a month older than the last three months.')
}
creations[index] = creations[index].plus(pendingCreation.amount.toString())
creations[index] = creations[index].plus(contribution.amount.toString())
return creations
}
function isCreationValid(creations: Decimal[], amount: Decimal, creationDate: Date) {
function isContributionValid(creations: Decimal[], amount: Decimal, creationDate: Date) {
logger.trace('isContributionValid', creations, amount, creationDate)
const index = getCreationIndex(creationDate.getMonth())
if (index < 0) {

View File

@ -1,13 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { createPendingCreation, confirmPendingCreation } from '@/seeds/graphql/mutations'
import { adminCreateContribution, confirmContribution } from '@/seeds/graphql/mutations'
import { login } from '@/seeds/graphql/queries'
import { CreationInterface } from '@/seeds/creation/CreationInterface'
import { ApolloServerTestClient } from 'apollo-server-testing'
import { User } from '@entity/User'
import { Transaction } from '@entity/Transaction'
import { AdminPendingCreation } from '@entity/AdminPendingCreation'
import { Contribution } from '@entity/Contribution'
// import CONFIG from '@/config/index'
export const nMonthsBefore = (date: Date, months = 1): string => {
@ -17,23 +17,23 @@ export const nMonthsBefore = (date: Date, months = 1): string => {
export const creationFactory = async (
client: ApolloServerTestClient,
creation: CreationInterface,
): Promise<AdminPendingCreation | void> => {
): Promise<Contribution | void> => {
const { mutate, query } = client
await query({ query: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } })
// TODO it would be nice to have this mutation return the id
await mutate({ mutation: createPendingCreation, variables: { ...creation } })
await mutate({ mutation: adminCreateContribution, variables: { ...creation } })
const user = await User.findOneOrFail({ where: { email: creation.email } })
const pendingCreation = await AdminPendingCreation.findOneOrFail({
const pendingCreation = await Contribution.findOneOrFail({
where: { userId: user.id, amount: creation.amount },
order: { created: 'DESC' },
order: { createdAt: 'DESC' },
})
if (creation.confirmed) {
await mutate({ mutation: confirmPendingCreation, variables: { id: pendingCreation.id } })
await mutate({ mutation: confirmContribution, variables: { id: pendingCreation.id } })
if (creation.moveCreationDate) {
const transaction = await Transaction.findOneOrFail({

View File

@ -81,15 +81,20 @@ export const createTransactionLink = gql`
// from admin interface
export const createPendingCreation = gql`
export const adminCreateContribution = gql`
mutation ($email: String!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
createPendingCreation(email: $email, amount: $amount, memo: $memo, creationDate: $creationDate)
adminCreateContribution(
email: $email
amount: $amount
memo: $memo
creationDate: $creationDate
)
}
`
export const confirmPendingCreation = gql`
export const confirmContribution = gql`
mutation ($id: Int!) {
confirmPendingCreation(id: $id)
confirmContribution(id: $id)
}
`
@ -105,19 +110,19 @@ export const unDeleteUser = gql`
}
`
export const createPendingCreations = gql`
mutation ($pendingCreations: [CreatePendingCreationArgs!]!) {
createPendingCreations(pendingCreations: $pendingCreations) {
export const adminCreateContributions = gql`
mutation ($pendingCreations: [AdminCreateContributionArgs!]!) {
adminCreateContributions(pendingCreations: $pendingCreations) {
success
successfulCreation
failedCreation
successfulContribution
failedContribution
}
}
`
export const updatePendingCreation = gql`
export const adminUpdateContribution = gql`
mutation ($id: Int!, $email: String!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
updatePendingCreation(
adminUpdateContribution(
id: $id
email: $email
amount: $amount
@ -132,9 +137,9 @@ export const updatePendingCreation = gql`
}
`
export const deletePendingCreation = gql`
export const adminDeleteContribution = gql`
mutation ($id: Int!) {
deletePendingCreation(id: $id)
adminDeleteContribution(id: $id)
}
`

View File

@ -173,9 +173,9 @@ export const queryTransactionLink = gql`
// from admin interface
export const getPendingCreations = gql`
export const listUnconfirmedContributions = gql`
query {
getPendingCreations {
listUnconfirmedContributions {
id
firstName
lastName

View File

@ -0,0 +1,48 @@
import Decimal from 'decimal.js-light'
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn, DeleteDateColumn } from 'typeorm'
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
@Entity('contributions')
export class Contribution extends BaseEntity {
@PrimaryGeneratedColumn('increment', { unsigned: true })
id: number
@Column({ unsigned: true, nullable: false, name: 'user_id' })
userId: number
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP', name: 'created_at' })
createdAt: Date
@Column({ type: 'datetime', nullable: false, name: 'contribution_date' })
contributionDate: Date
@Column({ length: 255, nullable: false, collation: 'utf8mb4_unicode_ci' })
memo: string
@Column({
type: 'decimal',
precision: 40,
scale: 20,
nullable: false,
transformer: DecimalTransformer,
})
amount: Decimal
@Column({ unsigned: true, nullable: true, name: 'moderator_id' })
moderatorId: number
@Column({ unsigned: true, nullable: true, name: 'contribution_link_id' })
contributionLinkId: number
@Column({ unsigned: true, nullable: true, name: 'confirmed_by' })
confirmedBy: number
@Column({ nullable: true, name: 'confirmed_at' })
confirmedAt: Date
@Column({ unsigned: true, nullable: true, name: 'transaction_id' })
transactionId: number
@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date | null
}

View File

@ -0,0 +1 @@
export { Contribution } from './0039-contributions_table/Contribution'

View File

@ -5,10 +5,10 @@ import { Migration } from './Migration'
import { Transaction } from './Transaction'
import { TransactionLink } from './TransactionLink'
import { User } from './User'
import { AdminPendingCreation } from './AdminPendingCreation'
import { Contribution } from './Contribution'
export const entities = [
AdminPendingCreation,
Contribution,
ContributionLink,
LoginElopageBuys,
LoginEmailOptIn,

View File

@ -0,0 +1,59 @@
/* MIGRATION to rename ADMIN_PENDING_CREATION table and add columns
*/
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
await queryFn('RENAME TABLE `admin_pending_creations` TO `contributions`;')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `userId` `user_id` int(10);')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `created` `created_at` datetime;')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `date` `contribution_date` datetime;')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `moderator` `moderator_id` int(10);')
await queryFn(
'ALTER TABLE `contributions` ADD COLUMN `contribution_link_id` int(10) unsigned DEFAULT NULL AFTER `moderator_id`;',
)
await queryFn(
'ALTER TABLE `contributions` ADD COLUMN `confirmed_by` int(10) unsigned DEFAULT NULL AFTER `contribution_link_id`;',
)
await queryFn(
'ALTER TABLE `contributions` ADD COLUMN `confirmed_at` datetime DEFAULT NULL AFTER `confirmed_by`;',
)
await queryFn(
'ALTER TABLE `contributions` ADD COLUMN `transaction_id` int(10) unsigned DEFAULT NULL AFTER `confirmed_at`;',
)
await queryFn(
'ALTER TABLE `contributions` ADD COLUMN `deleted_at` datetime DEFAULT NULL AFTER `confirmed_at`;',
)
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
await queryFn('ALTER TABLE `contributions` DROP COLUMN IF EXISTS `deleted_at`;')
await queryFn('ALTER TABLE `contributions` DROP COLUMN IF EXISTS `transaction_id`;')
await queryFn('ALTER TABLE `contributions` DROP COLUMN IF EXISTS `confirmed_at`;')
await queryFn('ALTER TABLE `contributions` DROP COLUMN IF EXISTS `confirmed_by`;')
await queryFn('ALTER TABLE `contributions` DROP COLUMN IF EXISTS `contribution_link_id`;')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `moderator_id` `moderator` int(10);')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `created_at` `created` datetime;')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `contribution_date` `date` datetime;')
await queryFn('ALTER TABLE `contributions` CHANGE COLUMN `user_id` `userId` int(10);')
await queryFn('RENAME TABLE `contributions` TO `admin_pending_creations`;')
}