Skip to content

Commit 2906599

Browse files
ahmaxedraisedadead
andauthored
feat: add socrates (freeCodeCamp#65430)
Co-authored-by: Mrugesh Mohapatra <noreply@mrugesh.dev>
1 parent 2ad6e44 commit 2906599

51 files changed

Lines changed: 1915 additions & 56 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/prisma/schema.prisma

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ model user {
173173
savedChallenges SavedChallenge[] // Undefined | SavedChallenge[]
174174
// Nullable tri-state: null (likely new user), true (subscribed), false (unsubscribed)
175175
sendQuincyEmail Boolean?
176+
socrates Boolean?
176177
theme String? // Undefined
177178
timezone String? // Undefined
178179
twitter String? // Null | Undefined
@@ -228,6 +229,17 @@ model Donation {
228229
@@index([userId], map: "userId_1")
229230
}
230231

232+
model SocratesUsage {
233+
id String @id @default(auto()) @map("_id") @db.ObjectId
234+
userId String @db.ObjectId
235+
/// UTC date representing the day of usage (midnight).
236+
date DateTime @db.Date
237+
/// Number of hints used on this day.
238+
count Int @default(0)
239+
240+
@@unique([userId, date])
241+
}
242+
231243
model UserToken {
232244
id String @id @map("_id")
233245
created DateTime @db.Date

api/src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export const build = async (
191191

192192
await fastify.register(protectedRoutes.challengeRoutes);
193193
await fastify.register(protectedRoutes.donateRoutes);
194+
await fastify.register(protectedRoutes.socratesRoutes);
194195
await fastify.register(protectedRoutes.protectedCertificateRoutes);
195196
await fastify.register(protectedRoutes.settingRoutes);
196197
await fastify.register(protectedRoutes.userRoutes);

api/src/plugins/__fixtures__/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const newUser = (email: string) => ({
9393
rand: null, // TODO(Post-MVP): delete from schema (it's not used or required).
9494
savedChallenges: [],
9595
sendQuincyEmail: null,
96+
socrates: null,
9697
theme: 'default',
9798
timezone: null,
9899
twitter: null,

api/src/routes/protected/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './challenge.js';
33
export * from './donate.js';
44
export * from './settings.js';
55
export * from './user.js';
6+
export * from './socrates.js';

api/src/routes/protected/settings.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,34 @@ ${isLinkSentWithinLimitTTL}`
616616
}
617617
);
618618

619+
fastify.put(
620+
'/update-socrates',
621+
{
622+
schema: schemas.updateSocrates
623+
},
624+
async (req, reply) => {
625+
const logger = fastify.log.child({ req, res: reply });
626+
try {
627+
await fastify.prisma.user.update({
628+
where: { id: req.user?.id },
629+
data: {
630+
socrates: req.body.socrates
631+
}
632+
});
633+
634+
return {
635+
message: 'flash.socrates-updated',
636+
type: 'success'
637+
} as const;
638+
} catch (err) {
639+
logger.error(err);
640+
fastify.Sentry.captureException(err);
641+
void reply.code(500);
642+
return { message: 'flash.wrong-updating', type: 'danger' } as const;
643+
}
644+
}
645+
);
646+
619647
fastify.put(
620648
'/update-my-honesty',
621649
{

0 commit comments

Comments
 (0)