Rework to use marbas and authentik instead of poketbase (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
63
src/market/acquisition/acquisition.ts
Normal file
63
src/market/acquisition/acquisition.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { marbasAxiosInstance } from "@/service";
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, ref } from "vue";
|
||||
|
||||
export type AcquiredMarketType = {
|
||||
id: number;
|
||||
type: number;
|
||||
quantity: number;
|
||||
remaining: number;
|
||||
price: number;
|
||||
date: Date;
|
||||
source: 'bo' | 'so' | 'prod';
|
||||
user: number;
|
||||
}
|
||||
|
||||
const endpoint = '/api/acquisitions/';
|
||||
|
||||
export const useAcquiredTypesStore = defineStore('market-acquisition', () => {
|
||||
const acquiredTypes = ref<AcquiredMarketType[]>([]);
|
||||
|
||||
const types = computed(() => acquiredTypes.value);
|
||||
const addType = async (type: number, quantity: number, price: number) => {
|
||||
acquiredTypes.value = [...acquiredTypes.value, (await marbasAxiosInstance.post<AcquiredMarketType>(endpoint, {
|
||||
type: type,
|
||||
quantity: quantity,
|
||||
remaining: quantity,
|
||||
price: price,
|
||||
date: new Date(),
|
||||
source: 'bo'
|
||||
})).data];
|
||||
};
|
||||
const removeType = async (type: number, quantity: number) => {
|
||||
const found = acquiredTypes.value.find(item => item.type === type);
|
||||
|
||||
if (!found) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (found.remaining <= 0) {
|
||||
acquiredTypes.value = acquiredTypes.value.filter(i => i.type !== type);
|
||||
|
||||
} else {
|
||||
acquiredTypes.value = acquiredTypes.value.map(i => {
|
||||
if (i.type === item.type) {
|
||||
return item;
|
||||
} else {
|
||||
return i;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const item = {
|
||||
...found,
|
||||
remaining: found.remaining - quantity
|
||||
};
|
||||
|
||||
await marbasAxiosInstance.put(`${endpoint}${item.id}`, item);
|
||||
};
|
||||
|
||||
marbasAxiosInstance.get<AcquiredMarketType[]>(endpoint).then(res => acquiredTypes.value = res.data.filter(item => item.remaining > 0));
|
||||
|
||||
return { types, addType, removeType };
|
||||
});
|
||||
Reference in New Issue
Block a user