Compare commits

...

2 Commits

Author SHA1 Message Date
Marco Lanzara
6f9e24a76e 🚀 Release v1.0.22
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.0.22_20251018_082656.sql.gz
- Data: 2025-10-18 08:27:14
2025-10-18 08:27:14 +00:00
marco370
74524206a5 Improve user data management by handling updates and inserts more robustly
Modify upsertUser logic in DatabaseStorage to first check for existing user by id or email, then perform an update or insert operation accordingly.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e5565357-90e1-419f-b9a8-6ee8394636df
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e5565357-90e1-419f-b9a8-6ee8394636df/DrGaAl6
2025-10-18 08:23:11 +00:00
4 changed files with 41 additions and 18 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 } from "drizzle-orm"; import { eq, and, gte, lte, desc, or } from "drizzle-orm";
export interface IStorage { export interface IStorage {
// User operations (Replit Auth required) // User operations (Replit Auth required)
@ -163,23 +163,40 @@ export class DatabaseStorage implements IStorage {
} }
async upsertUser(userData: UpsertUser): Promise<User> { async upsertUser(userData: UpsertUser): Promise<User> {
// Use onConflictDoUpdate to handle both insert and update cases // Handle conflicts on both id (primary key) and email (unique constraint)
// This handles conflicts on both id (primary key) and email (unique constraint) // Check if user exists by id or email first
const existingUser = await db
.select()
.from(users)
.where(
userData.id
? or(eq(users.id, userData.id), eq(users.email, userData.email || ''))
: eq(users.email, userData.email || '')
)
.limit(1);
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(),
})
.where(eq(users.id, existingUser[0].id))
.returning();
return updated;
} else {
// Insert new user
const [user] = await db const [user] = await db
.insert(users) .insert(users)
.values(userData) .values(userData)
.onConflictDoUpdate({
target: users.id,
set: {
email: userData.email,
name: userData.name,
role: userData.role,
updatedAt: new Date(),
},
})
.returning(); .returning();
return user; return user;
} }
}
async getAllUsers(): Promise<User[]> { async getAllUsers(): Promise<User[]> {
return await db.select().from(users).orderBy(desc(users.createdAt)); return await db.select().from(users).orderBy(desc(users.createdAt));

View File

@ -1,7 +1,13 @@
{ {
"version": "1.0.21", "version": "1.0.22",
"lastUpdate": "2025-10-18T08:18:27.659Z", "lastUpdate": "2025-10-18T08:27:14.297Z",
"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",