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

+104 -91
View File
@@ -1,16 +1,116 @@
import { applicationOperator, midjourneyOperator, serviceOperator } from '@/operators';
import { applicationOperator, credentialOperator, midjourneyOperator, serviceOperator } from '@/operators';
import { IMidjourneyState } from './models';
import { ActionContext } from 'vuex';
import { IRootState } from '../common/models';
import { IApplication, IApplicationType, ICredential, IMidjourneyConfig, IMidjourneyTask, IService } from '@/models';
import { IApplication, ICredential, IMidjourneyConfig, IMidjourneyTask, IService } from '@/models';
import { Status } from '@/models/common';
import { MIDJOURNEY_SERVICE_ID } from '@/constants';
import { mergeAndSortLists } from '@/utils/merge';
import { getFinalApplication } from '@/utils';
export const resetAll = ({ commit }: ActionContext<IMidjourneyState, IRootState>): void => {
commit('resetAll');
};
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 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> => {
console.debug('set service', payload);
commit('setService', payload);
};
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
console.debug('set credential', payload);
commit('setCredential', 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<IMidjourneyState, IRootState>): Promise<IService | undefined> => {
state.status.getService = Status.Request;
try {
const { data: service } = await serviceOperator.get(MIDJOURNEY_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<IMidjourneyState, 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: MIDJOURNEY_SERVICE_ID
});
state.status.getApplications = Status.Success;
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 setTasksItems = ({ commit }: any, payload: IMidjourneyTask[]) => {
commit('setTasksItems', payload);
};
@@ -19,11 +119,6 @@ export const setTasksTotal = ({ commit }: any, payload: number) => {
commit('setTasksTotal', payload);
};
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
console.debug(setCredential, 'set credential', payload);
commit('setCredential', payload);
};
export const setConfig = ({ commit }: any, payload: IMidjourneyConfig) => {
commit('setConfig', payload);
};
@@ -32,93 +127,10 @@ export const setMode = ({ commit }: any, payload: string) => {
commit('setMode', payload);
};
export const setService = async ({ commit }: any, payload: IService): Promise<void> => {
console.debug('set service', payload);
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 setApplications = ({ commit }: any, payload: IApplication[]) => {
commit('setApplications', payload);
};
export const getApplications = async ({
commit,
state,
rootState
}: ActionContext<IMidjourneyState, IRootState>): Promise<IApplication[]> => {
console.debug('start to get applications for midjourney');
return new Promise((resolve, reject) => {
state.status.getApplications = Status.Request;
applicationOperator
.getAll({
user_id: rootState?.user?.id,
service_id: MIDJOURNEY_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);
});
});
};
export const setTasks = ({ commit }: any, payload: IMidjourneyTask[]) => {
commit('setTasks', payload);
};
export const getService = async ({ commit, state }: ActionContext<IMidjourneyState, IRootState>): Promise<IService> => {
return new Promise((resolve, reject) => {
console.debug('start to get service for midjourney');
state.status.getService = Status.Request;
serviceOperator
.get(MIDJOURNEY_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<IMidjourneyState, IRootState>,
{
@@ -177,5 +189,6 @@ export default {
setApplications,
getApplications,
setTasks,
getTasks
getTasks,
createCredential
};
+7 -1
View File
@@ -1 +1,7 @@
export default ['midjourney.config', 'midjourney.credential', 'midjourney.mode', 'midjourney.application'];
export default [
'midjourney.config',
'midjourney.credential',
'midjourney.mode',
'midjourney.application',
'midjourney.applications'
];