Optimize application flow (#196)
Co-authored-by: AceDataCloud <office@acedata.cloud>
This commit is contained in:
1 parent
a37f796c81
commit
8ad9637b86
59 files changed
+1573
-1380
No files matched your search
+119
-122
@@ -1,36 +1,30 @@
|
||||
import { applicationOperator, chatOperator, serviceOperator } from '@/operators';
|
||||
import { applicationOperator, chatOperator, credentialOperator, serviceOperator } from '@/operators';
|
||||
import { IRootState } from '../common/models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IChatState } from './models';
|
||||
import {
|
||||
IApplication,
|
||||
IApplicationType,
|
||||
IChatConversation,
|
||||
IChatModel,
|
||||
IChatModelGroup,
|
||||
ICredential,
|
||||
IService,
|
||||
Status
|
||||
} from '@/models';
|
||||
import { IApplication, IChatConversation, IChatModel, IChatModelGroup, ICredential, IService, Status } from '@/models';
|
||||
import { CHAT_SERVICE_ID } from '@/constants';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IChatState, IRootState>): void => {
|
||||
commit('resetAll');
|
||||
};
|
||||
|
||||
export const setModel = async ({ commit }: any, payload: IChatModel): Promise<void> => {
|
||||
commit('setModel', payload);
|
||||
};
|
||||
|
||||
export const setModelGroup = async ({ commit }: any, payload: IChatModelGroup): Promise<void> => {
|
||||
commit('setModelGroup', payload);
|
||||
};
|
||||
|
||||
export const setApplication = async ({ commit }: any, payload: IApplication): Promise<void> => {
|
||||
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);
|
||||
commit('setCredential', credential);
|
||||
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> => {
|
||||
@@ -43,16 +37,86 @@ export const setService = async ({ commit }: any, payload: IService): Promise<vo
|
||||
commit('setService', payload);
|
||||
};
|
||||
|
||||
export const setConversations = async ({ commit }: any, payload: IChatConversation[]): Promise<void> => {
|
||||
console.debug('set conversations', payload);
|
||||
commit('setConversations', 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<IChatState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(CHAT_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<IChatState, 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: CHAT_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 setModel = async ({ commit }: any, payload: IChatModel): Promise<void> => {
|
||||
commit('setModel', payload);
|
||||
};
|
||||
|
||||
export const setModelGroup = async ({ commit }: any, payload: IChatModelGroup): Promise<void> => {
|
||||
commit('setModelGroup', payload);
|
||||
};
|
||||
|
||||
export const setConversation = async ({ commit, state }: any, payload: IChatConversation): Promise<void> => {
|
||||
console.debug('set conversation', payload);
|
||||
const conversations = state.conversations || [];
|
||||
@@ -66,72 +130,9 @@ export const setConversation = async ({ commit, state }: any, payload: IChatConv
|
||||
console.debug('set conversation success', conversations);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IChatState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for chat');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(CHAT_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 getApplications = async ({
|
||||
commit,
|
||||
state,
|
||||
rootState
|
||||
}: ActionContext<IChatState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for chat');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: CHAT_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 setConversations = async ({ commit }: any, payload: IChatConversation[]): Promise<void> => {
|
||||
console.debug('set conversations', payload);
|
||||
commit('setConversations', payload);
|
||||
};
|
||||
|
||||
export const getConversations = async ({
|
||||
@@ -139,40 +140,36 @@ export const getConversations = async ({
|
||||
state,
|
||||
rootState
|
||||
}: ActionContext<IChatState, IRootState>): Promise<IChatConversation[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getConversations = Status.Request;
|
||||
const credential = state.credential;
|
||||
console.debug('credential', credential);
|
||||
const token = credential?.token;
|
||||
if (!token) {
|
||||
state.status.getConversations = Status.Error;
|
||||
return reject(new Error('Token not found'));
|
||||
}
|
||||
chatOperator
|
||||
.getConversations(
|
||||
{
|
||||
userId: rootState.user?.id
|
||||
},
|
||||
{
|
||||
token
|
||||
}
|
||||
)
|
||||
.then((response) => {
|
||||
console.debug('get conversations success', response.data?.items?.length);
|
||||
commit('setConversations', response.data.items);
|
||||
const conversations = response.data.items;
|
||||
resolve(conversations);
|
||||
})
|
||||
.catch((error) => {
|
||||
state.status.getConversations = Status.Error;
|
||||
commit('setConversations', []);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
state.status.getConversations = Status.Request;
|
||||
const credential = state.credential;
|
||||
console.debug('credential', credential);
|
||||
const token = credential?.token;
|
||||
if (!token) {
|
||||
state.status.getConversations = Status.Error;
|
||||
return [];
|
||||
}
|
||||
try {
|
||||
const { data } = await chatOperator.getConversations(
|
||||
{
|
||||
userId: rootState.user?.id
|
||||
},
|
||||
{
|
||||
token
|
||||
}
|
||||
);
|
||||
console.debug('get conversations success', data?.items?.length);
|
||||
commit('setConversations', data.items);
|
||||
return data.items;
|
||||
} catch (error) {
|
||||
state.status.getConversations = Status.Error;
|
||||
commit('setConversations', []);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
resetAll,
|
||||
createCredential,
|
||||
setModel,
|
||||
setModelGroup,
|
||||
getService,
|
||||
|
||||
@@ -9,19 +9,36 @@ import {
|
||||
IChatdocRepository,
|
||||
ICredential,
|
||||
IService,
|
||||
Status,
|
||||
IApplicationType
|
||||
Status
|
||||
} from '@/models';
|
||||
import { chatdocOperator, applicationOperator, serviceOperator } from '@/operators';
|
||||
import { chatdocOperator, applicationOperator, serviceOperator, credentialOperator } from '@/operators';
|
||||
import { CHATDOC_SERVICE_ID } from '@/constants';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const setApplication = async ({ commit }: any, payload: IApplication): Promise<void> => {
|
||||
commit('setApplication', payload);
|
||||
export const resetAll = ({ commit }: ActionContext<IChatdocState, 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 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> => {
|
||||
@@ -29,61 +46,76 @@ export const setService = async ({ commit }: any, payload: IService): Promise<vo
|
||||
commit('setService', payload);
|
||||
};
|
||||
|
||||
export const setRepository = async ({ commit }: any, payload: { repository: IChatdocRepository }): Promise<void> => {
|
||||
commit('setRepository', 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<IChatdocState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(CHATDOC_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<IChatdocState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for chatdoc');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: CHATDOC_SERVICE_ID
|
||||
})
|
||||
.then((response) => {
|
||||
console.debug('get applications success', response?.data);
|
||||
state.status.getApplications = Status.Success;
|
||||
// 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 resetAll = ({ commit }: ActionContext<IChatdocState, IRootState>): void => {
|
||||
commit('resetAll');
|
||||
}: ActionContext<IChatdocState, 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: CHATDOC_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 getRepositories = async ({
|
||||
@@ -152,24 +184,6 @@ export const deleteDocument = async (
|
||||
return document;
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IChatdocState, IRootState>): Promise<IService> => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
console.debug('start to get service for chatdoc');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(CHATDOC_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 createRepository = async (
|
||||
{ state }: ActionContext<IChatdocState, IRootState>,
|
||||
payload: {
|
||||
@@ -317,7 +331,12 @@ export const getRepository = async (
|
||||
return repository;
|
||||
};
|
||||
|
||||
export const setRepository = async ({ commit }: any, payload: { repository: IChatdocRepository }): Promise<void> => {
|
||||
commit('setRepository', payload);
|
||||
};
|
||||
|
||||
export default {
|
||||
createCredential,
|
||||
setService,
|
||||
setConversation,
|
||||
getService,
|
||||
|
||||
@@ -44,7 +44,7 @@ export const setFingerprint = ({ commit }: ActionContext<IRootState, IRootState>
|
||||
commit('setFingerprint', payload);
|
||||
};
|
||||
|
||||
export const getUser = async ({ commit }: ActionContext<IRootState, IRootState>): Promise<IUser> => {
|
||||
export const getUser = async ({ commit }: ActionContext<IRootState, IRootState>): Promise<IUser | undefined> => {
|
||||
console.debug('start to get user');
|
||||
try {
|
||||
commit('resetUser');
|
||||
@@ -54,6 +54,7 @@ export const getUser = async ({ commit }: ActionContext<IRootState, IRootState>)
|
||||
return user;
|
||||
} catch (error) {
|
||||
console.error('get user failed', error);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+88
-75
@@ -1,23 +1,37 @@
|
||||
import { applicationOperator, fluxOperator, serviceOperator } from '@/operators';
|
||||
import { applicationOperator, credentialOperator, fluxOperator, serviceOperator } from '@/operators';
|
||||
import { IFluxState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, ICredential, IFluxConfig, IFluxTask, IService, IApplicationType } from '@/models';
|
||||
import { IApplication, ICredential, IFluxConfig, IFluxTask, IService } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { FLUX_SERVICE_ID } from '@/constants';
|
||||
import { mergeAndSortLists } from '@/utils/merge';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IFluxState, 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: IFluxConfig) => {
|
||||
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,80 @@ 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<IFluxState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(FLUX_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<IFluxState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for flux');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: FLUX_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<IFluxState, 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: FLUX_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 setConfig = ({ commit }: any, payload: IFluxConfig) => {
|
||||
commit('setConfig', payload);
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
@@ -101,24 +131,6 @@ export const setTasksActive = ({ commit }: any, payload: IFluxTask) => {
|
||||
commit('setTasksActive', payload);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IFluxState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for flux');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(FLUX_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<IFluxState, IRootState>,
|
||||
{
|
||||
@@ -167,6 +179,7 @@ export const getTasks = async (
|
||||
};
|
||||
|
||||
export default {
|
||||
createCredential,
|
||||
setService,
|
||||
getService,
|
||||
resetAll,
|
||||
|
||||
@@ -1 +1 @@
|
||||
export default ['flux.credential', 'flux.application'];
|
||||
export default ['flux.credential', 'flux.application', 'flux.applications'];
|
||||
+89
-76
@@ -1,23 +1,37 @@
|
||||
import { applicationOperator, hailuoOperator, serviceOperator } from '@/operators';
|
||||
import { applicationOperator, credentialOperator, hailuoOperator, serviceOperator } from '@/operators';
|
||||
import { IHailuoState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, ICredential, IHailuoConfig, IHailuoTask, IService, IApplicationType } from '@/models';
|
||||
import { IApplication, ICredential, IHailuoConfig, IHailuoTask, IService } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { HAILUO_SERVICE_ID } from '@/constants';
|
||||
import { mergeAndSortLists } from '@/utils/merge';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IHailuoState, 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: IHailuoConfig) => {
|
||||
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,80 @@ 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<IHailuoState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(HAILUO_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<IHailuoState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for hailuo');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: HAILUO_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<IHailuoState, 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: HAILUO_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 setConfig = ({ commit }: any, payload: IHailuoConfig) => {
|
||||
commit('setConfig', payload);
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
@@ -101,24 +131,6 @@ export const setTasksActive = ({ commit }: any, payload: IHailuoTask) => {
|
||||
commit('setTasksActive', payload);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IHailuoState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for hailuo');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(HAILUO_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<IHailuoState, IRootState>,
|
||||
{
|
||||
@@ -179,5 +191,6 @@ export default {
|
||||
setTasksItems,
|
||||
setTasksTotal,
|
||||
setTasksActive,
|
||||
getTasks
|
||||
getTasks,
|
||||
createCredential
|
||||
};
|
||||
@@ -1 +1 @@
|
||||
export default ['hailuo.credential', 'hailuo.application'];
|
||||
export default ['hailuo.credential', 'hailuo.application', 'hailuo.applications'];
|
||||
@@ -1,23 +1,37 @@
|
||||
import { applicationOperator, headshotsOperator, serviceOperator } from '@/operators';
|
||||
import { applicationOperator, credentialOperator, headshotsOperator, serviceOperator } from '@/operators';
|
||||
import { IHeadshotsState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, ICredential, IHeadshotsConfig, IHeadshotsTask, IService, IApplicationType } from '@/models';
|
||||
import { IApplication, ICredential, IHeadshotsConfig, IHeadshotsTask, IService } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { HEADSHOTS_SERVICE_ID } from '@/constants';
|
||||
import { mergeAndSortLists } from '@/utils/merge';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IHeadshotsState, 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: IHeadshotsConfig) => {
|
||||
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,80 @@ 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<IHeadshotsState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(HEADSHOTS_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<IHeadshotsState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for headshots');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: HEADSHOTS_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<IHeadshotsState, 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: HEADSHOTS_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 setConfig = ({ commit }: any, payload: IHeadshotsConfig) => {
|
||||
commit('setConfig', payload);
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
@@ -101,24 +131,6 @@ export const setTasksActive = ({ commit }: any, payload: IHeadshotsTask) => {
|
||||
commit('setTasksActive', payload);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IHeadshotsState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for headshots');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(HEADSHOTS_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<IHeadshotsState, IRootState>,
|
||||
{
|
||||
@@ -178,5 +190,6 @@ export default {
|
||||
setTasksItems,
|
||||
setTasksTotal,
|
||||
setTasksActive,
|
||||
getTasks
|
||||
getTasks,
|
||||
createCredential
|
||||
};
|
||||
@@ -1 +1 @@
|
||||
export default ['headshots.credential', 'headshots.application'];
|
||||
export default ['headshots.credential', 'headshots.application', 'headshots.applications'];
|
||||
+90
-76
@@ -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 @@
|
||||
export default ['luma.credential', 'luma.application'];
|
||||
export default ['luma.credential', 'luma.application', 'luma.applications'];
|
||||
+104
-91
@@ -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
|
||||
};
|
||||
@@ -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'
|
||||
];
|
||||
+89
-76
@@ -1,23 +1,37 @@
|
||||
import { applicationOperator, pikaOperator, serviceOperator } from '@/operators';
|
||||
import { applicationOperator, credentialOperator, pikaOperator, serviceOperator } from '@/operators';
|
||||
import { IPikaState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, ICredential, IPikaConfig, IPikaTask, IService, IApplicationType } from '@/models';
|
||||
import { IApplication, ICredential, IPikaConfig, IPikaTask, IService } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { PIKA_SERVICE_ID } from '@/constants';
|
||||
import { mergeAndSortLists } from '@/utils/merge';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IPikaState, 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: IPikaConfig) => {
|
||||
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,80 @@ 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<IPikaState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(PIKA_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<IPikaState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for pika');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: PIKA_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<IPikaState, 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: PIKA_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 setConfig = ({ commit }: any, payload: IPikaConfig) => {
|
||||
commit('setConfig', payload);
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
@@ -101,24 +131,6 @@ export const setTasksActive = ({ commit }: any, payload: IPikaTask) => {
|
||||
commit('setTasksActive', payload);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IPikaState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for pika');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(PIKA_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<IPikaState, IRootState>,
|
||||
{
|
||||
@@ -179,5 +191,6 @@ export default {
|
||||
setTasksItems,
|
||||
setTasksTotal,
|
||||
setTasksActive,
|
||||
getTasks
|
||||
getTasks,
|
||||
createCredential
|
||||
};
|
||||
@@ -1 +1 @@
|
||||
export default ['pika.credential', 'pika.application'];
|
||||
export default ['pika.credential', 'pika.application', 'pika.applications'];
|
||||
+89
-76
@@ -1,23 +1,37 @@
|
||||
import { applicationOperator, qrartOperator, serviceOperator } from '@/operators';
|
||||
import { applicationOperator, credentialOperator, qrartOperator, serviceOperator } from '@/operators';
|
||||
import { IQrartState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, IApplicationType, ICredential, IQrartConfig, IQrartTask, IService } from '@/models';
|
||||
import { IApplication, ICredential, IQrartConfig, IQrartTask, IService } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { QRART_SERVICE_ID } from '@/constants';
|
||||
import { mergeAndSortLists } from '@/utils/merge';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IQrartState, 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: IQrartConfig) => {
|
||||
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,80 @@ 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<IQrartState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(QRART_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<IQrartState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for qrart');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: QRART_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<IQrartState, 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: QRART_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 setConfig = ({ commit }: any, payload: IQrartConfig) => {
|
||||
commit('setConfig', payload);
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
@@ -101,24 +131,6 @@ export const setTasksActive = ({ commit }: any, payload: IQrartTask) => {
|
||||
commit('setTasksActive', payload);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IQrartState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for qrart');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(QRART_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<IQrartState, IRootState>,
|
||||
{
|
||||
@@ -178,5 +190,6 @@ export default {
|
||||
setTasksItems,
|
||||
setTasksTotal,
|
||||
setTasksActive,
|
||||
getTasks
|
||||
getTasks,
|
||||
createCredential
|
||||
};
|
||||
@@ -1 +1 @@
|
||||
export default ['qrart.config', 'qrart.credential', 'qrart.application'];
|
||||
export default ['qrart.config', 'qrart.credential', 'qrart.application', 'qrart.applications'];
|
||||
+89
-76
@@ -1,23 +1,37 @@
|
||||
import { applicationOperator, sunoOperator, serviceOperator } from '@/operators';
|
||||
import { applicationOperator, sunoOperator, serviceOperator, credentialOperator } from '@/operators';
|
||||
import { ISunoState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, ICredential, ISunoConfig, ISunoTask, IService, Song, IApplicationType } from '@/models';
|
||||
import { IApplication, ICredential, ISunoConfig, ISunoTask, IService } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { SUNO_SERVICE_ID } from '@/constants';
|
||||
import { mergeAndSortLists } from '@/utils/merge';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<ISunoState, 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: ISunoConfig) => {
|
||||
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,80 @@ 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<ISunoState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(SUNO_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<ISunoState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for suno');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: SUNO_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<ISunoState, 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: SUNO_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 setConfig = ({ commit }: any, payload: ISunoConfig) => {
|
||||
commit('setConfig', payload);
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
@@ -105,24 +135,6 @@ export const setAudio = ({ commit }: any, payload: any) => {
|
||||
commit('setAudio', payload);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<ISunoState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for suno');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(SUNO_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<ISunoState, IRootState>,
|
||||
{
|
||||
@@ -184,5 +196,6 @@ export default {
|
||||
setTasksTotal,
|
||||
setTasksActive,
|
||||
getTasks,
|
||||
setAudio
|
||||
setAudio,
|
||||
createCredential
|
||||
};
|
||||
@@ -1 +1 @@
|
||||
export default ['suno.credential', 'suno.application'];
|
||||
export default ['suno.credential', 'suno.application', 'suno.applications'];
|
||||
Reference in new issue
Block a user