feat: include users and messages in full system backup
This commit is contained in:
@@ -21,7 +21,7 @@ async function authenticate() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/admin/backup - Exportar todos os dados do CMS
|
// GET /api/admin/backup - Exportar todos os dados do CMS e Sistema
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const user = await authenticate();
|
const user = await authenticate();
|
||||||
@@ -33,14 +33,18 @@ export async function GET() {
|
|||||||
const services = await prisma.service.findMany();
|
const services = await prisma.service.findMany();
|
||||||
const pageContents = await prisma.pageContent.findMany();
|
const pageContents = await prisma.pageContent.findMany();
|
||||||
const settings = await prisma.settings.findFirst();
|
const settings = await prisma.settings.findFirst();
|
||||||
|
const users = await prisma.user.findMany();
|
||||||
|
const messages = await prisma.message.findMany();
|
||||||
|
|
||||||
const backupData = {
|
const backupData = {
|
||||||
version: '1.0',
|
version: '1.2',
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
projects,
|
projects,
|
||||||
services,
|
services,
|
||||||
pageContents,
|
pageContents,
|
||||||
settings,
|
settings,
|
||||||
|
users,
|
||||||
|
messages,
|
||||||
};
|
};
|
||||||
|
|
||||||
return NextResponse.json(backupData);
|
return NextResponse.json(backupData);
|
||||||
@@ -50,7 +54,7 @@ export async function GET() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /api/admin/backup - Importar dados do CMS
|
// POST /api/admin/backup - Importar dados do CMS e Sistema
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const user = await authenticate();
|
const user = await authenticate();
|
||||||
@@ -102,7 +106,31 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Importar Configurações
|
// 4. Importar Usuários
|
||||||
|
if (Array.isArray(backupData.users)) {
|
||||||
|
for (const u of backupData.users) {
|
||||||
|
const { id, createdAt, updatedAt, ...data } = u;
|
||||||
|
await tx.user.upsert({
|
||||||
|
where: { email: data.email },
|
||||||
|
create: { id, ...data },
|
||||||
|
update: data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Importar Mensagens
|
||||||
|
if (Array.isArray(backupData.messages)) {
|
||||||
|
for (const msg of backupData.messages) {
|
||||||
|
const { id, createdAt, updatedAt, ...data } = msg;
|
||||||
|
await tx.message.upsert({
|
||||||
|
where: { id: id },
|
||||||
|
create: { id, ...data },
|
||||||
|
update: data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Importar Configurações
|
||||||
if (backupData.settings) {
|
if (backupData.settings) {
|
||||||
const { id, updatedAt, ...data } = backupData.settings;
|
const { id, updatedAt, ...data } = backupData.settings;
|
||||||
const currentSettings = await tx.settings.findFirst();
|
const currentSettings = await tx.settings.findFirst();
|
||||||
|
|||||||
Reference in New Issue
Block a user