Fix marbas integration

This commit is contained in:
2024-05-18 14:14:57 +02:00
parent 5887ecb638
commit e3a5eeb50d
14 changed files with 107 additions and 93 deletions

View File

@@ -0,0 +1,3 @@
export type MarbasObject = {
id: number;
}

3
src/marbas/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export * from './MarbasObject';
export * from './marbasService';

View File

@@ -0,0 +1,46 @@
import { useAuthStore } from "@/auth";
import { logResource } from "@/service";
import axios from "axios";
export const marbasAxiosInstance = axios.create({
baseURL: import.meta.env.VITE_MARBAS_URL,
headers: {
'Accept': 'application/json',
"Content-Type": "application/json",
},
})
marbasAxiosInstance.interceptors.request.use(r => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error("Not logged in");
}
const accessToken = authStore.accessToken;
if (accessToken) {
r.headers.Authorization = `Bearer ${accessToken}`;
}
if (!r.params?.page_size) {
r.params = { ...r.params, page_size: 250 };
}
return r;
})
logResource(marbasAxiosInstance)
marbasAxiosInstance.interceptors.response.use(async r => {
const next = r.data?.next;
let results = r.data?.results;
if (next) {
results = results.concat((await marbasAxiosInstance.request({
...r.config,
url: next,
baseURL: '',
})).data);
}
if (results) {
r.data = results;
}
return r;
})

View File

