frontend: added subscription processing
This commit is contained in:
@@ -3,10 +3,10 @@ import { toRFC3339 } from './helpers';
|
|||||||
/**
|
/**
|
||||||
* Converts FormData to a nested object structure
|
* Converts FormData to a nested object structure
|
||||||
* @param {FormData} formData - The FormData object to convert
|
* @param {FormData} formData - The FormData object to convert
|
||||||
* @returns {{ user: Partial<App.Locals['user']>,password2: string }} Nested object representation of the form data
|
* @returns {{ object: Partial<App.Locals['user']> | Partial<App.Types['subscription']>, password2: string }} Nested object representation of the form data
|
||||||
*/
|
*/
|
||||||
export function formDataToObject(formData) {
|
export function formDataToObject(formData) {
|
||||||
/** @type { Partial<App.Locals['user']> } */
|
/** @type { Partial<App.Locals['user']> | Partial<App.Types['subscription']> } */
|
||||||
const object = {};
|
const object = {};
|
||||||
let password2 = '';
|
let password2 = '';
|
||||||
|
|
||||||
@@ -56,89 +56,113 @@ export function formDataToObject(formData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { user: object, password2: password2 };
|
return { object: object, password2: password2 };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes the raw form data into the expected user data structure
|
* Processes the raw form data into the expected user data structure
|
||||||
* @param {{ user: Partial<App.Locals['user']>, password2: string} } rawData - The raw form data object
|
* @param {{ object: Partial<App.Locals['user']>, password2: string} } rawData - The raw form data object
|
||||||
* @returns {{ user: Partial<App.Locals['user']> }} Processed user data
|
* @returns {{ user: Partial<App.Locals['user']> }} Processed user data
|
||||||
*/
|
*/
|
||||||
export function processFormData(rawData) {
|
export function processUserFormData(rawData) {
|
||||||
/** @type {{ user: Partial<App.Locals['user']> }} */
|
/** @type {{ user: Partial<App.Locals['user']> }} */
|
||||||
let processedData = {
|
let processedData = {
|
||||||
user: {
|
user: {
|
||||||
id: Number(rawData.user.id) || 0,
|
id: Number(rawData.object.id) || 0,
|
||||||
status: Number(rawData.user.status),
|
status: Number(rawData.object.status),
|
||||||
role_id: Number(rawData.user.role_id),
|
role_id: Number(rawData.object.role_id),
|
||||||
first_name: String(rawData.user.first_name),
|
first_name: String(rawData.object.first_name),
|
||||||
last_name: String(rawData.user.last_name),
|
last_name: String(rawData.object.last_name),
|
||||||
email: String(rawData.user.email),
|
email: String(rawData.object.email),
|
||||||
phone: String(rawData.user.phone || ''),
|
phone: String(rawData.object.phone || ''),
|
||||||
company: String(rawData.user.company || ''),
|
company: String(rawData.object.company || ''),
|
||||||
dateofbirth: toRFC3339(rawData.user.dateofbirth),
|
dateofbirth: toRFC3339(rawData.object.dateofbirth),
|
||||||
address: String(rawData.user.address || ''),
|
address: String(rawData.object.address || ''),
|
||||||
zip_code: String(rawData.user.zip_code || ''),
|
zip_code: String(rawData.object.zip_code || ''),
|
||||||
city: String(rawData.user.city || ''),
|
city: String(rawData.object.city || ''),
|
||||||
notes: String(rawData.user.notes || ''),
|
notes: String(rawData.object.notes || ''),
|
||||||
profile_picture: String(rawData.user.profile_picture || ''),
|
profile_picture: String(rawData.object.profile_picture || ''),
|
||||||
|
|
||||||
membership: {
|
membership: {
|
||||||
id: Number(rawData.user.membership?.id) || 0,
|
id: Number(rawData.object.membership?.id) || 0,
|
||||||
status: Number(rawData.user.membership?.status),
|
status: Number(rawData.object.membership?.status),
|
||||||
start_date: toRFC3339(rawData.user.membership?.start_date),
|
start_date: toRFC3339(rawData.object.membership?.start_date),
|
||||||
end_date: toRFC3339(rawData.user.membership?.end_date),
|
end_date: toRFC3339(rawData.object.membership?.end_date),
|
||||||
parent_member_id: Number(rawData.user.membership?.parent_member_id) || 0,
|
parent_member_id: Number(rawData.object.membership?.parent_member_id) || 0,
|
||||||
subscription_model: {
|
subscription_model: {
|
||||||
id: Number(rawData.user.membership?.subscription_model?.id) || 0,
|
id: Number(rawData.object.membership?.subscription_model?.id) || 0,
|
||||||
name: String(rawData.user.membership?.subscription_model?.name) || '',
|
name: String(rawData.object.membership?.subscription_model?.name) || '',
|
||||||
details: String(rawData.user.membership?.subscription_model?.details) || '',
|
details: String(rawData.object.membership?.subscription_model?.details) || '',
|
||||||
conditions: String(rawData.user.membership?.subscription_model?.conditions) || '',
|
conditions: String(rawData.object.membership?.subscription_model?.conditions) || '',
|
||||||
hourly_rate: Number(rawData.user.membership?.subscription_model?.hourly_rate) || 0,
|
hourly_rate: Number(rawData.object.membership?.subscription_model?.hourly_rate) || 0,
|
||||||
monthly_fee: Number(rawData.user.membership?.subscription_model?.monthly_fee) || 0,
|
monthly_fee: Number(rawData.object.membership?.subscription_model?.monthly_fee) || 0,
|
||||||
included_hours_per_month:
|
included_hours_per_month:
|
||||||
Number(rawData.user.membership?.subscription_model?.included_hours_per_month) || 0,
|
Number(rawData.object.membership?.subscription_model?.included_hours_per_month) || 0,
|
||||||
included_hours_per_year:
|
included_hours_per_year:
|
||||||
Number(rawData.user.membership?.subscription_model?.included_hours_per_year) || 0
|
Number(rawData.object.membership?.subscription_model?.included_hours_per_year) || 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
licence: {
|
licence: {
|
||||||
id: Number(rawData.user.licence?.id) || 0,
|
id: Number(rawData.object.licence?.id) || 0,
|
||||||
status: Number(rawData.user.licence?.status),
|
status: Number(rawData.object.licence?.status),
|
||||||
number: String(rawData.user.licence?.number || ''),
|
number: String(rawData.object.licence?.number || ''),
|
||||||
issued_date: toRFC3339(rawData.user.licence?.issued_date),
|
issued_date: toRFC3339(rawData.object.licence?.issued_date),
|
||||||
expiration_date: toRFC3339(rawData.user.licence?.expiration_date),
|
expiration_date: toRFC3339(rawData.object.licence?.expiration_date),
|
||||||
country: String(rawData.user.licence?.country || ''),
|
country: String(rawData.object.licence?.country || ''),
|
||||||
categories: rawData.user.licence?.categories || []
|
categories: rawData.object.licence?.categories || []
|
||||||
},
|
},
|
||||||
|
|
||||||
bank_account: {
|
bank_account: {
|
||||||
id: Number(rawData.user.bank_account?.id) || 0,
|
id: Number(rawData.object.bank_account?.id) || 0,
|
||||||
account_holder_name: String(rawData.user.bank_account?.account_holder_name || ''),
|
account_holder_name: String(rawData.object.bank_account?.account_holder_name || ''),
|
||||||
bank: String(rawData.user.bank_account?.bank || ''),
|
bank: String(rawData.object.bank_account?.bank || ''),
|
||||||
iban: String(rawData.user.bank_account?.iban || ''),
|
iban: String(rawData.object.bank_account?.iban || ''),
|
||||||
bic: String(rawData.user.bank_account?.bic || ''),
|
bic: String(rawData.object.bank_account?.bic || ''),
|
||||||
mandate_reference: String(rawData.user.bank_account?.mandate_reference || ''),
|
mandate_reference: String(rawData.object.bank_account?.mandate_reference || ''),
|
||||||
mandate_date_signed: toRFC3339(rawData.user.bank_account?.mandate_date_signed)
|
mandate_date_signed: toRFC3339(rawData.object.bank_account?.mandate_date_signed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
console.log('Categories: --------');
|
console.log('Categories: --------');
|
||||||
console.dir(rawData.user.licence);
|
console.dir(rawData.object.licence);
|
||||||
if (
|
if (
|
||||||
rawData.user.password &&
|
rawData.object.password &&
|
||||||
rawData.password2 &&
|
rawData.password2 &&
|
||||||
rawData.user.password === rawData.password2 &&
|
rawData.object.password === rawData.password2 &&
|
||||||
rawData.user.password.trim() !== ''
|
rawData.object.password.trim() !== ''
|
||||||
) {
|
) {
|
||||||
processedData.user.password = rawData.user.password;
|
processedData.user.password = rawData.object.password;
|
||||||
}
|
}
|
||||||
|
const clean = JSON.parse(JSON.stringify(processedData), (key, value) =>
|
||||||
// Remove undefined or null properties
|
|
||||||
const cleanUpdateData = JSON.parse(JSON.stringify(processedData), (key, value) =>
|
|
||||||
value !== null && value !== '' ? value : undefined
|
value !== null && value !== '' ? value : undefined
|
||||||
);
|
);
|
||||||
console.dir(cleanUpdateData);
|
console.dir(clean);
|
||||||
return cleanUpdateData;
|
return clean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes the raw form data into the expected user data structure
|
||||||
|
* @param {{ object: Partial<App.Types['subscription']>} } rawData - The raw form data object
|
||||||
|
* @returns {{ subscription: Partial<App.Types['subscription']> }} Processed user data
|
||||||
|
*/
|
||||||
|
export function processSubscriptionFormData(rawData) {
|
||||||
|
/** @type {{ subscription: Partial<App.Types['subscription']> }} */
|
||||||
|
let processedData = {
|
||||||
|
subscription: {
|
||||||
|
id: Number(rawData.object.id) || 0,
|
||||||
|
name: String(rawData.object.name) || '',
|
||||||
|
details: String(rawData.object.details) || '',
|
||||||
|
conditions: String(rawData.object.conditions) || '',
|
||||||
|
hourly_rate: Number(rawData.object.hourly_rate) || 0,
|
||||||
|
monthly_fee: Number(rawData.object.monthly_fee) || 0,
|
||||||
|
included_hours_per_month: Number(rawData.object.included_hours_per_month) || 0,
|
||||||
|
included_hours_per_year: Number(rawData.object.included_hours_per_year) || 0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const clean = JSON.parse(JSON.stringify(processedData), (key, value) =>
|
||||||
|
value !== null && value !== '' ? value : undefined
|
||||||
|
);
|
||||||
|
console.dir(clean);
|
||||||
|
return clean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { BASE_API_URI } from '$lib/utils/constants';
|
import { BASE_API_URI } from '$lib/utils/constants';
|
||||||
import { formatError, userDatesFromRFC3339 } from '$lib/utils/helpers';
|
import { formatError, userDatesFromRFC3339 } from '$lib/utils/helpers';
|
||||||
import { fail, redirect } from '@sveltejs/kit';
|
import { fail, redirect } from '@sveltejs/kit';
|
||||||
import { formDataToObject, processFormData } from '$lib/utils/processing';
|
import { formDataToObject, processUserFormData } from '$lib/utils/processing';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} UpdateData
|
* @typedef {Object} UpdateData
|
||||||
@@ -30,7 +30,7 @@ export const actions = {
|
|||||||
let formData = await request.formData();
|
let formData = await request.formData();
|
||||||
|
|
||||||
const rawData = formDataToObject(formData);
|
const rawData = formDataToObject(formData);
|
||||||
const processedData = processFormData(rawData);
|
const processedData = processUserFormData(rawData);
|
||||||
|
|
||||||
const isCreating = !processedData.user.id || processedData.user.id === 0;
|
const isCreating = !processedData.user.id || processedData.user.id === 0;
|
||||||
console.log('Is creating: ', isCreating);
|
console.log('Is creating: ', isCreating);
|
||||||
|
|||||||
@@ -5,7 +5,11 @@
|
|||||||
import { BASE_API_URI } from '$lib/utils/constants';
|
import { BASE_API_URI } from '$lib/utils/constants';
|
||||||
import { formatError, userDatesFromRFC3339 } from '$lib/utils/helpers';
|
import { formatError, userDatesFromRFC3339 } from '$lib/utils/helpers';
|
||||||
import { fail, redirect } from '@sveltejs/kit';
|
import { fail, redirect } from '@sveltejs/kit';
|
||||||
import { formDataToObject, processFormData } from '$lib/utils/processing';
|
import {
|
||||||
|
formDataToObject,
|
||||||
|
processSubscriptionFormData,
|
||||||
|
processUserFormData
|
||||||
|
} from '$lib/utils/processing';
|
||||||
|
|
||||||
/** @type {import('./$types').PageServerLoad} */
|
/** @type {import('./$types').PageServerLoad} */
|
||||||
export async function load({ locals }) {
|
export async function load({ locals }) {
|
||||||
@@ -29,7 +33,7 @@ export const actions = {
|
|||||||
let formData = await request.formData();
|
let formData = await request.formData();
|
||||||
|
|
||||||
const rawData = formDataToObject(formData);
|
const rawData = formDataToObject(formData);
|
||||||
const processedData = processFormData(rawData);
|
const processedData = processUserFormData(rawData);
|
||||||
|
|
||||||
console.dir(processedData.user.membership);
|
console.dir(processedData.user.membership);
|
||||||
const isCreating = !processedData.user.id || processedData.user.id === 0;
|
const isCreating = !processedData.user.id || processedData.user.id === 0;
|
||||||
@@ -55,6 +59,49 @@ export const actions = {
|
|||||||
return fail(400, { errors: errors });
|
return fail(400, { errors: errors });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const response = await res.json();
|
||||||
|
console.log('Server success response:', response);
|
||||||
|
locals.user = response;
|
||||||
|
userDatesFromRFC3339(locals.user);
|
||||||
|
throw redirect(303, `/auth/admin/users`);
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param request - The request object
|
||||||
|
* @param fetch - Fetch object from sveltekit
|
||||||
|
* @param cookies - SvelteKit's cookie object
|
||||||
|
* @param locals - The local object, housing current user
|
||||||
|
* @returns Error data or redirects user to the home page or the previous page
|
||||||
|
*/
|
||||||
|
updateSubscription: async ({ request, fetch, cookies, locals }) => {
|
||||||
|
let formData = await request.formData();
|
||||||
|
|
||||||
|
const rawData = formDataToObject(formData);
|
||||||
|
const processedData = processSubscriptionFormData(rawData);
|
||||||
|
|
||||||
|
const isCreating = !processedData.subscription.id || processedData.subscription.id === 0;
|
||||||
|
console.log('Is creating: ', isCreating);
|
||||||
|
const apiURL = `${BASE_API_URI}/backend/subscriptions/upsert`;
|
||||||
|
|
||||||
|
/** @type {RequestInit} */
|
||||||
|
const requestOptions = {
|
||||||
|
method: isCreating ? 'POST' : 'PATCH',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Cookie: `jwt=${cookies.get('jwt')}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify(processedData)
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await fetch(apiURL, requestOptions);
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const response = await res.json();
|
||||||
|
const errors = formatError(response.errors);
|
||||||
|
return fail(400, { errors: errors });
|
||||||
|
}
|
||||||
|
|
||||||
const response = await res.json();
|
const response = await res.json();
|
||||||
console.log('Server success response:', response);
|
console.log('Server success response:', response);
|
||||||
locals.user = response;
|
locals.user = response;
|
||||||
|
|||||||
Reference in New Issue
Block a user