Introduce initial version of the Lonc app with core features, styling, and configurations.
- Add base app structure, including Bootstrap setup and Alpine.js integration. - Implement authentication flow with session handling. - Integrate stock management and label creation functionalities. - Include responsive styling and theme using CSS variables and custom components. - Add API clients for Tryton-based backend. - Set up kitchen and dashboard navigation workflows. - Configure service worker for PWA support.
This commit is contained in:
+111
@@ -0,0 +1,111 @@
|
||||
import { CONNECTION_STATES, TRYTON_APPLICATION } from '../app/config.js';
|
||||
import { apiRequest, getPath } from './client.js';
|
||||
import { listKitchens } from './kitchens.js';
|
||||
|
||||
function extractKey(payload) {
|
||||
if (typeof payload === 'string') {
|
||||
return payload;
|
||||
}
|
||||
|
||||
return payload?.key || payload?.application_key || payload?.data?.key || null;
|
||||
}
|
||||
|
||||
function isAuthFailure(error) {
|
||||
const status = error?.cause?.status || error?.status;
|
||||
return status === 401 || status === 403;
|
||||
}
|
||||
|
||||
export async function login(store, credentials) {
|
||||
const payload = await apiRequest(store, getPath('userApplication'), {
|
||||
method: 'POST',
|
||||
body: {
|
||||
user: credentials.userLogin,
|
||||
application: TRYTON_APPLICATION,
|
||||
},
|
||||
includeKitchen: false,
|
||||
});
|
||||
|
||||
const applicationKey = extractKey(payload);
|
||||
if (!applicationKey) {
|
||||
throw new Error('User application creation did not return a key.');
|
||||
}
|
||||
|
||||
const session = {
|
||||
userLogin: credentials.userLogin,
|
||||
applicationKey,
|
||||
state: CONNECTION_STATES.pendingValidation,
|
||||
hasValidated: false,
|
||||
};
|
||||
store.setSession(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function restoreSession(store) {
|
||||
if (!store.session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
await verifyConnection(store);
|
||||
} catch (error) {
|
||||
if (!isAuthFailure(error)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return store.session;
|
||||
}
|
||||
|
||||
export async function logout(store) {
|
||||
try {
|
||||
if (store.session?.applicationKey && store.session?.userLogin) {
|
||||
await apiRequest(store, getPath('userApplication'), {
|
||||
method: 'DELETE',
|
||||
body: {
|
||||
user: store.session.userLogin,
|
||||
key: store.session.applicationKey,
|
||||
application: TRYTON_APPLICATION,
|
||||
},
|
||||
includeKitchen: false,
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
store.clearSessionState();
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyConnection(store) {
|
||||
if (!store.session?.applicationKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
await listKitchens(store);
|
||||
store.setSession({
|
||||
...store.session,
|
||||
state: CONNECTION_STATES.connected,
|
||||
hasValidated: true,
|
||||
});
|
||||
return store.session;
|
||||
} catch (error) {
|
||||
if (!isAuthFailure(error)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
store.setSession({
|
||||
...store.session,
|
||||
state: store.session.hasValidated
|
||||
? CONNECTION_STATES.invalidKey
|
||||
: CONNECTION_STATES.pendingValidation,
|
||||
});
|
||||
|
||||
throw new Error(
|
||||
store.session.hasValidated
|
||||
? 'The stored application key is no longer valid.'
|
||||
: 'The application key is still waiting for validation in Tryton preferences.',
|
||||
{
|
||||
cause: error.cause || error,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user