Skip to content

Commit

Permalink
Merge pull request #33 from REAN-Foundation/image_upload_issue
Browse files Browse the repository at this point in the history
Image upload issue
  • Loading branch information
kiran-rean authored Jun 19, 2024
2 parents f3fed95 + 70bb2a1 commit 6cb9f1b
Show file tree
Hide file tree
Showing 49 changed files with 742 additions and 725 deletions.
2 changes: 1 addition & 1 deletion src/lib/options/aha.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const Options: FeatureOptions[] = [
},
{
Name: 'Users',
Enabled: false
Enabled: true
},
//..............................
{
Expand Down
115 changes: 77 additions & 38 deletions src/routes/api/server/file-resources/upload/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,80 @@ import * as fs from 'fs';

//////////////////////////////////////////////////////////////

export const POST = (async (event: RequestEvent) => {
try {
console.log(`Upload in progress---`);

console.log(JSON.stringify(event, null, 2));

const data_ = await event.request.json();
const params: URLSearchParams = event.url.searchParams;
console.log(`search params : ` + params);
const filename = event.request.headers.get('filename');
console.log(filename);

const fileBuffer = data_['image'];
const filePath = filename;
fs.writeFileSync(filePath, fileBuffer, 'base64');

if (fs.existsSync(filePath)) {
console.log(Date.now().toString());
console.log(`Copied file ${filename} to server stats/temp.`);
}

const sessionId = event.locals.sessionUser.sessionId;
const buffer = fs.readFileSync(filePath);

console.log('Uploading file resource ...');
//const location = path.join(process.cwd(), filePath);
const response = await uploadBinary(sessionId, buffer, filename, true);
console.log(JSON.stringify(response, null, 2));

fs.unlinkSync(filePath);

return new Response(JSON.stringify(response));
} catch (err) {
//console.error(`Error uploading file: ${err.message}`);
console.error(`Error uploading file: ${JSON.stringify(err.stack.split('\n'), null, 2)}`);
return new Response(err.message);
}
}) satisfies RequestHandler;
// export const POST = (async (event: RequestEvent) => {
// try {
// console.log(`Upload in progress---`);

// // console.log(JSON.stringify(event, null, 2));
// console.log("Event",event);
// const formData = await event.request.formData();
// // console.log(JSON.stringify(event, null, 2));
// const file = formData.get('file') as File;
// const filename = formData.get('filename');
// // const data_ = await event.request.json();
// // const params: URLSearchParams = event.url.searchParams;
// // console.log(`search params : ` + params);
// // const filename = event.request.headers.get('filename');
// console.log("filename............",filename);

// const fileBuffer = data_['image'];
// const filePath = filename;
// fs.writeFileSync(filePath, fileBuffer, 'base64');

// if (fs.existsSync(filePath)) {
// console.log(Date.now().toString());
// console.log(`Copied file ${filename} to server stats/temp.`);
// }

// const sessionId = event.locals.sessionUser.sessionId;
// const buffer = fs.readFileSync(filePath);

// console.log('Uploading file resource ...');
// //const location = path.join(process.cwd(), filePath);
// const response = await uploadBinary(sessionId, buffer, filename, true);
// console.log(JSON.stringify(response, null, 2));

// fs.unlinkSync(filePath);

// return new Response(JSON.stringify(response));
// } catch (err) {
// //console.error(`Error uploading file: ${err.message}`);
// console.error(`Error uploading file: ${JSON.stringify(err.stack.split('\n'), null, 2)}`);
// return new Response(err.message);
// }
// }) satisfies RequestHandler;
export const POST: RequestHandler = async (event: RequestEvent) => {
try {
console.log(`Upload in progress---`);

const formData = await event.request.formData();
const file = formData.get('file') as File;
const filename = formData.get('filename');

if (!file || !filename) {
return new Response(JSON.stringify({ Status: 'error', Message: 'File or filename missing' }), { status: 400 });
}

const buffer = await file.arrayBuffer();
const filePath = `/tmp/${filename}`;
fs.writeFileSync(filePath, Buffer.from(buffer));

if (fs.existsSync(filePath)) {
console.log(`Copied file ${filename} to server /tmp.`);
}

const sessionId = event.locals.sessionUser.sessionId;
const fileBuffer = fs.readFileSync(filePath);

console.log('Uploading file resource ...');
const response = await uploadBinary(sessionId, fileBuffer, filename, true);
console.log(JSON.stringify(response, null, 2));

fs.unlinkSync(filePath);

return new Response(JSON.stringify(response), { status: 201 });
} catch (err) {
console.error(`Error uploading file: ${err}`);
return new Response(JSON.stringify({ Status: 'error', Message: err.message }), { status: 500 });
}
};
2 changes: 1 addition & 1 deletion src/routes/api/server/users/search/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const GET = async (event: RequestEvent) => {
};
console.log('Search parms: ', searchParams);
const response = await searchUsers(sessionId, searchParams);
const items = response.Data.UserRecords.Items;
const items = response.Data.Users;
console.log('res==', response);

return new Response(JSON.stringify(items));
Expand Down
26 changes: 15 additions & 11 deletions src/routes/api/services/reancare/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,23 @@ export const resetPassword = async (

export const createUser = async (
sessionId: string,
tenantId: string,
tenantId: string,
firstName: string,
lastName: string,
phone: string,
phone: string,
email: string,
role: string,
roleId: string,
roleId: string,
password: string,
) => {
const body = {
TenantId: tenantId,
TenantId: tenantId,
FirstName: firstName,
LastName: lastName,
Role: role,
RoleId: roleId,
Role: role,
RoleId: roleId,
Phone: phone ? phone : null,
Email: email ? email : null,
Email: email ? email : null,
Password: password,
};

Expand Down Expand Up @@ -176,6 +176,7 @@ export const searchUsers = async (sessionId: string, searchParams?: any) => {
}
searchString += params.join('&');
}
console.log("searchString",searchString);
}
const url = BACKEND_API_URL + `/users/search${searchString}`;
return await get(sessionId, url, true, API_CLIENT_INTERNAL_KEY);
Expand All @@ -188,22 +189,25 @@ export const updateUser = async (
lastName: string,
phone: string,
email: string,
role: string,
password: string,
roleId:string,
// role: string,
// password: string,
imageResourceId: string
) => {
const body = {
FirstName: firstName,
LastName: lastName,
Role: role,
RoleId: roleId,
Phone: phone ? phone : null,
Email: email ? email : null,
Password: password,
// Password: password,
ImageResourceId: imageResourceId
};
if (Helper.isPhone(phone)) {
body.Phone = Helper.sanitizePhone(phone);
};

console.log("body.....",body)
const url = BACKEND_API_URL + `/users/${userId}`;
return await put(sessionId, url, body, true, API_CLIENT_INTERNAL_KEY);
};
Expand Down
4 changes: 2 additions & 2 deletions src/routes/users/[userId]/assessment-templates/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
export let data: PageServerData;
$:assessmentTemplates = data.assessmentTemplate.Items;
$: assessmentTemplates = data.assessmentTemplate.Items;
let retrivedAssessmentTemplates;
const userId = $page.params.userId;
const assessmentRoute = `/users/${userId}/assessment-templates`;
Expand All @@ -29,7 +29,7 @@
let sortOrder = 'ascending';
let itemsPerPage = 10;
let offest = 0;
let totalAssessmentTemplatesCount = data.assessmentTemplate.TotalCount;
$: totalAssessmentTemplatesCount = data.assessmentTemplate.TotalCount;
let isSortingTitle = false;
let isSortingType = false;
let items = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ import { searchAssessmentNodes } from '../../../../../api/services/reancare/asse
export const load: PageServerLoad = async (event: ServerLoadEvent) => {
const sessionId = event.cookies.get('sessionId');
event.depends('app:assessment-nodes');
try {
const templateId = event.params.templateId;
const searchParams = {
templateId: templateId
};
const response = await searchAssessmentNodes(sessionId, searchParams);
if (response.Status === 'failure' || response.HttpCode !== 200) {
throw error(response.HttpCode, response.Message);
}
const assessmentNodes = response.Data.AssessmentNodeRecords;
const templateId = event.params.templateId;
const searchParams = {
templateId: templateId
};
const response = await searchAssessmentNodes(sessionId, searchParams);
if (response.Status === 'failure' || response.HttpCode !== 200) {
throw error(response.HttpCode, response.Message);
}
const assessmentNodes = response.Data.AssessmentNodeRecords;

return {
assessmentNodes,
sessionId,
message: response.Message
};

return {
assessmentNodes,
sessionId,
message: response.Message
};
} catch (error) {
console.error(`Error retriving assessment nodes: ${error.message}`);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,30 @@ import {
export const load: PageServerLoad = async (event: RequestEvent) => {
const sessionId = event.cookies.get('sessionId');

try {
const templateId = event.params.templateId;
const assessmentNodeId = event.params.nodeId;
const searchParams = {
templateId: templateId
};
const _queryResponseTypes = await getQueryResponseTypes(sessionId);
const _assessmentNodes = await searchAssessmentNodes(sessionId, searchParams);
const response = await getAssessmentNodeById(sessionId, templateId, assessmentNodeId);
const templateId = event.params.templateId;
const assessmentNodeId = event.params.nodeId;
const searchParams = {
templateId: templateId
};
const _queryResponseTypes = await getQueryResponseTypes(sessionId);
const _assessmentNodes = await searchAssessmentNodes(sessionId, searchParams);
const response = await getAssessmentNodeById(sessionId, templateId, assessmentNodeId);

if (response.Status === 'failure' || response.HttpCode !== 200) {
throw error(response.HttpCode, response.Message);
}
const assessmentNode = response.Data.AssessmentNode;
const queryResponseTypes = _queryResponseTypes.Data.QueryResponseTypes;
const assessmentNodes = _assessmentNodes.Data.AssessmentNodeRecords.Items;
const id = response.Data.AssessmentNode.id;
return {
location: `${id}/edit`,
assessmentNode,
queryResponseTypes,
assessmentNodes,
message: response.Message
};

if (response.Status === 'failure' || response.HttpCode !== 200) {
throw error(response.HttpCode, response.Message);
}
const assessmentNode = response.Data.AssessmentNode;
const queryResponseTypes = _queryResponseTypes.Data.QueryResponseTypes;
const assessmentNodes = _assessmentNodes.Data.AssessmentNodeRecords.Items;
const id = response.Data.AssessmentNode.id;
return {
location: `${id}/edit`,
assessmentNode,
queryResponseTypes,
assessmentNodes,
message: response.Message
};
} catch (error) {
console.error(`Error retriving assessment node: ${error.message}`);
}
};

const updateAssessmentNodeSchema = zfd.formData({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { getAssessmentNodeById } from '../../../../../../../api/services/reancar

export const load: PageServerLoad = async (event: RequestEvent) => {
const sessionId = event.cookies.get('sessionId');

try {
const assessmentNodeId = event.params.nodeId;
const assessmentNodeId = event.params.nodeId;
const templateId = event.params.templateId;
const response = await getAssessmentNodeById(sessionId, templateId, assessmentNodeId);
const _templateScoringCondition = await getAssessmentTemplateById(sessionId, templateId);
Expand All @@ -27,7 +25,6 @@ export const load: PageServerLoad = async (event: RequestEvent) => {
templateScoringCondition,
message: response.Message
};
} catch (error) {
console.error(`Error retriving assessment node: ${error.message}`);
}


};
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,25 @@ import {

export const load: PageServerLoad = async (event: RequestEvent) => {
const sessionId = event.cookies.get('sessionId');
const templateId = event.params.templateId;
const searchParams = {
templateId: templateId
};
const _queryResponseTypes = await getQueryResponseTypes(sessionId);
const response = await searchAssessmentNodes(sessionId, searchParams);

try {
const templateId = event.params.templateId;
const searchParams = {
templateId: templateId
};
const _queryResponseTypes = await getQueryResponseTypes(sessionId);
const response = await searchAssessmentNodes(sessionId, searchParams);

if (response.Status === 'failure' || response.HttpCode !== 200) {
throw error(response.HttpCode, response.Message);
}
const queryResponseTypes = _queryResponseTypes.Data.QueryResponseTypes;
const assessmentNodes = response.Data.AssessmentNodeRecords.Items;
if (response.Status === 'failure' || response.HttpCode !== 200) {
throw error(response.HttpCode, response.Message);
}
const queryResponseTypes = _queryResponseTypes.Data.QueryResponseTypes;
const assessmentNodes = response.Data.AssessmentNodeRecords.Items;

return {
queryResponseTypes,
assessmentNodes,
message: response.Message
};
} catch (error) {
console.error(`Error retriving query response types: ${error.message}`);
}
return {
queryResponseTypes,
assessmentNodes,
message: response.Message
};

};

const createAssessmentNodeSchema = zfd.formData({
Expand Down
Loading

0 comments on commit 6cb9f1b

Please sign in to comment.