-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.ts
85 lines (73 loc) · 2.83 KB
/
storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { type ContactMessage, type InsertContact, type User, type InsertUser, type Appointment, type InsertAppointment } from "@shared/schema";
import type { SessionData } from "express-session";
import createMemoryStore from "memorystore";
import session from "express-session";
const MemoryStore = createMemoryStore(session);
export interface IStorage {
createContactMessage(message: InsertContact): Promise<ContactMessage>;
createOrUpdateUser(user: Partial<InsertUser> & { googleId: string, accessToken?: string, refreshToken?: string }): Promise<User>;
getUser(id: number): Promise<User | null>;
getUserByGoogleId(googleId: string): Promise<User | null>;
createAppointment(appointment: InsertAppointment & { userId: number, status: string }): Promise<Appointment>;
sessionStore: session.Store;
}
export class MemStorage implements IStorage {
private messages: Map<number, ContactMessage>;
private users: Map<number, User>;
private appointments: Map<number, Appointment>;
private currentId: number;
sessionStore: session.Store;
constructor() {
this.messages = new Map();
this.users = new Map();
this.appointments = new Map();
this.currentId = 1;
this.sessionStore = new MemoryStore({
checkPeriod: 86400000 // Prune expired entries every 24h
});
}
async createContactMessage(message: InsertContact): Promise<ContactMessage> {
const id = this.currentId++;
const newMessage: ContactMessage = { id, ...message };
this.messages.set(id, newMessage);
return newMessage;
}
async createOrUpdateUser(userData: Partial<InsertUser> & { googleId: string }): Promise<User> {
let user = await this.getUserByGoogleId(userData.googleId);
if (!user) {
const id = this.currentId++;
user = {
id,
name: userData.name || '',
email: userData.email || '',
phone: userData.phone || '',
googleId: userData.googleId,
gender: userData.gender || null,
age: userData.age || null,
createdAt: new Date()
};
this.users.set(id, user);
}
return user;
}
async getUser(id: number): Promise<User | null> {
return this.users.get(id) || null;
}
async getUserByGoogleId(googleId: string): Promise<User | null> {
return Array.from(this.users.values()).find(user => user.googleId === googleId) || null;
}
async createAppointment(appointment: InsertAppointment & { userId: number, status: string }): Promise<Appointment> {
const id = this.currentId++;
const newAppointment: Appointment = {
id,
userId: appointment.userId,
serviceType: appointment.serviceType,
appointmentDate: appointment.appointmentDate,
status: appointment.status,
createdAt: new Date()
};
this.appointments.set(id, newAppointment);
return newAppointment;
}
}
export const storage = new MemStorage();