import { defineStore } from 'pinia' import { postFunction } from '@/api/supplied-service' const USER_KEY = 'joyful-selection-user' const DEFAULT_SUPPLIER_IDENTIFICATION = (process.env.VUE_APP_SUPPLIER_IDENTIFICATION || '').toLowerCase() let bridgeTokenPromise = null const readUser = () => { try { return JSON.parse(localStorage.getItem(USER_KEY) || '{}') } catch (error) { return {} } } const saveUser = data => { localStorage.setItem(USER_KEY, JSON.stringify(data)) } export const useUserStore = defineStore('joyfulSelectionUser', { state: () => ({ userId: '', supplierIdentification: '', supplierId: '', supplierName: '', token: '', refreshToken: '', tokenExpiresAt: 0, pointsBalance: 0, loaded: false, loadingToken: false }), getters: { hasToken: state => Boolean(state.token && state.tokenExpiresAt > Date.now()) }, actions: { loadUser() { const data = readUser() this.userId = data.userId || '' const supplierIdentification = DEFAULT_SUPPLIER_IDENTIFICATION || data.supplierIdentification || '' const supplierChanged = Boolean(data.supplierIdentification && supplierIdentification !== data.supplierIdentification) this.supplierIdentification = supplierIdentification this.supplierId = supplierChanged ? '' : data.supplierId || '' this.supplierName = supplierChanged ? '' : data.supplierName || '' this.token = supplierChanged ? '' : data.token || '' this.refreshToken = supplierChanged ? '' : data.refreshToken || '' this.tokenExpiresAt = supplierChanged ? 0 : Number(data.tokenExpiresAt || 0) this.pointsBalance = supplierChanged ? 0 : Number(data.pointsBalance || 0) this.loaded = true }, persistUser() { saveUser({ userId: this.userId, supplierIdentification: this.supplierIdentification, supplierId: this.supplierId, supplierName: this.supplierName, token: this.token, refreshToken: this.refreshToken, tokenExpiresAt: this.tokenExpiresAt, pointsBalance: this.pointsBalance }) this.loaded = true }, hydrateFromRoute(query = {}) { if (!this.loaded) { this.loadUser() } const nextUserId = query.userId || '' if (nextUserId && nextUserId !== this.userId) { this.userId = nextUserId this.token = '' this.refreshToken = '' this.tokenExpiresAt = 0 this.pointsBalance = 0 } if (!this.supplierIdentification) { this.supplierIdentification = DEFAULT_SUPPLIER_IDENTIFICATION } this.persistUser() }, async fetchSupplierInfo() { if (!this.loaded) { this.loadUser() } if (!this.supplierIdentification) { return } const data = await postFunction('supplier-info', { supplierIdentification: this.supplierIdentification }) if (data?.supplierId && data.supplierId !== this.supplierId) { this.token = '' this.refreshToken = '' this.tokenExpiresAt = 0 } this.supplierId = data?.supplierId || '' this.supplierName = data?.supplierName || '' this.persistUser() }, async fetchBridgeToken() { if (!this.loaded) { this.loadUser() } if (!this.userId || !this.supplierIdentification || this.hasToken) { return } if (this.loadingToken && bridgeTokenPromise) { return bridgeTokenPromise } this.loadingToken = true bridgeTokenPromise = (async () => { if (!this.supplierId) { await this.fetchSupplierInfo() } const data = await postFunction('supplier-token', { userId: this.userId, supplierIdentification: this.supplierIdentification }) this.supplierId = data?.supplierId || this.supplierId this.supplierName = data?.supplierName || this.supplierName this.token = data?.token || data?.accessToken || '' this.refreshToken = data?.refreshToken || '' const expiresIn = Number(data?.expiresIn || 0) this.tokenExpiresAt = this.token && expiresIn > 0 ? Date.now() + expiresIn * 1000 : 0 this.persistUser() })() try { await bridgeTokenPromise } finally { this.loadingToken = false bridgeTokenPromise = null } }, async fetchUserPoints() { if (!this.loaded) { this.loadUser() } await this.fetchBridgeToken() if (!this.userId || !this.supplierId || !this.token) { return this.pointsBalance } const data = await postFunction('supplier-points', { userId: this.userId, supplierId: this.supplierId, token: this.token }) this.pointsBalance = Number(data?.userPoints || 0) this.persistUser() return this.pointsBalance } } })