mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #2811 from gradido/2803-open-contribution-edit-button
fix(admin): admin open contribution edit button
This commit is contained in:
commit
042f13fce7
@ -2,14 +2,18 @@ import { mount } from '@vue/test-utils'
|
||||
import CreationFormular from './CreationFormular'
|
||||
import { adminCreateContribution } from '../graphql/adminCreateContribution'
|
||||
import { toastErrorSpy, toastSuccessSpy } from '../../test/testSetup'
|
||||
import VueApollo from 'vue-apollo'
|
||||
import { createMockClient } from 'mock-apollo-client'
|
||||
import { adminOpenCreations } from '../graphql/adminOpenCreations'
|
||||
|
||||
const mockClient = createMockClient()
|
||||
const apolloProvider = new VueApollo({
|
||||
defaultClient: mockClient,
|
||||
})
|
||||
|
||||
const localVue = global.localVue
|
||||
localVue.use(VueApollo)
|
||||
|
||||
const apolloMutateMock = jest.fn().mockResolvedValue({
|
||||
data: {
|
||||
adminCreateContribution: [0, 0, 0],
|
||||
},
|
||||
})
|
||||
const stateCommitMock = jest.fn()
|
||||
|
||||
const mocks = {
|
||||
@ -18,9 +22,6 @@ const mocks = {
|
||||
const date = new Date(d)
|
||||
return date.toISOString().split('T')[0]
|
||||
}),
|
||||
$apollo: {
|
||||
mutate: apolloMutateMock,
|
||||
},
|
||||
$store: {
|
||||
commit: stateCommitMock,
|
||||
},
|
||||
@ -31,7 +32,8 @@ const propsData = {
|
||||
creation: [],
|
||||
}
|
||||
|
||||
const now = new Date(Date.now())
|
||||
const now = new Date()
|
||||
|
||||
const getCreationDate = (sub) => {
|
||||
const date = sub === 0 ? now : new Date(now.getFullYear(), now.getMonth() - sub, 1, 0)
|
||||
return date.toISOString().split('T')[0]
|
||||
@ -40,8 +42,43 @@ const getCreationDate = (sub) => {
|
||||
describe('CreationFormular', () => {
|
||||
let wrapper
|
||||
|
||||
const adminOpenCreationsMock = jest.fn()
|
||||
const adminCreateContributionMock = jest.fn()
|
||||
mockClient.setRequestHandler(
|
||||
adminOpenCreations,
|
||||
adminOpenCreationsMock.mockResolvedValue({
|
||||
data: {
|
||||
adminOpenCreations: [
|
||||
{
|
||||
month: new Date(now.getFullYear(), now.getMonth() - 2).getMonth(),
|
||||
year: new Date(now.getFullYear(), now.getMonth() - 2).getFullYear(),
|
||||
amount: '200',
|
||||
},
|
||||
{
|
||||
month: new Date(now.getFullYear(), now.getMonth() - 1).getMonth(),
|
||||
year: new Date(now.getFullYear(), now.getMonth() - 1).getFullYear(),
|
||||
amount: '400',
|
||||
},
|
||||
{
|
||||
month: now.getMonth(),
|
||||
year: now.getFullYear(),
|
||||
amount: '600',
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
)
|
||||
mockClient.setRequestHandler(
|
||||
adminCreateContribution,
|
||||
adminCreateContributionMock.mockResolvedValue({
|
||||
data: {
|
||||
adminCreateContribution: [0, 0, 0],
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
const Wrapper = () => {
|
||||
return mount(CreationFormular, { localVue, mocks, propsData })
|
||||
return mount(CreationFormular, { localVue, mocks, propsData, apolloProvider })
|
||||
}
|
||||
|
||||
describe('mount', () => {
|
||||
@ -107,17 +144,12 @@ describe('CreationFormular', () => {
|
||||
})
|
||||
|
||||
it('sends ... to apollo', () => {
|
||||
expect(apolloMutateMock).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
mutation: adminCreateContribution,
|
||||
variables: {
|
||||
email: 'benjamin@bluemchen.de',
|
||||
creationDate: getCreationDate(2),
|
||||
amount: 90,
|
||||
memo: 'Test create coins',
|
||||
},
|
||||
}),
|
||||
)
|
||||
expect(adminCreateContributionMock).toBeCalledWith({
|
||||
email: 'benjamin@bluemchen.de',
|
||||
creationDate: getCreationDate(2),
|
||||
amount: 90,
|
||||
memo: 'Test create coins',
|
||||
})
|
||||
})
|
||||
|
||||
it('emits update-user-data', () => {
|
||||
@ -144,7 +176,7 @@ describe('CreationFormular', () => {
|
||||
|
||||
describe('sendForm with server error', () => {
|
||||
beforeEach(async () => {
|
||||
apolloMutateMock.mockRejectedValueOnce({ message: 'Ouch!' })
|
||||
adminCreateContributionMock.mockRejectedValueOnce({ message: 'Ouch!' })
|
||||
await wrapper.find('.test-submit').trigger('click')
|
||||
})
|
||||
|
||||
@ -212,7 +244,7 @@ describe('CreationFormular', () => {
|
||||
})
|
||||
|
||||
it('sends ... to apollo', () => {
|
||||
expect(apolloMutateMock).toBeCalled()
|
||||
expect(adminCreateContributionMock).toBeCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@ -275,7 +307,7 @@ describe('CreationFormular', () => {
|
||||
})
|
||||
|
||||
it('sends mutation to apollo', () => {
|
||||
expect(apolloMutateMock).toBeCalled()
|
||||
expect(adminCreateContributionMock).toBeCalled()
|
||||
})
|
||||
|
||||
it('toast success message', () => {
|
||||
|
||||
@ -117,10 +117,6 @@ export default {
|
||||
return {}
|
||||
},
|
||||
},
|
||||
creation: {
|
||||
type: Array,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
@ -129,6 +125,7 @@ export default {
|
||||
rangeMin: 0,
|
||||
rangeMax: 1000,
|
||||
selected: '',
|
||||
userId: this.item.userId,
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
@ -136,7 +133,7 @@ export default {
|
||||
// do we want to reset the memo everytime the month changes?
|
||||
this.text = this.$t('creation_form.creation_for') + ' ' + name.short + ' ' + name.year
|
||||
this.rangeMin = 0
|
||||
this.rangeMax = name.creation
|
||||
this.rangeMax = Number(name.creation)
|
||||
},
|
||||
submitCreation() {
|
||||
this.$apollo
|
||||
@ -167,6 +164,10 @@ export default {
|
||||
this.$refs.creationForm.reset()
|
||||
this.value = 0
|
||||
})
|
||||
.finally(() => {
|
||||
this.$apollo.queries.OpenCreations.refetch()
|
||||
this.selected = ''
|
||||
})
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
|
||||
@ -1,19 +1,18 @@
|
||||
import { mount } from '@vue/test-utils'
|
||||
import EditCreationFormular from './EditCreationFormular'
|
||||
import { toastErrorSpy, toastSuccessSpy } from '../../test/testSetup'
|
||||
import VueApollo from 'vue-apollo'
|
||||
import { createMockClient } from 'mock-apollo-client'
|
||||
import { adminOpenCreations } from '../graphql/adminOpenCreations'
|
||||
import { adminUpdateContribution } from '../graphql/adminUpdateContribution'
|
||||
|
||||
const mockClient = createMockClient()
|
||||
const apolloProvider = new VueApollo({
|
||||
defaultClient: mockClient,
|
||||
})
|
||||
|
||||
const localVue = global.localVue
|
||||
|
||||
const apolloMutateMock = jest.fn().mockResolvedValue({
|
||||
data: {
|
||||
adminUpdateContribution: {
|
||||
creation: [0, 0, 0],
|
||||
amount: 500,
|
||||
date: new Date(),
|
||||
memo: 'Test Schöpfung 2',
|
||||
},
|
||||
},
|
||||
})
|
||||
localVue.use(VueApollo)
|
||||
|
||||
const stateCommitMock = jest.fn()
|
||||
|
||||
@ -23,22 +22,18 @@ const mocks = {
|
||||
const date = new Date(d)
|
||||
return date.toISOString().split('T')[0]
|
||||
}),
|
||||
$apollo: {
|
||||
mutate: apolloMutateMock,
|
||||
},
|
||||
$store: {
|
||||
commit: stateCommitMock,
|
||||
},
|
||||
}
|
||||
|
||||
const now = new Date(Date.now())
|
||||
const now = new Date()
|
||||
const getCreationDate = (sub) => {
|
||||
const date = sub === 0 ? now : new Date(now.getFullYear(), now.getMonth() - sub, 1, 0)
|
||||
return date.toISOString().split('T')[0]
|
||||
}
|
||||
|
||||
const propsData = {
|
||||
creation: [200, 400, 600],
|
||||
creationUserData: {
|
||||
memo: 'Test schöpfung 1',
|
||||
amount: 100,
|
||||
@ -46,20 +41,65 @@ const propsData = {
|
||||
},
|
||||
item: {
|
||||
id: 0,
|
||||
email: 'bob@baumeister.de',
|
||||
amount: '300',
|
||||
contributionDate: `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`,
|
||||
},
|
||||
}
|
||||
|
||||
const data = () => {
|
||||
return { creation: ['1000', '1000', '400'] }
|
||||
}
|
||||
|
||||
describe('EditCreationFormular', () => {
|
||||
let wrapper
|
||||
|
||||
const adminUpdateContributionMock = jest.fn()
|
||||
const adminOpenCreationsMock = jest.fn()
|
||||
mockClient.setRequestHandler(
|
||||
adminOpenCreations,
|
||||
adminOpenCreationsMock.mockResolvedValue({
|
||||
data: {
|
||||
adminOpenCreations: [
|
||||
{
|
||||
month: new Date(now.getFullYear(), now.getMonth() - 2).getMonth(),
|
||||
year: new Date(now.getFullYear(), now.getMonth() - 2).getFullYear(),
|
||||
amount: '1000',
|
||||
},
|
||||
{
|
||||
month: new Date(now.getFullYear(), now.getMonth() - 1).getMonth(),
|
||||
year: new Date(now.getFullYear(), now.getMonth() - 1).getFullYear(),
|
||||
amount: '1000',
|
||||
},
|
||||
{
|
||||
month: now.getMonth(),
|
||||
year: now.getFullYear(),
|
||||
amount: '400',
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
)
|
||||
mockClient.setRequestHandler(
|
||||
adminUpdateContribution,
|
||||
adminUpdateContributionMock.mockResolvedValue({
|
||||
data: {
|
||||
adminUpdateContribution: {
|
||||
amount: '600',
|
||||
date: new Date(),
|
||||
memo: 'This is my memo',
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
const Wrapper = () => {
|
||||
return mount(EditCreationFormular, { localVue, mocks, propsData })
|
||||
return mount(EditCreationFormular, { localVue, mocks, propsData, data, apolloProvider })
|
||||
}
|
||||
|
||||
describe('mount', () => {
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
wrapper = Wrapper()
|
||||
await wrapper.vm.$nextTick()
|
||||
})
|
||||
|
||||
it('has a DIV element with the class.component-edit-creation-formular', () => {
|
||||
@ -89,42 +129,16 @@ describe('EditCreationFormular', () => {
|
||||
})
|
||||
|
||||
it('calls the API', () => {
|
||||
expect(apolloMutateMock).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
variables: {
|
||||
id: 0,
|
||||
email: 'bob@baumeister.de',
|
||||
creationDate: getCreationDate(0),
|
||||
amount: 500,
|
||||
memo: 'Test Schöpfung 2',
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('emits update-user-data', () => {
|
||||
expect(wrapper.emitted('update-user-data')).toEqual([
|
||||
[
|
||||
{
|
||||
id: 0,
|
||||
email: 'bob@baumeister.de',
|
||||
},
|
||||
[0, 0, 0],
|
||||
],
|
||||
])
|
||||
expect(adminUpdateContributionMock).toBeCalledWith({
|
||||
id: 0,
|
||||
creationDate: getCreationDate(0),
|
||||
amount: 500,
|
||||
memo: 'Test Schöpfung 2',
|
||||
})
|
||||
})
|
||||
|
||||
it('emits update-creation-data', () => {
|
||||
expect(wrapper.emitted('update-creation-data')).toEqual([
|
||||
[
|
||||
{
|
||||
amount: 500,
|
||||
date: expect.any(Date),
|
||||
memo: 'Test Schöpfung 2',
|
||||
row: expect.any(Object),
|
||||
},
|
||||
],
|
||||
])
|
||||
expect(wrapper.emitted('update-creation-data')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('toasts a success message', () => {
|
||||
@ -134,7 +148,7 @@ describe('EditCreationFormular', () => {
|
||||
|
||||
describe('change and save memo and value with error', () => {
|
||||
beforeEach(async () => {
|
||||
apolloMutateMock.mockRejectedValue({ message: 'Oh no!' })
|
||||
adminUpdateContributionMock.mockRejectedValue({ message: 'Oh no!' })
|
||||
await wrapper.find('input[type="number"]').setValue(500)
|
||||
await wrapper.find('textarea').setValue('Test Schöpfung 2')
|
||||
await wrapper.find('.test-submit').trigger('click')
|
||||
|
||||
@ -96,18 +96,14 @@ export default {
|
||||
type: Object,
|
||||
required: true,
|
||||
},
|
||||
creation: {
|
||||
type: Array,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
text: !this.creationUserData.memo ? '' : this.creationUserData.memo,
|
||||
value: !this.creationUserData.amount ? 0 : Number(this.creationUserData.amount),
|
||||
rangeMin: 0,
|
||||
rangeMax: 1000,
|
||||
selected: '',
|
||||
selected: this.selectedComputed,
|
||||
userId: this.item.userId,
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
@ -117,20 +113,13 @@ export default {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: this.item.id,
|
||||
email: this.item.email,
|
||||
creationDate: this.selected.date,
|
||||
amount: Number(this.value),
|
||||
memo: this.text,
|
||||
},
|
||||
})
|
||||
.then((result) => {
|
||||
this.$emit('update-user-data', this.item, result.data.adminUpdateContribution.creation)
|
||||
this.$emit('update-creation-data', {
|
||||
amount: Number(result.data.adminUpdateContribution.amount),
|
||||
date: result.data.adminUpdateContribution.date,
|
||||
memo: result.data.adminUpdateContribution.memo,
|
||||
row: this.row,
|
||||
})
|
||||
this.$emit('update-creation-data')
|
||||
this.toastSuccess(
|
||||
this.$t('creation_form.toasted_update', {
|
||||
value: this.value,
|
||||
@ -149,15 +138,29 @@ export default {
|
||||
// Den geschöpften Wert auf o setzen
|
||||
this.value = 0
|
||||
})
|
||||
.finally(() => {
|
||||
this.$apollo.queries.OpenCreations.refetch()
|
||||
})
|
||||
},
|
||||
},
|
||||
created() {
|
||||
if (this.creationUserData.date) {
|
||||
const month = this.$d(new Date(this.creationUserData.date), 'month')
|
||||
const index = this.radioOptions.findIndex((obj) => obj.item.short === month)
|
||||
this.selected = this.radioOptions[index].item
|
||||
this.rangeMax = Number(this.creation[index]) + Number(this.creationUserData.amount)
|
||||
}
|
||||
computed: {
|
||||
creationIndex() {
|
||||
const month = this.$d(new Date(this.item.contributionDate), 'month')
|
||||
return this.radioOptions.findIndex((obj) => {
|
||||
return obj.item.short === month
|
||||
})
|
||||
},
|
||||
selectedComputed() {
|
||||
return this.radioOptions[this.creationIndex].item
|
||||
},
|
||||
rangeMax() {
|
||||
return Number(this.creation[this.creationIndex]) + Number(this.item.amount)
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
selectedComputed() {
|
||||
this.selected = this.selectedComputed
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
@ -5,7 +5,6 @@ const localVue = global.localVue
|
||||
|
||||
const apolloMutateMock = jest.fn().mockResolvedValue({})
|
||||
const apolloQueryMock = jest.fn().mockResolvedValue({})
|
||||
const toggleDetailsMock = jest.fn()
|
||||
|
||||
const propsData = {
|
||||
items: [
|
||||
@ -17,7 +16,7 @@ const propsData = {
|
||||
amount: 300,
|
||||
memo: 'Aktives Grundeinkommen für Januar 2022',
|
||||
date: '2022-01-01T00:00:00.000Z',
|
||||
moderator: 1,
|
||||
moderatorId: 1,
|
||||
creation: [700, 1000, 1000],
|
||||
__typename: 'PendingCreation',
|
||||
},
|
||||
@ -29,7 +28,7 @@ const propsData = {
|
||||
amount: 210,
|
||||
memo: 'Aktives Grundeinkommen für Januar 2022',
|
||||
date: '2022-01-01T00:00:00.000Z',
|
||||
moderator: null,
|
||||
moderatorId: null,
|
||||
creation: [790, 1000, 1000],
|
||||
__typename: 'PendingCreation',
|
||||
},
|
||||
@ -41,7 +40,7 @@ const propsData = {
|
||||
amount: 330,
|
||||
memo: 'Aktives Grundeinkommen für Januar 2022',
|
||||
date: '2022-01-01T00:00:00.000Z',
|
||||
moderator: 1,
|
||||
moderatorId: 1,
|
||||
creation: [670, 1000, 1000],
|
||||
__typename: 'PendingCreation',
|
||||
},
|
||||
@ -83,7 +82,7 @@ const mocks = {
|
||||
$store: {
|
||||
state: {
|
||||
moderator: {
|
||||
id: 0,
|
||||
id: 1,
|
||||
name: 'test moderator',
|
||||
},
|
||||
},
|
||||
@ -132,14 +131,6 @@ describe('OpenCreationsTable', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('call updateUserData', () => {
|
||||
it('user creations has updated data', async () => {
|
||||
wrapper.vm.updateUserData(propsData.items[0], [444, 555, 666])
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.vm.items[0].creation).toEqual([444, 555, 666])
|
||||
})
|
||||
})
|
||||
|
||||
describe('call updateState', () => {
|
||||
beforeEach(() => {
|
||||
wrapper.vm.updateState(4)
|
||||
@ -149,40 +140,5 @@ describe('OpenCreationsTable', () => {
|
||||
expect(wrapper.vm.$root.$emit('update-state', 4)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('call updateCreationData', () => {
|
||||
const date = new Date()
|
||||
beforeEach(() => {
|
||||
wrapper.vm.updateCreationData({
|
||||
amount: Number(80.0),
|
||||
date: date,
|
||||
memo: 'Test memo',
|
||||
row: {
|
||||
item: {},
|
||||
detailsShowing: false,
|
||||
toggleDetails: toggleDetailsMock,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('emits update-state', () => {
|
||||
expect(
|
||||
wrapper.vm.$emit('update-contributions', {
|
||||
amount: Number(80.0),
|
||||
date: date,
|
||||
memo: 'Test memo',
|
||||
row: {
|
||||
item: {},
|
||||
detailsShowing: false,
|
||||
toggleDetails: toggleDetailsMock,
|
||||
},
|
||||
}),
|
||||
).toBeTruthy()
|
||||
})
|
||||
|
||||
it('calls toggleDetails', () => {
|
||||
expect(toggleDetailsMock).toBeCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -27,9 +27,10 @@
|
||||
<template #cell(editCreation)="row">
|
||||
<div v-if="!myself(row.item)">
|
||||
<b-button
|
||||
v-if="row.item.moderator"
|
||||
v-if="row.item.moderatorId"
|
||||
variant="info"
|
||||
size="md"
|
||||
:index="0"
|
||||
@click="rowToggleDetails(row, 0)"
|
||||
class="mr-2"
|
||||
>
|
||||
@ -89,14 +90,13 @@
|
||||
@row-toggle-details="rowToggleDetails(row, 0)"
|
||||
>
|
||||
<template #show-creation>
|
||||
<div v-if="row.item.moderator">
|
||||
<div v-if="row.item.moderatorId">
|
||||
<edit-creation-formular
|
||||
type="singleCreation"
|
||||
:creation="row.item.creation"
|
||||
:item="row.item"
|
||||
:row="row"
|
||||
:creationUserData="creationUserData"
|
||||
@update-creation-data="updateCreationData"
|
||||
@update-creation-data="$emit('update-contributions')"
|
||||
/>
|
||||
</div>
|
||||
<div v-else>
|
||||
@ -104,7 +104,6 @@
|
||||
:contributionId="row.item.id"
|
||||
:contributionState="row.item.state"
|
||||
@update-state="updateState"
|
||||
@update-user-data="updateUserData"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
@ -146,22 +145,9 @@ export default {
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
creationUserData: {
|
||||
amount: null,
|
||||
date: null,
|
||||
memo: null,
|
||||
moderator: null,
|
||||
},
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
myself(item) {
|
||||
return (
|
||||
`${item.firstName} ${item.lastName}` ===
|
||||
`${this.$store.state.moderator.firstName} ${this.$store.state.moderator.lastName}`
|
||||
)
|
||||
return item.userId === this.$store.state.moderator.id
|
||||
},
|
||||
getStatusIcon(status) {
|
||||
return iconMap[status] ? iconMap[status] : 'default-icon'
|
||||
@ -174,16 +160,6 @@ export default {
|
||||
if (item.state === 'IN_PROGRESS') return 'table-primary'
|
||||
if (item.state === 'PENDING') return 'table-primary'
|
||||
},
|
||||
updateCreationData(data) {
|
||||
const row = data.row
|
||||
this.$emit('update-contributions', data)
|
||||
delete data.row
|
||||
this.creationUserData = { ...this.creationUserData, ...data }
|
||||
row.toggleDetails()
|
||||
},
|
||||
updateUserData(rowItem, newCreation) {
|
||||
rowItem.creation = newCreation
|
||||
},
|
||||
updateState(id) {
|
||||
this.$emit('update-state', id)
|
||||
},
|
||||
|
||||
@ -32,6 +32,8 @@ export const adminListContributions = gql`
|
||||
deniedBy
|
||||
deletedAt
|
||||
deletedBy
|
||||
moderatorId
|
||||
userId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
admin/src/graphql/adminOpenCreations.js
Normal file
11
admin/src/graphql/adminOpenCreations.js
Normal file
@ -0,0 +1,11 @@
|
||||
import gql from 'graphql-tag'
|
||||
|
||||
export const adminOpenCreations = gql`
|
||||
query ($userId: Int!) {
|
||||
adminOpenCreations(userId: $userId) {
|
||||
year
|
||||
month
|
||||
amount
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -1,18 +1,11 @@
|
||||
import gql from 'graphql-tag'
|
||||
|
||||
export const adminUpdateContribution = gql`
|
||||
mutation ($id: Int!, $email: String!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
|
||||
adminUpdateContribution(
|
||||
id: $id
|
||||
email: $email
|
||||
amount: $amount
|
||||
memo: $memo
|
||||
creationDate: $creationDate
|
||||
) {
|
||||
mutation ($id: Int!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
|
||||
adminUpdateContribution(id: $id, amount: $amount, memo: $memo, creationDate: $creationDate) {
|
||||
amount
|
||||
date
|
||||
memo
|
||||
creation
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
import { adminOpenCreations } from '../graphql/adminOpenCreations'
|
||||
|
||||
export const creationMonths = {
|
||||
props: {
|
||||
creation: {
|
||||
type: Array,
|
||||
default: () => [1000, 1000, 1000],
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
creation: [1000, 1000, 1000],
|
||||
userId: 0,
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
creationDates() {
|
||||
@ -38,4 +40,23 @@ export const creationMonths = {
|
||||
return this.creationDates.map((date) => this.$d(date, 'monthShort')).join(' | ')
|
||||
},
|
||||
},
|
||||
apollo: {
|
||||
OpenCreations: {
|
||||
query() {
|
||||
return adminOpenCreations
|
||||
},
|
||||
variables() {
|
||||
return {
|
||||
userId: this.userId,
|
||||
}
|
||||
},
|
||||
fetchPolicy: 'no-cache',
|
||||
update({ adminOpenCreations }) {
|
||||
this.creation = adminOpenCreations.map((obj) => obj.amount)
|
||||
},
|
||||
error({ message }) {
|
||||
this.toastError(message)
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
:fields="fields"
|
||||
@show-overlay="showOverlay"
|
||||
@update-state="updateStatus"
|
||||
@update-contributions="$apollo.queries.AllContributions.refetch()"
|
||||
@update-contributions="$apollo.queries.ListAllContributions.refetch()"
|
||||
/>
|
||||
|
||||
<b-pagination
|
||||
@ -212,7 +212,7 @@ export default {
|
||||
return this.formatDateOrDash(value)
|
||||
},
|
||||
},
|
||||
{ key: 'moderator', label: this.$t('moderator') },
|
||||
{ key: 'moderatorId', label: this.$t('moderator') },
|
||||
{ key: 'editCreation', label: this.$t('chat') },
|
||||
{ key: 'confirm', label: this.$t('save') },
|
||||
],
|
||||
|
||||
@ -42,7 +42,7 @@ const defaultData = () => {
|
||||
amount: 500,
|
||||
memo: 'Danke für alles',
|
||||
date: new Date(),
|
||||
moderator: 1,
|
||||
moderatorId: 1,
|
||||
state: 'PENDING',
|
||||
creation: [500, 500, 500],
|
||||
messagesCount: 0,
|
||||
@ -64,7 +64,7 @@ const defaultData = () => {
|
||||
amount: 1000000,
|
||||
memo: 'Gut Ergattert',
|
||||
date: new Date(),
|
||||
moderator: 1,
|
||||
moderatorId: 1,
|
||||
state: 'PENDING',
|
||||
creation: [500, 500, 500],
|
||||
messagesCount: 0,
|
||||
|
||||
@ -6,9 +6,6 @@ export default class AdminUpdateContributionArgs {
|
||||
@Field(() => Int)
|
||||
id: number
|
||||
|
||||
@Field(() => String)
|
||||
email: string
|
||||
|
||||
@Field(() => Decimal)
|
||||
amount: Decimal
|
||||
|
||||
|
||||
@ -21,6 +21,8 @@ export class Contribution {
|
||||
this.deniedBy = contribution.deniedBy
|
||||
this.deletedAt = contribution.deletedAt
|
||||
this.deletedBy = contribution.deletedBy
|
||||
this.moderatorId = contribution.moderatorId
|
||||
this.userId = contribution.userId
|
||||
}
|
||||
|
||||
@Field(() => Int)
|
||||
@ -67,6 +69,12 @@ export class Contribution {
|
||||
|
||||
@Field(() => String)
|
||||
state: string
|
||||
|
||||
@Field(() => Int, { nullable: true })
|
||||
moderatorId: number | null
|
||||
|
||||
@Field(() => Int, { nullable: true })
|
||||
userId: number | null
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
|
||||
@ -437,7 +437,6 @@ describe('ContributionResolver', () => {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: pendingContribution.data.createContribution.id,
|
||||
email: 'bibi@bloxberg.de',
|
||||
amount: 10.0,
|
||||
memo: 'Test env contribution',
|
||||
creationDate: new Date().toString(),
|
||||
@ -1672,7 +1671,6 @@ describe('ContributionResolver', () => {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: 1,
|
||||
email: 'bibi@bloxberg.de',
|
||||
amount: new Decimal(300),
|
||||
memo: 'Danke Bibi!',
|
||||
creationDate: contributionDateFormatter(new Date()),
|
||||
@ -1751,7 +1749,6 @@ describe('ContributionResolver', () => {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: 1,
|
||||
email: 'bibi@bloxberg.de',
|
||||
amount: new Decimal(300),
|
||||
memo: 'Danke Bibi!',
|
||||
creationDate: contributionDateFormatter(new Date()),
|
||||
@ -2045,6 +2042,50 @@ describe('ContributionResolver', () => {
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
describe('user tries to update admin contribution', () => {
|
||||
beforeAll(async () => {
|
||||
await mutate({
|
||||
mutation: login,
|
||||
variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' },
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await mutate({
|
||||
mutation: login,
|
||||
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
||||
})
|
||||
})
|
||||
|
||||
it('logs and throws "Cannot update contribution of moderator" error', async () => {
|
||||
jest.clearAllMocks()
|
||||
const adminContribution = await Contribution.findOne({
|
||||
where: {
|
||||
moderatorId: admin.id,
|
||||
userId: bibi.id,
|
||||
},
|
||||
})
|
||||
await expect(
|
||||
mutate({
|
||||
mutation: updateContribution,
|
||||
variables: {
|
||||
contributionId: (adminContribution && adminContribution.id) || -1,
|
||||
amount: 100.0,
|
||||
memo: 'Test Test Test',
|
||||
creationDate: new Date().toString(),
|
||||
},
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
errors: [new GraphQLError('Cannot update contribution of moderator')],
|
||||
})
|
||||
expect(logger.error).toBeCalledWith(
|
||||
'Cannot update contribution of moderator',
|
||||
expect.any(Object),
|
||||
bibi.id,
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('second creation surpasses the available amount ', () => {
|
||||
@ -2082,58 +2123,6 @@ describe('ContributionResolver', () => {
|
||||
// stephen@hawking.uk: [1000, 1000, 1000] - deleted
|
||||
// garrick@ollivander.com: [1000, 1000, 1000] - not activated
|
||||
|
||||
describe('user for creation to update does not exist', () => {
|
||||
it('throws an error', async () => {
|
||||
jest.clearAllMocks()
|
||||
await expect(
|
||||
mutate({
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: 1,
|
||||
email: 'bob@baumeister.de',
|
||||
amount: new Decimal(300),
|
||||
memo: 'Danke Bibi!',
|
||||
creationDate: contributionDateFormatter(new Date()),
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
errors: [new GraphQLError('Could not find User')],
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('logs the error "Could not find User"', () => {
|
||||
expect(logger.error).toBeCalledWith('Could not find User', 'bob@baumeister.de')
|
||||
})
|
||||
})
|
||||
|
||||
describe('user for creation to update is deleted', () => {
|
||||
it('throws an error', async () => {
|
||||
jest.clearAllMocks()
|
||||
await expect(
|
||||
mutate({
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: 1,
|
||||
email: 'stephen@hawking.uk',
|
||||
amount: new Decimal(300),
|
||||
memo: 'Danke Bibi!',
|
||||
creationDate: contributionDateFormatter(new Date()),
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
errors: [new GraphQLError('User was deleted')],
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('logs the error "User was deleted"', () => {
|
||||
expect(logger.error).toBeCalledWith('User was deleted', 'stephen@hawking.uk')
|
||||
})
|
||||
})
|
||||
|
||||
describe('creation does not exist', () => {
|
||||
it('throws an error', async () => {
|
||||
jest.clearAllMocks()
|
||||
@ -2142,7 +2131,6 @@ describe('ContributionResolver', () => {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: -1,
|
||||
email: 'bibi@bloxberg.de',
|
||||
amount: new Decimal(300),
|
||||
memo: 'Danke Bibi!',
|
||||
creationDate: contributionDateFormatter(new Date()),
|
||||
@ -2160,40 +2148,6 @@ describe('ContributionResolver', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('user email does not match creation user', () => {
|
||||
it('throws an error', async () => {
|
||||
jest.clearAllMocks()
|
||||
await expect(
|
||||
mutate({
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: creation ? creation.id : -1,
|
||||
email: 'bibi@bloxberg.de',
|
||||
amount: new Decimal(300),
|
||||
memo: 'Danke Bibi!',
|
||||
creationDate: creation
|
||||
? contributionDateFormatter(creation.contributionDate)
|
||||
: contributionDateFormatter(new Date()),
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
errors: [
|
||||
new GraphQLError(
|
||||
'User of the pending contribution and send user does not correspond',
|
||||
),
|
||||
],
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('logs the error "User of the pending contribution and send user does not correspond"', () => {
|
||||
expect(logger.error).toBeCalledWith(
|
||||
'User of the pending contribution and send user does not correspond',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('creation update is not valid', () => {
|
||||
// as this test has not clearly defined that date, it is a false positive
|
||||
it('throws an error', async () => {
|
||||
@ -2203,7 +2157,6 @@ describe('ContributionResolver', () => {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: creation ? creation.id : -1,
|
||||
email: 'peter@lustig.de',
|
||||
amount: new Decimal(1900),
|
||||
memo: 'Danke Peter!',
|
||||
creationDate: creation
|
||||
@ -2240,7 +2193,6 @@ describe('ContributionResolver', () => {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: creation?.id,
|
||||
email: 'peter@lustig.de',
|
||||
amount: new Decimal(300),
|
||||
memo: 'Danke Peter!',
|
||||
creationDate: creation
|
||||
@ -2255,7 +2207,6 @@ describe('ContributionResolver', () => {
|
||||
date: expect.any(String),
|
||||
memo: 'Danke Peter!',
|
||||
amount: '300',
|
||||
creation: ['1000', '700', '500'],
|
||||
},
|
||||
},
|
||||
}),
|
||||
@ -2282,7 +2233,6 @@ describe('ContributionResolver', () => {
|
||||
mutation: adminUpdateContribution,
|
||||
variables: {
|
||||
id: creation?.id,
|
||||
email: 'peter@lustig.de',
|
||||
amount: new Decimal(200),
|
||||
memo: 'Das war leider zu Viel!',
|
||||
creationDate: creation
|
||||
@ -2297,7 +2247,6 @@ describe('ContributionResolver', () => {
|
||||
date: expect.any(String),
|
||||
memo: 'Das war leider zu Viel!',
|
||||
amount: '200',
|
||||
creation: ['1000', '800', '1000'],
|
||||
},
|
||||
},
|
||||
}),
|
||||
|
||||
@ -201,6 +201,9 @@ export class ContributionResolver {
|
||||
user.id,
|
||||
)
|
||||
}
|
||||
if (contributionToUpdate.moderatorId) {
|
||||
throw new LogError('Cannot update contribution of moderator', contributionToUpdate, user.id)
|
||||
}
|
||||
if (
|
||||
contributionToUpdate.contributionStatus !== ContributionStatus.IN_PROGRESS &&
|
||||
contributionToUpdate.contributionStatus !== ContributionStatus.PENDING
|
||||
@ -306,41 +309,27 @@ export class ContributionResolver {
|
||||
@Authorized([RIGHTS.ADMIN_UPDATE_CONTRIBUTION])
|
||||
@Mutation(() => AdminUpdateContribution)
|
||||
async adminUpdateContribution(
|
||||
@Args() { id, email, amount, memo, creationDate }: AdminUpdateContributionArgs,
|
||||
@Args() { id, amount, memo, creationDate }: AdminUpdateContributionArgs,
|
||||
@Ctx() context: Context,
|
||||
): Promise<AdminUpdateContribution> {
|
||||
const clientTimezoneOffset = getClientTimezoneOffset(context)
|
||||
const emailContact = await UserContact.findOne({
|
||||
where: { email },
|
||||
withDeleted: true,
|
||||
relations: ['user'],
|
||||
})
|
||||
if (!emailContact || !emailContact.user) {
|
||||
throw new LogError('Could not find User', email)
|
||||
}
|
||||
if (emailContact.deletedAt || emailContact.user.deletedAt) {
|
||||
throw new LogError('User was deleted', email)
|
||||
}
|
||||
|
||||
const moderator = getUser(context)
|
||||
|
||||
const contributionToUpdate = await DbContribution.findOne({
|
||||
where: { id, confirmedAt: IsNull(), deniedAt: IsNull() },
|
||||
})
|
||||
|
||||
if (!contributionToUpdate) {
|
||||
throw new LogError('Contribution not found', id)
|
||||
}
|
||||
|
||||
if (contributionToUpdate.userId !== emailContact.user.id) {
|
||||
throw new LogError('User of the pending contribution and send user does not correspond')
|
||||
}
|
||||
|
||||
if (contributionToUpdate.moderatorId === null) {
|
||||
throw new LogError('An admin is not allowed to update an user contribution')
|
||||
}
|
||||
|
||||
const creationDateObj = new Date(creationDate)
|
||||
let creations = await getUserCreation(emailContact.user.id, clientTimezoneOffset)
|
||||
let creations = await getUserCreation(contributionToUpdate.userId, clientTimezoneOffset)
|
||||
|
||||
// TODO: remove this restriction
|
||||
if (contributionToUpdate.contributionDate.getMonth() === creationDateObj.getMonth()) {
|
||||
@ -363,9 +352,9 @@ export class ContributionResolver {
|
||||
result.amount = amount
|
||||
result.memo = contributionToUpdate.memo
|
||||
result.date = contributionToUpdate.contributionDate
|
||||
result.creation = await getUserCreation(emailContact.user.id, clientTimezoneOffset)
|
||||
|
||||
await EVENT_ADMIN_CONTRIBUTION_UPDATE(
|
||||
emailContact.user,
|
||||
{ id: contributionToUpdate.userId } as DbUser,
|
||||
moderator,
|
||||
contributionToUpdate,
|
||||
amount,
|
||||
|
||||
@ -133,18 +133,11 @@ export const unDeleteUser = gql`
|
||||
`
|
||||
|
||||
export const adminUpdateContribution = gql`
|
||||
mutation ($id: Int!, $email: String!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
|
||||
adminUpdateContribution(
|
||||
id: $id
|
||||
email: $email
|
||||
amount: $amount
|
||||
memo: $memo
|
||||
creationDate: $creationDate
|
||||
) {
|
||||
mutation ($id: Int!, $amount: Decimal!, $memo: String!, $creationDate: String!) {
|
||||
adminUpdateContribution(id: $id, amount: $amount, memo: $memo, creationDate: $creationDate) {
|
||||
amount
|
||||
date
|
||||
memo
|
||||
creation
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
@ -47,7 +47,7 @@
|
||||
<div v-else class="font-weight-bold">{{ amount | GDD }}</div>
|
||||
</b-col>
|
||||
<b-col cols="12" md="1" lg="1" class="text-right align-items-center">
|
||||
<div v-if="messagesCount > 0" @click="visible = !visible">
|
||||
<div v-if="messagesCount > 0 && !moderatorId" @click="visible = !visible">
|
||||
<collapse-icon class="text-right" :visible="visible" />
|
||||
</div>
|
||||
</b-col>
|
||||
@ -58,7 +58,7 @@
|
||||
>
|
||||
<b-col cols="3" class="mr-auto text-center">
|
||||
<div
|
||||
v-if="!['CONFIRMED', 'DELETED'].includes(state) && !allContribution"
|
||||
v-if="!['CONFIRMED', 'DELETED'].includes(state) && !allContribution && !moderatorId"
|
||||
class="test-delete-contribution pointer mr-3"
|
||||
@click="deleteContribution({ id })"
|
||||
>
|
||||
@ -69,7 +69,7 @@
|
||||
</b-col>
|
||||
<b-col cols="3" class="text-center">
|
||||
<div
|
||||
v-if="!['CONFIRMED', 'DELETED'].includes(state) && !allContribution"
|
||||
v-if="!['CONFIRMED', 'DELETED'].includes(state) && !allContribution && !moderatorId"
|
||||
class="test-edit-contribution pointer mr-3"
|
||||
@click="
|
||||
$emit('update-contribution-form', {
|
||||
@ -84,9 +84,8 @@
|
||||
<div>{{ $t('edit') }}</div>
|
||||
</div>
|
||||
</b-col>
|
||||
|
||||
<b-col cols="6" class="text-center">
|
||||
<div v-if="messagesCount > 0" class="pointer" @click="visible = !visible">
|
||||
<div v-if="messagesCount > 0 && !moderatorId" class="pointer" @click="visible = !visible">
|
||||
<b-icon icon="chat-dots"></b-icon>
|
||||
<div>{{ $t('moderatorChat') }}</div>
|
||||
</div>
|
||||
@ -180,6 +179,11 @@ export default {
|
||||
required: false,
|
||||
default: false,
|
||||
},
|
||||
moderatorId: {
|
||||
type: Number,
|
||||
required: false,
|
||||
default: 0,
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
|
||||
@ -187,6 +187,7 @@ export const listContributions = gql`
|
||||
messagesCount
|
||||
deniedAt
|
||||
deniedBy
|
||||
moderatorId
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -236,7 +237,7 @@ export const searchAdminUsers = gql`
|
||||
`
|
||||
|
||||
export const listContributionMessages = gql`
|
||||
query($contributionId: Float!, $pageSize: Int = 25, $currentPage: Int = 1, $order: Order = ASC) {
|
||||
query($contributionId: Int!, $pageSize: Int = 25, $currentPage: Int = 1, $order: Order = ASC) {
|
||||
listContributionMessages(
|
||||
contributionId: $contributionId
|
||||
pageSize: $pageSize
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user