102 lines
2.5 KiB
TypeScript
102 lines
2.5 KiB
TypeScript
import projectModel from "../../V1Models/Project/project-model.ts";
|
|
import { Types } from "mongoose";
|
|
import versionModel from "../../V1Models/Version/versionModel.ts";
|
|
import AuthModel from "../../V1Models/Auth/userAuthModel.ts";
|
|
export const existingProject = async (
|
|
projectUuid: string,
|
|
organization: string,
|
|
userId: string
|
|
) => {
|
|
const projectData = await projectModel(organization).findOne({
|
|
projectUuid: projectUuid,
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
});
|
|
return projectData;
|
|
};
|
|
|
|
export const existingUser = async (userId: string, organization: string) => {
|
|
if (!Types.ObjectId.isValid(userId)) {
|
|
console.log("Invalid ObjectId format");
|
|
return null;
|
|
}
|
|
const userData = await AuthModel(organization).findOne({
|
|
_id: userId,
|
|
isArchive: false,
|
|
});
|
|
return userData;
|
|
};
|
|
|
|
export const archiveProject = async (
|
|
projectId: string,
|
|
organization: string
|
|
) => {
|
|
return await projectModel(organization).findByIdAndUpdate(
|
|
projectId,
|
|
{ isArchive: true },
|
|
{ new: true }
|
|
);
|
|
};
|
|
export const previousVersion = async (
|
|
projectId: string,
|
|
organization: string
|
|
) => {
|
|
const result = await versionModel(organization).findOne({
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
// .sort({ version: -1 });
|
|
return result;
|
|
};
|
|
export const generateUntitledProjectName = async (
|
|
organization: string,
|
|
userId: string
|
|
): Promise<string> => {
|
|
const projects = await projectModel(organization)
|
|
.find({
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
projectName: { $regex: /^Untitled(?: \s?\d+)?$/, $options: "i" },
|
|
})
|
|
.select("projectName");
|
|
|
|
const usedNumbers = new Set<number>();
|
|
|
|
for (const proj of projects) {
|
|
const match = proj.projectName.match(/^Untitled(?:\s?(\d+))?$/);
|
|
if (match) {
|
|
const num = match[1] ? parseInt(match[1], 10) : 0;
|
|
usedNumbers.add(num);
|
|
}
|
|
}
|
|
|
|
let newNumber = 0;
|
|
while (usedNumbers.has(newNumber)) {
|
|
newNumber++;
|
|
}
|
|
|
|
return newNumber === 0 ? "Untitled" : `Untitled ${newNumber}`;
|
|
};
|
|
export const existingProjectById = async (
|
|
projectId: string,
|
|
organization: string,
|
|
userId: string
|
|
) => {
|
|
const projectData = await projectModel(organization).findOne({
|
|
_id: projectId,
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
});
|
|
return projectData;
|
|
};
|
|
export const existingProjectByIdWithoutUser = async (
|
|
projectId: string,
|
|
organization: string
|
|
) => {
|
|
const projectData = await projectModel(organization).findOne({
|
|
_id: projectId,
|
|
isArchive: false,
|
|
});
|
|
return projectData;
|
|
};
|