74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import {defineStore} from "pinia";
|
|
import {computed, ref} from "vue";
|
|
import {acquisitionApi, activityApi} from "@/mammon";
|
|
import {AcquisitionResponse, ActivitySourceResponse} from "@/generated/mammon";
|
|
import {onActivitiesProcessed} from "@/activity";
|
|
|
|
export type RawAcquiredType = {
|
|
id: string;
|
|
ledgerId: string;
|
|
type: number;
|
|
quantity: number;
|
|
remaining: number;
|
|
price: number;
|
|
date: Date;
|
|
source: ActivitySourceResponse;
|
|
}
|
|
|
|
export const toAcquiredType = (a: AcquisitionResponse): RawAcquiredType => ({
|
|
id: a.acquisitionId,
|
|
ledgerId: a.ledgerId,
|
|
type: a.marketTypeId,
|
|
quantity: a.quantity,
|
|
remaining: a.remaining,
|
|
price: a.unitCost,
|
|
date: new Date(a.datetime),
|
|
source: a.source,
|
|
});
|
|
|
|
export const useAcquiredTypesStore = defineStore('market-acquisition', () => {
|
|
const acquiredTypes = ref<RawAcquiredType[]>([]);
|
|
|
|
const types = computed(() => acquiredTypes.value.filter(item => item.remaining > 0));
|
|
|
|
const addAcquiredType = (type: number, quantity: number, price: number, ledgerId: string, datetime: Date) =>
|
|
activityApi.acquire({
|
|
source: { type: 'LEDGER', characterId: null, ledgerId },
|
|
marketTypeId: type,
|
|
quantity,
|
|
unitPrice: price,
|
|
datetime: datetime.toISOString(),
|
|
taxes: null,
|
|
description: null,
|
|
});
|
|
|
|
const removeAcquiredType = (id: string, quantity: number, ledgerId: string, datetime: Date) => {
|
|
const acquisition = acquiredTypes.value.find(a => a.id === id);
|
|
|
|
if (!acquisition) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
return activityApi.consume({
|
|
source: { type: 'LEDGER', characterId: null, ledgerId },
|
|
marketTypeId: acquisition.type,
|
|
quantity,
|
|
unitPrice: 0,
|
|
datetime: datetime.toISOString(),
|
|
taxes: null,
|
|
description: null,
|
|
});
|
|
};
|
|
|
|
const processNewActivities = () => activityApi.processNewActivities();
|
|
|
|
const refresh = () => acquisitionApi.findAllAcquisitions()
|
|
.then(response => acquiredTypes.value = response.data.map(toAcquiredType));
|
|
|
|
refresh();
|
|
|
|
onActivitiesProcessed(refresh);
|
|
|
|
return { acquiredTypes: types, addAcquiredType, removeAcquiredType, processNewActivities, refresh };
|
|
});
|