Skip to content

Commit d36ed4e

Browse files
committed
feat: update User entity to track lastSeen and manage login streak logic
1 parent 3bcdfab commit d36ed4e

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

server/src/user/entity/user.entity.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@ export class User {
1919
lastEdited: Date;
2020

2121
@Prop({ type: MongooseSchema.Types.Date, required: true, default: Date.now })
22-
lastLogin: Date;
22+
lastSeen: Date;
2323

2424
@Prop({ type: Number, required: true, default: 0 })
2525
loginStreak: number;
2626

27-
@Prop({ type: Number, required: true, default: 0 })
28-
loginCount: number;
29-
3027
@Prop({ type: Number, required: true, default: 0 })
3128
playCount: number;
3229

server/src/user/user.service.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ export class UserService {
9595
if (!usedData)
9696
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
9797

98+
const today = new Date();
99+
today.setHours(0, 0, 0, 0); // Set the time to the start of the day
100+
101+
const lastSeenDate = new Date(usedData.lastSeen);
102+
lastSeenDate.setHours(0, 0, 0, 0); // Set the time to the start of the day
103+
104+
if (lastSeenDate < today) {
105+
usedData.lastSeen = new Date();
106+
usedData.lastSeen = new Date();
107+
108+
// if the last seen date is not yesterday, reset the login streak
109+
// if the last seen date is not yesterday, reset the login streak
110+
const yesterday = new Date(today);
111+
yesterday.setDate(today.getDate() - 1);
112+
113+
if (lastSeenDate < yesterday) usedData.loginStreak = 1;
114+
else usedData.loginStreak += 1;
115+
116+
usedData.save(); // no need to await this, we already have the data to sent back
117+
} // if equal or greater, do nothing about the login streak
118+
98119
return usedData;
99120
}
100121

0 commit comments

Comments
 (0)