diff --git a/src/server/services/applicationRecordService.ts b/src/server/services/applicationRecordService.ts index 7893dda..ab6bc23 100644 --- a/src/server/services/applicationRecordService.ts +++ b/src/server/services/applicationRecordService.ts @@ -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; }; + diff --git a/update.sh b/update.sh new file mode 100644 index 0000000..0b33189 --- /dev/null +++ b/update.sh @@ -0,0 +1,47 @@ +#!/bin/bash +# --------------------------------------------------------- +# GPI: SCRIPT DE DEPLOY TOTAL & SINCRONIZAÇÃO AUTOMÁTICA +# --------------------------------------------------------- + +CYAN='\033[0;36m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' + +echo -e "\n${CYAN}🚀 Iniciando Ciclo Automático de Deploy GPI...${NC}" + +# Carrega variáveis do .env se existir +if [ -f .env ]; then + export $(grep -v '^#' .env | xargs) +fi + +# 1. Sincronização com Repositório (Git - Gitea) +echo -e "${YELLOW}📝 Sincronizando código com o Gitea...${NC}" +git add . + +# Verifica se existem alterações para o commit +if git diff-index --quiet HEAD --; then + echo -e "${GREEN}✨ Código local já está sincronizado com o último commit.${NC}" +else + TIMESTAMP=$(date +"%d/%m/%Y %H:%M:%S") + MSG="${1:-🚀 Auto-deploy: GPI atualizado em $TIMESTAMP}" + echo -e "${CYAN}📤 Enviando alterações: $MSG${NC}" + git commit -m "$MSG" + git push origin main +fi + +# 2. Gatilho de Deploy no Coolify (API Oficial v1) +if [ ! -z "$COOLIFY_RESOURCE_UUID" ]; then + echo -e "${YELLOW}🔄 Disparando Deploy via API Oficial no Coolify...${NC}" + COOLIFY_TOKEN="12|wbepNILQe24LAOjfEUmMROgU93F6uG1zuwPLwrRi786bca03" + + # Endpoint de Deploy via API v1 + curl -s -X GET "https://painel.reifonas.cloud/api/v1/deploy?uuid=${COOLIFY_RESOURCE_UUID}&force=false" \ + -H "Authorization: Bearer $COOLIFY_TOKEN" + + echo -e "\n${GREEN}✅ Deploy oficial via API engatilhado (Hash Sincronizado).${NC}" +fi + +# 3. Resultado Final +echo -e "${GREEN}🏁 Ciclo concluído com sucesso.${NC}\n"