Project Create and Get All projects API completed
This commit is contained in:
10
src/shared/connect/blobConnection.ts
Normal file
10
src/shared/connect/blobConnection.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Client } from 'minio';
|
||||
const minioClient = new Client({
|
||||
endPoint: process.env.MinIO_URL!,
|
||||
port: parseInt(process.env.MinIO_PORT!, 10),
|
||||
useSSL: false,
|
||||
accessKey: process.env.MinIO_accessKey!,
|
||||
secretKey: process.env.MinIO_secretKey!,
|
||||
});
|
||||
|
||||
export { minioClient}
|
||||
@@ -49,12 +49,6 @@ const MainModel = <T>(
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
export const minioClient = new Client({
|
||||
endPoint: "185.100.212.76", // MinIO server IP or hostname
|
||||
port: 9999, // MinIO server port
|
||||
useSSL: false, // Set to true if SSL is configured
|
||||
accessKey: "sabarinathan", // Access key
|
||||
secretKey: "sabarinathan",
|
||||
});
|
||||
|
||||
|
||||
export default MainModel;
|
||||
|
||||
@@ -15,7 +15,7 @@ export interface Project extends Document {
|
||||
}
|
||||
const projectSchema: Schema = new Schema(
|
||||
{
|
||||
projectUuid: { type: String, required: true },
|
||||
projectUuid: { type: String },
|
||||
projectName: { type: String },
|
||||
thumbnail: { type: String },
|
||||
isArchive: { type: Boolean, default: false },
|
||||
|
||||
67
src/shared/services/blob/blobServices.ts
Normal file
67
src/shared/services/blob/blobServices.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { minioClient } from "../../connect/blobConnection.ts";
|
||||
|
||||
type MulterFile = {
|
||||
fieldname: string;
|
||||
originalname: string;
|
||||
encoding: string;
|
||||
mimetype: string;
|
||||
size: number;
|
||||
buffer: Buffer;
|
||||
};
|
||||
|
||||
const bucketName = "assets-public-bucket";
|
||||
|
||||
// Define public read policy
|
||||
const publicReadPolicy = {
|
||||
Version: "2012-10-17",
|
||||
Statement: [
|
||||
{
|
||||
Effect: "Allow",
|
||||
Principal: { AWS: "*" },
|
||||
Action: ["s3:GetObject"],
|
||||
Resource: [`arn:aws:s3:::${bucketName}/*`],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async function ensureBucketExists() {
|
||||
const exists = await minioClient.bucketExists(bucketName);
|
||||
if (!exists) {
|
||||
await minioClient.makeBucket(bucketName, "local-region");
|
||||
console.log("Bucket created");
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureFolderExists(folderName: string) {
|
||||
const folderPrefix = folderName.endsWith("/") ? folderName : `${folderName}/`;
|
||||
const objects = minioClient.listObjects(bucketName, folderPrefix, true);
|
||||
for await (const _ of objects) return; // Folder exists
|
||||
await minioClient.putObject(bucketName, folderPrefix, Buffer.from(""));
|
||||
}
|
||||
|
||||
async function setPublicPolicy() {
|
||||
await minioClient.setBucketPolicy(bucketName, JSON.stringify(publicReadPolicy));
|
||||
}
|
||||
|
||||
export async function uploadProjectThumbnail(file: MulterFile, folderName: string): Promise<string> {
|
||||
try {
|
||||
const folderName = "models";
|
||||
const objectName = `${folderName}/${Date.now()}_${file.originalname}`;
|
||||
|
||||
|
||||
await ensureBucketExists();
|
||||
await ensureFolderExists(folderName);
|
||||
await minioClient.putObject(bucketName, objectName, file.buffer, file.size, {
|
||||
"Content-Type": file.mimetype,
|
||||
});
|
||||
await setPublicPolicy();
|
||||
|
||||
const encodedName = encodeURIComponent(objectName);
|
||||
const blobUrl = `${process.env.MinIO_USESSL === "true" ? "https" : "http"}://${process.env.MinIO_URL}:${process.env.MINIO_PORT}/${bucketName}/${encodedName}`;
|
||||
|
||||
return blobUrl;
|
||||
} catch (err) {
|
||||
console.error("Upload failed:", err);
|
||||
throw new Error("Thumbnail upload failed");
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import projectModel from "../../model/project/project-model.ts";
|
||||
import userModel from "../../model/user-Model.ts";
|
||||
import { Types } from "mongoose";
|
||||
import versionModel from "../../model/version/versionModel.ts";
|
||||
import { uploadProjectThumbnail } from "../blob/blobServices.ts";
|
||||
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
projectUuid: string;
|
||||
@@ -10,7 +12,10 @@ interface CreateProjectInput {
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
}
|
||||
|
||||
interface GetInterface {
|
||||
userId: string;
|
||||
organization: string;
|
||||
}
|
||||
export const createProject = async (data: CreateProjectInput) => {
|
||||
try {
|
||||
const {
|
||||
@@ -21,9 +26,9 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
sharedUsers,
|
||||
organization,
|
||||
} = data;
|
||||
|
||||
if (
|
||||
!projectName ||
|
||||
!projectUuid ||
|
||||
!userId ||
|
||||
!thumbnail ||
|
||||
// !sharedUsers ||
|
||||
@@ -36,7 +41,7 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProject(projectUuid, organization);
|
||||
const projectExisting = await existingProject(projectName, organization,userId);
|
||||
|
||||
if (projectExisting) {
|
||||
return {
|
||||
@@ -49,17 +54,18 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
projectName: projectName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
thumbnail: thumbnail || "",
|
||||
thumbnail: thumbnail,
|
||||
sharedUsers: sharedUsers || [],
|
||||
isArchive: false,
|
||||
});
|
||||
const versionData = previousVersion(project._id, organization);
|
||||
if (!versionData) {
|
||||
await versionModel(organization).create({
|
||||
const versionData = await previousVersion(project._id, organization);
|
||||
if (!versionData || versionData.length === 0) {
|
||||
const newVersion= await versionModel(organization).create({
|
||||
projectId: project._id,
|
||||
createdBy: userId,
|
||||
version: 0.01,
|
||||
});
|
||||
await projectModel(organization).findByIdAndUpdate({_id:project._id,isArchive:false},{total_versions:`v-${newVersion.version.toFixed(2)}`})
|
||||
}
|
||||
return {
|
||||
status: "success",
|
||||
@@ -73,7 +79,7 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const GetAllProjects = async (data: CreateProjectInput) => {
|
||||
export const GetAllProjects = async (data: GetInterface) => {
|
||||
try {
|
||||
const { userId, organization } = data;
|
||||
await existingUser(userId, organization);
|
||||
@@ -83,18 +89,20 @@ export const GetAllProjects = async (data: CreateProjectInput) => {
|
||||
isArchive: false,
|
||||
})
|
||||
.select("_id projectName createdBy thumbnail");
|
||||
if (projectDatas) return { Datas: projectDatas };
|
||||
if (projectDatas) return {status:"success", Datas: projectDatas };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
|
||||
export const existingProject = async (
|
||||
projectUuid: string,
|
||||
organization: string
|
||||
projectName: string,
|
||||
organization: string,
|
||||
userId:string
|
||||
) => {
|
||||
const projectData = await projectModel(organization).findOne({
|
||||
projectUuid: projectUuid,
|
||||
projectName: projectName,
|
||||
createdBy:userId,
|
||||
isArchive: false,
|
||||
});
|
||||
return projectData;
|
||||
@@ -124,12 +132,10 @@ export const archiveProject = async (
|
||||
export const previousVersion = async (
|
||||
projectId: string,
|
||||
organization: string
|
||||
): Promise<void> => {
|
||||
const result = await versionModel(organization)
|
||||
.findOne({
|
||||
projectId: projectId,
|
||||
isArchive: false,
|
||||
})
|
||||
.sort({ version: -1 });
|
||||
)=> {
|
||||
console.log('projectId: ', projectId);
|
||||
const result = await versionModel(organization).findOne({ projectId: projectId, isArchive: false})
|
||||
console.log('result: ', result);
|
||||
// .sort({ version: -1 });
|
||||
return result;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user