Skip to content
Snippets Groups Projects
Commit 26b9a085 authored by Trygve Jørgensen's avatar Trygve Jørgensen
Browse files

Merge branch 'enhancement/project-cleanup' into 'dev'

Config reliability

See merge request !54
parents 489aa84e 70893380
No related branches found
No related tags found
3 merge requests!66Final merge,!54Config reliability,!4Pipeline fix
Pipeline #283557 passed
......@@ -177,7 +177,6 @@ router.beforeEach(async (to, from, next) => {
}
const isConfigured = userStore.user.isConfigured
if (configRequired && !isConfigured) {
await router.replace({ name: 'configure-biometric' })
return next({ name: 'configure-biometric' })
......
......@@ -2,30 +2,36 @@ import { ref } from 'vue'
import { defineStore } from 'pinia'
import authInterceptor from '@/services/authInterceptor'
import { AxiosError } from 'axios'
import type { ChallengeConfig } from '@/types/challengeConfig'
import router from '@/router'
export const useUserConfigStore = defineStore('userConfig', () => {
const role = ref('USER')
const experience = ref('')
const motivation = ref('')
const challengeTypeConfigs = ref(
[] as {
type: string
specificAmount: number
generalAmount: number
}[]
)
const accounts = ref({
savings: '',
spending: ''
const challengeConfig = ref<ChallengeConfig>({
experience: '',
motivation: '',
challengeTypeConfigs: []
})
const savingAccount = ref({
accountType: 'SAVING',
accNumber: 0,
balance: 0
})
const spendingAccount = ref({
accountType: 'SPENDING',
accNumber: 0,
balance: 0
})
const errorMessage = ref<string>('')
const setExperience = (value: string) => {
experience.value = value
challengeConfig.value.experience = value
}
const setMotivation = (value: string) => {
motivation.value = value
challengeConfig.value.motivation = value
}
const addChallengeTypeConfig = (
......@@ -33,53 +39,62 @@ export const useUserConfigStore = defineStore('userConfig', () => {
specificAmount: number,
generalAmount: number
) => {
challengeTypeConfigs.value.push({ type, specificAmount, generalAmount })
challengeConfig.value.challengeTypeConfigs.push({
type: type,
specificAmount: specificAmount,
generalAmount: generalAmount
})
}
const postAccount = async (
accountType: 'SAVING' | 'SPENDING',
accNumber: string,
balance: number
) => {
const payload = {
accountType,
accNumber,
balance
const setAccount = (type: 'SAVING' | 'SPENDING', accNumber: number) => {
if (type === 'SAVING') {
savingAccount.value.accNumber = accNumber
} else {
spendingAccount.value.accNumber = accNumber
}
await authInterceptor.post('/accounts', payload).catch((error) => {
const axiosError = error as AxiosError
errorMessage.value =
(axiosError.response?.data as string) || 'An error occurred while posting account'
console.error('Error posting account:', errorMessage.value)
})
}
const postUserConfig = async () => {
const payload = {
experience: experience.value,
motivation: motivation.value,
challengeTypeConfigs: Array.from(challengeTypeConfigs.value)
const createConfig = () => {
authInterceptor
.post('/accounts', savingAccount.value)
.then(() => authInterceptor.post('/accounts', spendingAccount.value))
.then(() => authInterceptor.post('/config/challenge', challengeConfig.value))
.then(() => {
resetConfig()
return router.push({ name: 'home', query: { firstLogin: 'true' } })
})
.catch((error: AxiosError) => {
console.error(error)
resetConfig()
return router.push({ name: 'configurations1' })
})
}
const resetConfig = () => {
challengeConfig.value = {
experience: '',
motivation: '',
challengeTypeConfigs: []
}
savingAccount.value = {
accountType: 'SAVING',
accNumber: 0,
balance: 0
}
spendingAccount.value = {
accountType: 'SPENDING',
accNumber: 0,
balance: 0
}
await authInterceptor.post('/config/challenge', payload).catch((error) => {
const axiosError = error as AxiosError
errorMessage.value =
(axiosError.response?.data as string) ||
'An error occurred while updating configuration'
console.error('Error updating configuration:', errorMessage.value)
})
}
return {
role,
experience,
motivation,
challengeTypeConfigs,
accounts,
challengeConfig,
errorMessage,
setExperience,
setMotivation,
setAccount,
addChallengeTypeConfig,
postAccount,
postUserConfig
createConfig
}
})
......@@ -238,7 +238,6 @@ export const useUserStore = defineStore('user', () => {
const checkIfUserConfigured = async () => {
await authInterceptor('/config')
.then((response) => {
console.log('User configured: ' + user.value.isConfigured)
user.value.isConfigured = response.data.challengeConfig != null
})
.catch(() => {
......
......@@ -63,7 +63,6 @@
import { computed, ref } from 'vue'
import { useUserConfigStore } from '@/stores/userConfigStore'
import ContinueButtonComponent from '@/components/ContinueButtonComponent.vue'
import router from '@/router'
import SpareComponent from '@/components/SpareComponent.vue'
const MAX_DIGITS = 11
......@@ -80,14 +79,13 @@ const isFormValid = computed(() => {
})
async function onButtonClick() {
const savingAccountNumber = savingsAccount.value.replace(/\./g, '')
const spendingAccountNumber = spendingAccount.value.replace(/\./g, '')
const savingAccountNumber = parseInt(savingsAccount.value.replace(/\./g, ''))
const spendingAccountNumber = parseInt(spendingAccount.value.replace(/\./g, ''))
await userConfigStore.postAccount('SAVING', savingAccountNumber, 0)
await userConfigStore.postAccount('SPENDING', spendingAccountNumber, 0)
await userConfigStore.postUserConfig()
userConfigStore.setAccount('SAVING', savingAccountNumber)
userConfigStore.setAccount('SPENDING', spendingAccountNumber)
await router.push({ name: 'home', query: { firstLogin: 'true' } })
await userConfigStore.createConfig()
}
function restrictToNumbers(event: InputEvent, type: string) {
......
......@@ -7,7 +7,7 @@
<img alt="bioAuthFace" class="w-40 h-40" src="@/assets/bioAuthFace.png" />
</div>
<div class="flex flex-col gap-5">
<button @click="userStore.bioRegister()">Legg til nå!</button>
<button @click="bioRegister">Legg til nå!</button>
<button @click="router.push({ name: 'configurations1' })">Jeg gjør det senere</button>
</div>
</div>
......@@ -17,6 +17,11 @@ import { useUserStore } from '@/stores/userStore'
import router from '@/router'
const userStore = useUserStore()
const bioRegister = async () => {
await userStore.bioRegister()
await router.push({ name: 'configurations1' })
}
</script>
<style scoped></style>
......@@ -95,14 +95,14 @@ import SpareComponent from '@/components/SpareComponent.vue'
const userConfigStore = useUserConfigStore()
const options = ref(userConfigStore.challengeTypeConfigs)
const options = ref(userConfigStore.challengeConfig.challengeTypeConfigs)
const amounts = ref(options.value.map(() => ''))
const isAllAmountsFilled = computed(() => amounts.value.every((amount) => amount.trim() !== ''))
const onButtonClick = () => {
options.value.forEach((option, index) => {
userConfigStore.challengeTypeConfigs[index].specificAmount =
userConfigStore.challengeConfig.challengeTypeConfigs[index].specificAmount =
parseFloat(amounts.value[index]) || 0
})
router.push({ name: 'configurations5' })
......
......@@ -95,14 +95,14 @@ import SpareComponent from '@/components/SpareComponent.vue'
const userConfigStore = useUserConfigStore()
const options = ref(userConfigStore.challengeTypeConfigs)
const options = ref(userConfigStore.challengeConfig.challengeTypeConfigs)
const amounts = ref(options.value.map(() => ''))
const isAllAmountsFilled = computed(() => amounts.value.every((amount) => amount.trim() !== ''))
const onButtonClick = async () => {
options.value.forEach((option, index) => {
userConfigStore.challengeTypeConfigs[index].generalAmount =
userConfigStore.challengeConfig.challengeTypeConfigs[index].generalAmount =
parseFloat(amounts.value[index]) || 0
})
......
......@@ -122,7 +122,14 @@ const onButtonClick = () => {
generalAmount: 0
}))
userConfigStore.challengeTypeConfigs = [...predefinedChallengeTypes, ...customChallengeTypes]
for (const challengeType of predefinedChallengeTypes.concat(customChallengeTypes)) {
userConfigStore.addChallengeTypeConfig(
challengeType.type,
challengeType.specificAmount,
challengeType.generalAmount
)
}
router.push({ name: 'configurations4' })
}
</script>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment