Refatoração: applicationRecordService atualizado para usar Supabase
This commit is contained in:
@@ -1,55 +1,52 @@
|
||||
import ApplicationRecord from '../models/ApplicationRecord.js';
|
||||
import { ApplicationRecord } from '../lib/compat.js';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const createApplicationRecord = async (data: any & { organizationId?: string, createdBy?: string }) => {
|
||||
const newRecord = new ApplicationRecord({
|
||||
const record = await ApplicationRecord.create({
|
||||
...data,
|
||||
date: data.date ? new Date(data.date) : null,
|
||||
organizationId: data.organizationId,
|
||||
createdBy: data.createdBy
|
||||
date: data.date ? new Date(data.date).toISOString() : null,
|
||||
organization_id: data.organizationId,
|
||||
created_by: data.createdBy,
|
||||
project_id: data.projectId
|
||||
});
|
||||
const saved = await newRecord.save();
|
||||
return { ...saved.toObject(), id: saved._id.toString() };
|
||||
return record;
|
||||
};
|
||||
|
||||
export const getApplicationRecordsByProject = async (projectId: string, organizationId?: string) => {
|
||||
const query = { projectId, ...(organizationId ? { organizationId } : {}) };
|
||||
const records = await ApplicationRecord.find(query).sort({ date: -1 }).lean();
|
||||
return records.map(r => ({ ...r, id: r._id.toString() }));
|
||||
const filter: any = { project_id: projectId };
|
||||
if (organizationId) {
|
||||
filter.organization_id = organizationId;
|
||||
}
|
||||
return await ApplicationRecord.find(filter);
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const updateApplicationRecord = async (id: string, data: any, organizationId?: string, userId?: string, userRole?: string, isDeveloper: boolean = false) => {
|
||||
const existing = await ApplicationRecord.findById(id);
|
||||
if (!existing) return null;
|
||||
|
||||
// Organization Check
|
||||
if (organizationId && existing.organizationId && existing.organizationId !== organizationId) {
|
||||
if (organizationId && existing.organization_id && existing.organization_id !== organizationId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Role/Ownership check
|
||||
const isPowerUser = userRole === 'admin' || isDeveloper;
|
||||
if (!isPowerUser && existing.createdBy && existing.createdBy !== userId) {
|
||||
console.warn(`Permission Denied: User ${userId} tried to update record ${id} created by ${existing.createdBy}`);
|
||||
if (!isPowerUser && existing.created_by && existing.created_by !== userId) {
|
||||
console.warn(`Permission Denied: User ${userId} tried to update record ${id} created by ${existing.created_by}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const updateData = {
|
||||
...data,
|
||||
date: data.date ? new Date(data.date) : undefined
|
||||
date: data.date ? new Date(data.date).toISOString() : undefined
|
||||
};
|
||||
|
||||
if (organizationId && !existing.organizationId) {
|
||||
updateData.organizationId = organizationId;
|
||||
if (organizationId && !existing.organization_id) {
|
||||
updateData.organization_id = organizationId;
|
||||
}
|
||||
|
||||
const updated = await ApplicationRecord.findOneAndUpdate({ _id: id }, updateData, { new: true }).lean();
|
||||
if (updated) {
|
||||
return { ...updated, id: updated._id.toString() };
|
||||
}
|
||||
return null;
|
||||
return await ApplicationRecord.findByIdAndUpdate(id, updateData);
|
||||
};
|
||||
|
||||
export const deleteApplicationRecord = async (id: string, organizationId?: string, userId?: string, userRole?: string, isDeveloper: boolean = false) => {
|
||||
@@ -57,16 +54,17 @@ export const deleteApplicationRecord = async (id: string, organizationId?: strin
|
||||
if (!existing) return false;
|
||||
|
||||
// Organization Check
|
||||
if (organizationId && existing.organizationId && existing.organizationId !== organizationId) {
|
||||
if (organizationId && existing.organization_id && existing.organization_id !== organizationId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Role/Ownership check
|
||||
const isPowerUser = userRole === 'admin' || isDeveloper;
|
||||
if (!isPowerUser && existing.createdBy && existing.createdBy !== userId) {
|
||||
if (!isPowerUser && existing.created_by && existing.created_by !== userId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await ApplicationRecord.deleteOne({ _id: id });
|
||||
await ApplicationRecord.findByIdAndDelete(id);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user