Compare commits

..

No commits in common. "6f9e24a76e01cbe5f45b31d171ed105c445c22c5" and "6b6db9474ec1de0e5a89a037679272c7a888d3ff" have entirely different histories.

4 changed files with 18 additions and 41 deletions

View File

@ -56,7 +56,7 @@ import {
type InsertCcnlSetting, type InsertCcnlSetting,
} from "@shared/schema"; } from "@shared/schema";
import { db } from "./db"; import { db } from "./db";
import { eq, and, gte, lte, desc, or } from "drizzle-orm"; import { eq, and, gte, lte, desc } from "drizzle-orm";
export interface IStorage { export interface IStorage {
// User operations (Replit Auth required) // User operations (Replit Auth required)
@ -163,39 +163,22 @@ export class DatabaseStorage implements IStorage {
} }
async upsertUser(userData: UpsertUser): Promise<User> { async upsertUser(userData: UpsertUser): Promise<User> {
// Handle conflicts on both id (primary key) and email (unique constraint) // Use onConflictDoUpdate to handle both insert and update cases
// Check if user exists by id or email first // This handles conflicts on both id (primary key) and email (unique constraint)
const existingUser = await db const [user] = await db
.select() .insert(users)
.from(users) .values(userData)
.where( .onConflictDoUpdate({
userData.id target: users.id,
? or(eq(users.id, userData.id), eq(users.email, userData.email || '')) set: {
: eq(users.email, userData.email || '') email: userData.email,
) name: userData.name,
.limit(1); role: userData.role,
if (existingUser.length > 0) {
// Update existing user - NEVER change the ID (it's a primary key)
const [updated] = await db
.update(users)
.set({
...(userData.email && { email: userData.email }),
...(userData.name && { name: userData.name }),
...(userData.role && { role: userData.role }),
updatedAt: new Date(), updatedAt: new Date(),
}) },
.where(eq(users.id, existingUser[0].id)) })
.returning(); .returning();
return updated; return user;
} else {
// Insert new user
const [user] = await db
.insert(users)
.values(userData)
.returning();
return user;
}
} }
async getAllUsers(): Promise<User[]> { async getAllUsers(): Promise<User[]> {

View File

@ -1,13 +1,7 @@
{ {
"version": "1.0.22", "version": "1.0.21",
"lastUpdate": "2025-10-18T08:27:14.297Z", "lastUpdate": "2025-10-18T08:18:27.659Z",
"changelog": [ "changelog": [
{
"version": "1.0.22",
"date": "2025-10-18",
"type": "patch",
"description": "Deployment automatico v1.0.22"
},
{ {
"version": "1.0.21", "version": "1.0.21",
"date": "2025-10-18", "date": "2025-10-18",