@@ -1,7 +1,7 @@
import { MarketType } from "..";
import { AcquiredMarketType } from "./acquisition";
import { MarbasAcquiredType } from "./acquisition";
export type AcquiredType = Omit<AcquiredMarketType, 'type'> & {
export type AcquiredType = Omit<MarbasAcquiredType, 'type'> & {
type: MarketType,
buy: number,
sell: number

View File

@@ -1,7 +1,7 @@
<script setup lang="ts">
import { SortableHeader, useSort } from '@/components/table';
import { formatIsk, percentFormater } from '@/formaters';
import { MarketType, MarketTypeLabel, TaxInput, useMarketTaxStore } from "@/market";
import { MarketTypeLabel, TaxInput, useMarketTaxStore } from "@/market";
import { MinusIcon, PlusIcon } from '@heroicons/vue/24/outline';
import { useStorage } from '@vueuse/core';
import { computed, ref } from 'vue';
@@ -9,7 +9,7 @@ import { AcquiredType } from './AcquiredType';
import AcquisitionQuantilsTooltip from './AcquisitionQuantilsTooltip.vue';
type Result = {
type: MarketType;
type: AcquiredType;
typeID: number;
name: string;
buy: number;
@@ -26,8 +26,8 @@ interface Props {
}
interface Emits {
(e: 'buy', type: MarketType, price: number, buy: number, sell: number): void;
(e: 'sell', type: MarketType): void;
(e: 'buy', type: AcquiredType, price: number, buy: number, sell: number): void;
(e: 'sell', type: AcquiredType): void;
}
const props = withDefaults(defineProps<Props>(), {
@@ -46,7 +46,7 @@ const { sortedArray, headerProps } = useSort<Result>(computed(() => props.items
const precentProfit = marketTaxStore.calculateProfit(r.price, r.sell);
return {
type: r.type,
type: r,
typeID: r.type.id,
name: r.type.name,
buy: r.buy,

View File

@@ -38,7 +38,7 @@ const add = () => {
return;
}
acquiredTypesStore.addType(id, count.value, price.value);
acquiredTypesStore.addAcquiredType(id, count.value, price.value);
modalOpen.value = false;
}

View File

@@ -2,6 +2,7 @@
import { Modal } from '@/components';
import { MarketType, MarketTypeLabel } from '@/market';
import { ref } from 'vue';
import { AcquiredType } from './AcquiredType';
import { useAcquiredTypesStore } from './acquisition';
@@ -10,21 +11,21 @@ const acquiredTypesStore = useAcquiredTypesStore();
const modalOpen = ref<boolean>(false);
const type = ref<MarketType>();
const count = ref(1);
const id = ref<number>();
const open = (t: MarketType) => {
type.value = t;
const open = (t: AcquiredType) => {
id.value = t.id;
type.value = t.type;
count.value = 1;
modalOpen.value = true;
}
const remove = () => {
const id = type.value?.id;
if (!id) {
if (!id.value) {
modalOpen.value = false;
return;
}
acquiredTypesStore.removeType(id, count.value);
acquiredTypesStore.removeAcquiredType(id.value, count.value);
modalOpen.value = false;
}

View File

@@ -1,36 +1,41 @@
import { marbasAxiosInstance } from "@/service";
import { marbasAxiosInstance, MarbasObject } from "@/marbas";
import log from "loglevel";
import { defineStore } from "pinia";
import { computed, ref } from "vue";
export type AcquiredMarketType = {
id: number;
export type AcquiredTypeSource = 'bo' | 'so' | 'prod';
export type MarbasAcquiredType = MarbasObject & {
type: number;
quantity: number;
remaining: number;
price: number;
date: Date;
source: 'bo' | 'so' | 'prod';
source: AcquiredTypeSource;
user: number;
}
const endpoint = '/api/acquisitions/';
export const useAcquiredTypesStore = defineStore('market-acquisition', () => {
const acquiredTypes = ref<AcquiredMarketType[]>([]);
const acquiredTypes = ref<MarbasAcquiredType[]>([]);
const types = computed(() => acquiredTypes.value.filter(item => item.remaining > 0));
const addType = async (type: number, quantity: number, price: number) => {
acquiredTypes.value = [...acquiredTypes.value, (await marbasAxiosInstance.post<AcquiredMarketType>(endpoint, {
const addAcquiredType = async (type: number, quantity: number, price: number, source?: AcquiredTypeSource) => {
const newItem = (await marbasAxiosInstance.post<MarbasAcquiredType>(endpoint, {
type: type,
quantity: quantity,
remaining: quantity,
price: price,
date: new Date(),
source: 'bo'
})).data];
source: source ?? 'bo',
})).data
acquiredTypes.value = [...acquiredTypes.value, newItem];
log.info(`Acquired type ${newItem.id} with quantity ${newItem.quantity} and price ${newItem.price}`, newItem);
};
const removeType = async (type: number, quantity: number) => {
const found = acquiredTypes.value.find(item => item.type === type);
const removeAcquiredType = async (id: number, quantity: number) => {
const found = acquiredTypes.value.find(t => t.id === id);
if (!found) {
return;
@@ -41,21 +46,18 @@ export const useAcquiredTypesStore = defineStore('market-acquisition', () => {
remaining: Math.max(0, found.remaining - quantity)
};
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;
}
});
}
acquiredTypes.value = acquiredTypes.value.map(i => {
if (i.id === item.id) {
return item;
} else {
return i;
}
});
await marbasAxiosInstance.put(`${endpoint}${item.id}/`, item);
log.info(`Acquired type ${item.id} remaining: ${item.remaining}`, item);
};
marbasAxiosInstance.get<AcquiredMarketType[]>(endpoint).then(res => acquiredTypes.value = res.data);
marbasAxiosInstance.get<MarbasAcquiredType[]>(endpoint).then(res => acquiredTypes.value = res.data);
return { types, addType, removeType };
return { acquiredTypes: types, addAcquiredType, removeAcquiredType };
});

View File

@@ -1,5 +1,6 @@
import { MarketOrderHistory, MarketType, MarketTypePrice, getHistory, jitaId } from "@/market";
import { marbasAxiosInstance } from "@/service";
import { marbasAxiosInstance, MarbasObject } from "@/marbas";
import { getHistory, jitaId, MarketOrderHistory, MarketType, MarketTypePrice } from "@/market";
import log from "loglevel";
import { defineStore } from "pinia";
import { computed, ref } from "vue";
@@ -11,22 +12,22 @@ export type TrackingResult = {
orderCount: number,
}
type MarketTracking = {
id: number,
export type MarbasTrackedType = MarbasObject & {
type: number
};
const endpoint = '/api/types_tracking/';
export const useMarketTrackingStore = defineStore('marketTracking', () => {
const trackedTypes = ref<MarketTracking[]>([]);
const trackedTypes = ref<MarbasTrackedType[]>([]);
const types = computed(() => trackedTypes.value.map(item => item.type) ?? []);
const addType = async (type: number) => {
const found = trackedTypes.value.find(item => item.type === type);
if (!found) {
trackedTypes.value = [...trackedTypes.value, (await marbasAxiosInstance.post<MarketTracking>(endpoint, { type })).data];
trackedTypes.value = [...trackedTypes.value, (await marbasAxiosInstance.post<MarbasTrackedType>(endpoint, { type })).data];
log.info(`Tracking type ${type}`);
}
}
const removeType = async (type: number) => {
@@ -38,9 +39,10 @@ export const useMarketTrackingStore = defineStore('marketTracking', () => {
trackedTypes.value = trackedTypes.value.filter(t => t.id !== found.id);
await marbasAxiosInstance.delete(`${endpoint}${found.id}`);
log.info(`Stopped tracking type ${type}`);
}
marbasAxiosInstance.get<MarketTracking[]>(endpoint).then(res => trackedTypes.value = res.data);
marbasAxiosInstance.get<MarbasTrackedType[]>(endpoint).then(res => trackedTypes.value = res.data);
return { types, addType, removeType };
});

View File

@@ -1,4 +1,4 @@
import { marbasAxiosInstance } from "@/service";
import { marbasAxiosInstance } from "@/marbas";
export type MarketType = {
id: number;

View File

@@ -11,7 +11,7 @@ const apraisalStore = useApraisalStore();
const acquiredTypesStore = useAcquiredTypesStore();
const items = ref<AcquiredType[]>([]);
watch(() => acquiredTypesStore.types, async itms => {
watch(() => acquiredTypesStore.acquiredTypes, async itms => {
if (itms.length === 0) {
return;
}
@@ -35,7 +35,7 @@ watch(() => acquiredTypesStore.types, async itms => {
<template>
<div class="mt-4">
<template v-if="items.length > 0">
<AcquisitionResultTable :items="items" @buy="(type, price, buy, sell) => buyModal?.open(type, { 'Price': price, 'Buy': buy, 'Sell': sell })" @sell="type => sellModal?.open(type)" />
<AcquisitionResultTable :items="items" @buy="(type, price, buy, sell) => buyModal?.open(type.type, { 'Price': price, 'Buy': buy, 'Sell': sell })" @sell="type => sellModal?.open(type)" />
<BuyModal ref="buyModal" />
<SellModal ref="sellModal" />
</template>

View File

@@ -26,7 +26,7 @@ const isTracked = computed(() => item.value ? marketTrackingStore.types.includes
const acquisitions = computed(() => {
const p = price.value;
return !p ?[] : acquiredTypesStore.types
return !p ?[] : acquiredTypesStore.acquiredTypes
.filter(t => t.type === item.value?.id)
.map(i => ({
...i,

View File

@@ -1,4 +1,4 @@
import { marbasAxiosInstance } from "@/service";
import { marbasAxiosInstance } from "@/marbas";
export type ReprocessItemValues = {
typeID: number;

View File

@@ -1,4 +1,3 @@
import { useAuthStore } from '@/auth';
import axios, { AxiosInstance } from 'axios';
import log from 'loglevel';
@@ -15,48 +14,6 @@ export const logResource = (a: AxiosInstance) => {
});
}
export const marbasAxiosInstance = axios.create({
baseURL: import.meta.env.VITE_MARBAS_URL,
headers: {
'Accept': 'application/json',
"Content-Type": "application/json",
},
})
marbasAxiosInstance.interceptors.request.use(r => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error("Not logged in");
}
const accessToken = authStore.accessToken;
if (accessToken) {
r.headers.Authorization = `Bearer ${accessToken}`;
}
if (!r.params?.page_size) {
r.params = { ...r.params, page_size: 250 };
}
return r;
})
logResource(marbasAxiosInstance)
marbasAxiosInstance.interceptors.response.use(async r => {
const next = r.data?.next;
let results = r.data?.results;
if (next) {
results = results.concat((await marbasAxiosInstance.request({
...r.config,
url: next,
baseURL: '',
})).data);
}
if (results) {
r.data = results;
}
return r;
})
export const esiAxiosInstance = axios.create({
baseURL: import.meta.env.VITE_ESI_URL,
headers: {