fix acquisition creeation

This commit is contained in:
2024-05-27 12:16:15 +02:00
parent 1868b3e248
commit 9ccba70ede

View File

@@ -1,4 +1,5 @@
import { marbasAxiosInstance, MarbasObject } from "@/marbas";
import { AxiosResponse } from "axios";
import log from "loglevel";
import { defineStore } from "pinia";
import { computed, ref } from "vue";
@@ -15,6 +16,17 @@ export type MarbasAcquiredType = MarbasObject & {
user: number;
}
type RawMarbasAcquiredType = Omit<MarbasAcquiredType, 'date'> & {
date: string;
}
type InsertableRawMarbasAcquiredType = Omit<MarbasAcquiredType, 'id' | 'user' | 'date'>;
const mapRawMarbasAcquiredType = (raw: RawMarbasAcquiredType): MarbasAcquiredType => ({
...raw,
date: raw.date ? new Date(raw.date) : new Date()
});
const endpoint = '/api/acquisitions/';
export const useAcquiredTypesStore = defineStore('market-acquisition', () => {
@@ -22,14 +34,13 @@ export const useAcquiredTypesStore = defineStore('market-acquisition', () => {
const types = computed(() => acquiredTypes.value.filter(item => item.remaining > 0));
const addAcquiredType = async (type: number, quantity: number, price: number, source?: AcquiredTypeSource) => {
const newItem = (await marbasAxiosInstance.post<MarbasAcquiredType>(endpoint, {
const newItem = mapRawMarbasAcquiredType((await marbasAxiosInstance.post<RawMarbasAcquiredType, AxiosResponse<RawMarbasAcquiredType>, InsertableRawMarbasAcquiredType>(endpoint, {
type: type,
quantity: quantity,
remaining: quantity,
price: price,
date: new Date(),
source: source ?? 'misc',
})).data
})).data);
acquiredTypes.value = [...acquiredTypes.value, newItem];
log.info(`Acquired type ${newItem.id} with quantity ${newItem.quantity} and price ${newItem.price}`, newItem);
@@ -57,7 +68,7 @@ export const useAcquiredTypesStore = defineStore('market-acquisition', () => {
log.info(`Acquired type ${item.id} remaining: ${item.remaining}`, item);
};
marbasAxiosInstance.get<MarbasAcquiredType[]>(endpoint).then(res => acquiredTypes.value = res.data.map(item => ({ ...item, date: new Date(item.date) })));
marbasAxiosInstance.get<RawMarbasAcquiredType[]>(endpoint).then(res => acquiredTypes.value = res.data.map(mapRawMarbasAcquiredType));
return { acquiredTypes: types, addAcquiredType, removeAcquiredType };
});