feat(#4): Add versioning of application form
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import type { ApplicationFormVersionDto, ApplicationFormVersionListItemDto, ApplicationFormDto } from '~~/.api-client'
|
||||
import { useApplicationFormVersionApi } from '~/composables'
|
||||
|
||||
export function useApplicationFormVersion() {
|
||||
const versionApi = useApplicationFormVersionApi()
|
||||
|
||||
async function getVersions(applicationFormId: string): Promise<ApplicationFormVersionListItemDto[]> {
|
||||
try {
|
||||
return await versionApi.getVersions(applicationFormId)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed retrieving versions for application form ${applicationFormId}:`, e)
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
async function getVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormVersionDto> {
|
||||
try {
|
||||
return await versionApi.getVersion(applicationFormId, versionNumber)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed retrieving version ${versionNumber} for application form ${applicationFormId}:`, e)
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormDto> {
|
||||
if (!applicationFormId) {
|
||||
return Promise.reject(new Error('Application form ID missing'))
|
||||
}
|
||||
|
||||
try {
|
||||
return await versionApi.restoreVersion(applicationFormId, versionNumber)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed restoring version ${versionNumber} for application form ${applicationFormId}:`, e)
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
getVersions,
|
||||
getVersion,
|
||||
restoreVersion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
ApplicationFormVersionApi,
|
||||
Configuration,
|
||||
type ApplicationFormVersionDto,
|
||||
type ApplicationFormVersionListItemDto,
|
||||
type ApplicationFormDto
|
||||
} from '~~/.api-client'
|
||||
import { cleanDoubleSlashes, withoutTrailingSlash } from 'ufo'
|
||||
import { wrappedFetchWrap } from '~/utils/wrappedFetch'
|
||||
|
||||
export function useApplicationFormVersionApi() {
|
||||
const appBaseUrl = useRuntimeConfig().app.baseURL
|
||||
const { serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public
|
||||
|
||||
const basePath = withoutTrailingSlash(
|
||||
cleanDoubleSlashes(
|
||||
import.meta.client
|
||||
? appBaseUrl + clientProxyBasePath
|
||||
: useRequestURL().origin + clientProxyBasePath + serverApiBasePath
|
||||
)
|
||||
)
|
||||
|
||||
const versionApiClient = new ApplicationFormVersionApi(
|
||||
new Configuration({ basePath, fetchApi: wrappedFetchWrap(useRequestFetch()) })
|
||||
)
|
||||
|
||||
async function getVersions(applicationFormId: string): Promise<ApplicationFormVersionListItemDto[]> {
|
||||
return versionApiClient.getApplicationFormVersions({ id: applicationFormId })
|
||||
}
|
||||
|
||||
async function getVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormVersionDto> {
|
||||
return versionApiClient.getApplicationFormVersion({
|
||||
id: applicationFormId,
|
||||
versionNumber
|
||||
})
|
||||
}
|
||||
|
||||
async function restoreVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormDto> {
|
||||
return versionApiClient.restoreApplicationFormVersion({
|
||||
id: applicationFormId,
|
||||
versionNumber
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
getVersions,
|
||||
getVersion,
|
||||
restoreVersion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user