forked from git.gladyson/openmonetis
feat: sincronizar nome entre usuário e pagador admin
- Ao atualizar nome no perfil, sincronizar com pagador admin - Ao editar pagador admin, sincronizar com nome do usuário Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import { auth } from "@/lib/auth/config";
|
import { auth } from "@/lib/auth/config";
|
||||||
import { db, schema } from "@/lib/db";
|
import { db, schema } from "@/lib/db";
|
||||||
|
import { pagadores } from "@/db/schema";
|
||||||
|
import { PAGADOR_ROLE_ADMIN } from "@/lib/pagadores/constants";
|
||||||
import { eq, and, ne } from "drizzle-orm";
|
import { eq, and, ne } from "drizzle-orm";
|
||||||
import { headers } from "next/headers";
|
import { headers } from "next/headers";
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
@@ -72,13 +74,26 @@ export async function updateNameAction(
|
|||||||
const validated = updateNameSchema.parse(data);
|
const validated = updateNameSchema.parse(data);
|
||||||
const fullName = `${validated.firstName} ${validated.lastName}`;
|
const fullName = `${validated.firstName} ${validated.lastName}`;
|
||||||
|
|
||||||
|
// Atualizar nome do usuário
|
||||||
await db
|
await db
|
||||||
.update(schema.user)
|
.update(schema.user)
|
||||||
.set({ name: fullName })
|
.set({ name: fullName })
|
||||||
.where(eq(schema.user.id, session.user.id));
|
.where(eq(schema.user.id, session.user.id));
|
||||||
|
|
||||||
|
// Sincronizar nome com o pagador admin
|
||||||
|
await db
|
||||||
|
.update(pagadores)
|
||||||
|
.set({ name: fullName })
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(pagadores.userId, session.user.id),
|
||||||
|
eq(pagadores.role, PAGADOR_ROLE_ADMIN)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
// Revalidar o layout do dashboard para atualizar a sidebar
|
// Revalidar o layout do dashboard para atualizar a sidebar
|
||||||
revalidatePath("/", "layout");
|
revalidatePath("/", "layout");
|
||||||
|
revalidatePath("/pagadores");
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { pagadores, pagadorShares } from "@/db/schema";
|
import { pagadores, pagadorShares, user } from "@/db/schema";
|
||||||
import { handleActionError, revalidateForEntity } from "@/lib/actions/helpers";
|
import { handleActionError, revalidateForEntity } from "@/lib/actions/helpers";
|
||||||
import type { ActionResult } from "@/lib/actions/types";
|
import type { ActionResult } from "@/lib/actions/types";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
@@ -113,11 +113,11 @@ export async function updatePagadorAction(
|
|||||||
input: UpdateInput
|
input: UpdateInput
|
||||||
): Promise<ActionResult> {
|
): Promise<ActionResult> {
|
||||||
try {
|
try {
|
||||||
const user = await getUser();
|
const currentUser = await getUser();
|
||||||
const data = updateSchema.parse(input);
|
const data = updateSchema.parse(input);
|
||||||
|
|
||||||
const existing = await db.query.pagadores.findFirst({
|
const existing = await db.query.pagadores.findFirst({
|
||||||
where: and(eq(pagadores.id, data.id), eq(pagadores.userId, user.id)),
|
where: and(eq(pagadores.id, data.id), eq(pagadores.userId, currentUser.id)),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
@@ -139,7 +139,17 @@ export async function updatePagadorAction(
|
|||||||
isAutoSend: data.isAutoSend ?? false,
|
isAutoSend: data.isAutoSend ?? false,
|
||||||
role: existing.role ?? PAGADOR_ROLE_TERCEIRO,
|
role: existing.role ?? PAGADOR_ROLE_TERCEIRO,
|
||||||
})
|
})
|
||||||
.where(and(eq(pagadores.id, data.id), eq(pagadores.userId, user.id)));
|
.where(and(eq(pagadores.id, data.id), eq(pagadores.userId, currentUser.id)));
|
||||||
|
|
||||||
|
// Se o pagador é admin, sincronizar nome com o usuário
|
||||||
|
if (existing.role === PAGADOR_ROLE_ADMIN) {
|
||||||
|
await db
|
||||||
|
.update(user)
|
||||||
|
.set({ name: data.name })
|
||||||
|
.where(eq(user.id, currentUser.id));
|
||||||
|
|
||||||
|
revalidatePath("/", "layout");
|
||||||
|
}
|
||||||
|
|
||||||
revalidate();
|
revalidate();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user