Skip to content

Commit

Permalink
Merge pull request #62 from ConductionNL/feature/PC108-99/contact-mom…
Browse files Browse the repository at this point in the history
…ent-sluiten

added ability to close contact moment
  • Loading branch information
remko48 authored Dec 2, 2024
2 parents b6629e8 + 67380dd commit 767505b
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 130 deletions.
5 changes: 5 additions & 0 deletions img/progress-close-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions img/progress-close-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/entities/contactmoment/contactmoment.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const mockContactMomentData = (): TContactMoment[] => [
taak: 'Taak 3',
product: 'Product 3',
startDate: new Date().toISOString(),
status: 'open',
},
]

Expand Down
3 changes: 3 additions & 0 deletions src/entities/contactmoment/contactmoment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class ContactMoment implements TContactMoment {
public taak: string
public product: string
public startDate: string
public status: string

constructor(source: TContactMoment) {
this.id = source.id || ''
Expand All @@ -23,6 +24,7 @@ export class ContactMoment implements TContactMoment {
this.taak = source.taak || ''
this.product = source.product || ''
this.startDate = source.startDate || ''
this.status = source.status || 'open'
}

public validate(): SafeParseReturnType<TContactMoment, unknown> {
Expand All @@ -36,6 +38,7 @@ export class ContactMoment implements TContactMoment {
taak: z.string().min(1),
product: z.string().min(1),
startDate: z.string().min(1),
status: z.string().min(1),
})

return schema.safeParse(this)
Expand Down
1 change: 1 addition & 0 deletions src/entities/contactmoment/contactmoment.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type TContactMoment = {
taak: string;
product: string;
startDate: string;
status: string;
}
125 changes: 0 additions & 125 deletions src/store/modules/contactmoment.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('Contact Moment Store', () => {
it('sets contact moment list correctly', () => {
const store = useContactMomentStore()

store.setContactMomentList(mockContactMoment())
store.setContactMomentenList(mockContactMoment())

expect(store.contactMomentList).toHaveLength(mockContactMoment().length)
expect(store.contactMomentenList).toHaveLength(mockContactMoment().length)

store.contactMomentList.forEach((item, index) => {
store.contactMomentenList.forEach((item, index) => {
expect(item).toBeInstanceOf(ContactMoment)
expect(item).toEqual(mockContactMoment()[index])
expect(item.validate().success).toBe(true)
Expand Down
165 changes: 165 additions & 0 deletions src/store/modules/contactmoment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* eslint-disable no-console */
import { defineStore } from 'pinia'
import { ContactMoment, TContactMoment } from '../../entities/index.js'

const apiEndpoint = '/index.php/apps/zaakafhandelapp/api/contactmomenten'

export const useContactMomentStore = defineStore('contactmomenten', {
state: () => ({
contactMomentItem: null as ContactMoment,
contactMomentenList: [] as ContactMoment[],
}),
actions: {
/**
* Set the active contact moment item.
*
* @param contactMomentItem - The contact moment item to set.
*/
setContactMomentItem(contactMomentItem: TContactMoment | ContactMoment) {
this.contactMomentItem = contactMomentItem && new ContactMoment(contactMomentItem)
console.info('Active contactmoment item set to ' + contactMomentItem)
},
/**
* Set the list of contact moments.
*
* @param contactMomentenList - The list of contact moments to set.
*/
setContactMomentenList(contactMomentenList: TContactMoment[] | ContactMoment[]) {
this.contactMomentenList = contactMomentenList.map(
(contactMomentItem) => new ContactMoment(contactMomentItem),
)
console.info('Contactmomenten list set to ' + contactMomentenList.length + ' items')
},
/**
* Refresh the list of contact moments.
*
* @param search - Optional search query to filter the contact moments list. (default: `null`)
* @throws If the HTTP request fails.
* @return {Promise<{ response: Response, data: TContactMoment[], entities: ContactMoment[] }>} The response, raw data, and entities.
*/
async refreshContactMomentenList(search: string = null): Promise<{ response: Response, data: TContactMoment[], entities: ContactMoment[] }> {
let endpoint = apiEndpoint

if (search !== null && search !== '') {
endpoint = endpoint + '?_search=' + search
}

const response = await fetch(endpoint, {
method: 'GET',
})

if (!response.ok) {
console.info(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

const data = (await response.json()).results as TContactMoment[]
const entities = data.map((contactMomentItem) => new ContactMoment(contactMomentItem))

this.setContactMomentenList(data)

return { response, data, entities }
},
/**
* Fetch a single contact moment by its ID.
*
* @param id - The ID of the contact moment to fetch.
* @throws If the ID is invalid or if the HTTP request fails.
* @return {Promise<{ response: Response, data: TContactMoment, entity: ContactMoment }>} The response, raw data, and entity.
*/
async getContactMoment(id: string | number): Promise<{ response: Response, data: TContactMoment, entity: ContactMoment }> {
if (!id || (typeof id !== 'string' && typeof id !== 'number') || (typeof id === 'string' && id.trim() === '')) {
throw new Error('Invalid ID provided for fetching contact moment')
}

const endpoint = `${apiEndpoint}/${id}`

const response = await fetch(endpoint, {
method: 'GET',
})

if (!response.ok) {
console.info(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

const data = await response.json() as TContactMoment
const entity = new ContactMoment(data)

this.setContactMomentItem(data)

return { response, data, entity }
},
/**
* Delete a contact moment.
*
* @param contactMomentItem - The contact moment item to delete.
* @throws If there is no contact moment item to delete or if the contact moment item does not have an id.
* @throws If the HTTP request fails.
* @return {Promise<{ response: Response }>} The response from the delete request.
*/
async deleteContactMoment(contactMomentItem: ContactMoment): Promise<{ response: Response }> {
if (!contactMomentItem || !contactMomentItem.id) {
throw new Error('No contactmoment item to delete')
}

const endpoint = `${apiEndpoint}/${contactMomentItem.id}`

const response = await fetch(endpoint, {
method: 'DELETE',
})

if (!response.ok) {
console.info(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

this.refreshContactMomentenList()

return { response }
},
/**
* Save a contact moment to the database. If the contact moment does not have an id, it will be created.
* Otherwise, it will be updated.
*
* @param contactMomentItem - The contact moment item to save.
* @throws If there is no contact moment item to save or if the HTTP request fails.
* @return {Promise<{ response: Response, data: TContactMoment, entity: ContactMoment }>} The response, raw data, and entity.
*/
async saveContactMoment(contactMomentItem: TContactMoment | ContactMoment): Promise<{ response: Response, data: TContactMoment, entity: ContactMoment }> {
if (!contactMomentItem) {
throw new Error('No contactmoment item to save')
}

const isNewContactMoment = !contactMomentItem.id
const endpoint = isNewContactMoment
? `${apiEndpoint}`
: `${apiEndpoint}/${contactMomentItem.id}`
const method = isNewContactMoment ? 'POST' : 'PUT'

const response = await fetch(
endpoint,
{
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ ...contactMomentItem }),
},
)

if (!response.ok) {
console.info(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

const data = await response.json() as TContactMoment
const entity = new ContactMoment(data)

this.setContactMomentItem(data)
this.refreshContactMomentenList()

return { response, data, entity }
},
},
})
2 changes: 1 addition & 1 deletion src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useKlantStore } from './modules/klanten.js'
import { useRolStore } from './modules/rol.ts'
import { useTaakStore } from './modules/taak.js'
import { useSearchStore } from './modules/search.ts'
import { useContactMomentStore } from './modules/contactmoment.js'
import { useContactMomentStore } from './modules/contactmoment.ts'
const berichtStore = useBerichtStore(pinia)
const klantStore = useKlantStore(pinia)
const navigationStore = useNavigationStore(pinia)
Expand Down
Loading

0 comments on commit 767505b

Please sign in to comment.