Optimize application flow (#196)

Co-authored-by: AceDataCloud <office@acedata.cloud>
This commit is contained in:
GermeyandAceDataCloud authored and GitHub committed 2025-03-03 00:57:36 +08:00
1 parent a37f796c81
commit 8ad9637b86
59 files changed
+1573 -1380

No files matched your search

+90 -76
View File
@@ -1,23 +1,37 @@
import { applicationOperator, lumaOperator, serviceOperator } from '@/operators';
import { applicationOperator, credentialOperator, lumaOperator, serviceOperator } from '@/operators';
import { ILumaState } from './models';
import { ActionContext } from 'vuex';
import { IRootState } from '../common/models';
import { IApplication, ICredential, ILumaConfig, ILumaTask, IService, IApplicationType } from '@/models';
import { IApplication, ICredential, ILumaConfig, ILumaTask, IService } from '@/models';
import { Status } from '@/models/common';
import { LUMA_SERVICE_ID } from '@/constants';
import { mergeAndSortLists } from '@/utils/merge';
import { getFinalApplication } from '@/utils';
export const resetAll = ({ commit }: ActionContext<ILumaState, IRootState>): void => {
commit('resetAll');
};
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
console.debug('set credential', payload);
commit('setCredential', payload);
export const setApplication = async ({ commit, dispatch }: any, payload: IApplication): Promise<void> => {
console.debug('set application', payload);
commit('setApplication', payload);
if (!payload) {
console.debug('application is null, return');
return;
}
const credential = payload?.credentials?.find((credential) => credential?.host === window.location.origin);
if (credential) {
console.debug('credential exists, set credential', credential);
commit('setCredential', credential);
} else {
console.debug('credential not exists, start to create credential for application', payload);
await dispatch('createCredential');
}
};
export const setConfig = ({ commit }: any, payload: ILumaConfig) => {
commit('setConfig', payload);
export const setApplications = async ({ commit }: any, payload: IApplication[]): Promise<void> => {
console.debug('set applications', payload);
commit('setApplications', payload);
};
export const setService = async ({ commit }: any, payload: IService): Promise<void> => {
@@ -25,64 +39,81 @@ export const setService = async ({ commit }: any, payload: IService): Promise<vo
commit('setService', payload);
};
export const setApplication = ({ commit }: any, payload: IApplication) => {
commit('setApplication', payload);
const credential = payload?.credentials?.find((credential) => credential?.host === window.location.origin);
commit('setCredential', credential);
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
console.debug('set credential', payload);
commit('setCredential', payload);
};
export const setApplications = ({ commit }: any, payload: IApplication[]) => {
commit('setApplications', payload);
export const createCredential = async ({ commit, state }: any): Promise<ICredential | undefined> => {
const application = state.application;
console.debug('prepare to create credential for application', application);
if (!application) {
console.error('Application not found');
return undefined;
}
console.debug('creating create credential for application', application);
const { data: credential } = await credentialOperator.create({
application_id: application?.id,
host: window.location.origin
});
console.debug('created credential success', credential);
commit('setCredential', credential);
console.debug('end createCredential');
return credential;
};
export const getService = async ({
commit,
state
}: ActionContext<ILumaState, IRootState>): Promise<IService | undefined> => {
state.status.getService = Status.Request;
try {
const { data: service } = await serviceOperator.get(LUMA_SERVICE_ID);
state.status.getService = Status.Success;
commit('setService', service);
return service;
} catch (error) {
state.status.getService = Status.Error;
commit('setService', undefined);
}
};
export const getApplications = async ({
commit,
state,
rootState
}: ActionContext<ILumaState, IRootState>): Promise<IApplication[]> => {
console.debug('start to get applications for luma');
return new Promise((resolve, reject) => {
state.status.getApplications = Status.Request;
applicationOperator
.getAll({
user_id: rootState?.user?.id,
service_id: LUMA_SERVICE_ID
})
.then((response) => {
console.debug('get applications success', response?.data);
state.status.getApplications = Status.Success;
commit('setApplications', response.data.items);
// check if there is any application with 'Period' type
const application = response.data.items?.find((application) => application?.type === IApplicationType.PERIOD);
const application2 = response.data.items?.find((application) => application?.type === IApplicationType.USAGE);
if (application && application?.remaining_amount) {
console.debug('set application with Period', application);
commit('setApplication', application);
const credential = application?.credentials?.find(
(credential) => credential?.host === window.location.origin
);
console.debug('set credential with Period', application);
commit('setCredential', credential);
} else if (application2) {
console.debug('set application with Usage', application2);
commit('setApplication', application2);
const credential = application2?.credentials?.find(
(credential) => credential?.host === window.location.origin
);
console.debug('set credential with Usage', application);
commit('setCredential', credential);
} else {
console.debug('set application with null', response.data.items?.[0]);
commit('setApplication', response.data.items?.[0]);
}
resolve(response.data.items);
console.debug('save application success', response.data.items[0]);
})
.catch((error) => {
state.status.getApplications = Status.Error;
reject(error);
});
});
}: ActionContext<ILumaState, IRootState>): Promise<IApplication[] | undefined> => {
console.debug('start to get applications for chat');
state.status.getApplications = Status.Request;
const currentApplication = state.application;
console.debug('current application', currentApplication);
try {
const { data: applications } = await applicationOperator.getAll({
user_id: rootState?.user?.id,
service_id: LUMA_SERVICE_ID
});
state.status.getApplications = Status.Success;
console.debug('get applications success', applications.items);
commit('setApplications', applications.items);
const finalApplication = getFinalApplication(applications.items, currentApplication);
if (finalApplication) {
console.debug('set final application', finalApplication, finalApplication?.type);
commit('setApplication', finalApplication);
} else {
console.debug('set application undefined', undefined);
commit('setApplication', undefined);
}
return applications.items;
} catch (error) {
console.error('get applications failed', error);
state.status.getApplications = Status.Error;
commit('setApplications', undefined);
commit('setApplication', undefined);
}
};
export const setConfig = ({ commit }: any, payload: ILumaConfig) => {
commit('setConfig', payload);
};
export const setTasks = ({ commit }: any, payload: any) => {
@@ -101,24 +132,6 @@ export const setTasksActive = ({ commit }: any, payload: ILumaTask) => {
commit('setTasksActive', payload);
};
export const getService = async ({ commit, state }: ActionContext<ILumaState, IRootState>): Promise<IService> => {
return new Promise((resolve, reject) => {
console.debug('start to get service for luma');
state.status.getService = Status.Request;
serviceOperator
.get(LUMA_SERVICE_ID)
.then((response) => {
state.status.getService = Status.Success;
commit('setService', response.data);
resolve(response.data);
})
.catch((error) => {
state.status.getService = Status.Error;
reject(error);
});
});
};
export const getTasks = async (
{ commit, state, rootState }: ActionContext<ILumaState, IRootState>,
{
@@ -178,5 +191,6 @@ export default {
setTasksItems,
setTasksTotal,
setTasksActive,
getTasks
getTasks,
createCredential
};
+1 -1
View File
@@ -1 +1 @@
export default ['luma.credential', 'luma.application'];
export default ['luma.credential', 'luma.application', 'luma.applications'];