Skip to content

Commit

Permalink
feat: add grouped argument so we can extract grouped dates
Browse files Browse the repository at this point in the history
  • Loading branch information
capJavert committed Jan 16, 2025
1 parent 04930ba commit ebef8be
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/schema/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ export const typeDefs = /* GraphQL */ `
}
type ReadHistory {
date: DateTime!
date: String!
reads: Int!
}
Expand Down Expand Up @@ -796,7 +796,16 @@ export const typeDefs = /* GraphQL */ `
"""
Get a heatmap of reads per day in a given time frame.
"""
userReadHistory(id: ID!, after: String!, before: String!): [ReadHistory]
userReadHistory(
id: ID!
after: String!
before: String!
"""
Group by day stamp yyyy-MM-dd
"""
grouped: Boolean
): [ReadHistory]
"""
Get the number of articles the user read
"""
Expand Down Expand Up @@ -1105,16 +1114,28 @@ export const getUserReadHistory = async ({
userId,
after,
before,
grouped,
}: {
con: DataSource;
userId: string;
after: Date;
before: Date;
grouped?: boolean;
}) => {
return con
const readHistoryQuery = con
.getRepository(ActiveView)
.createQueryBuilder('view')
.select('view.timestamp', 'date')
.createQueryBuilder('view');

if (grouped) {
readHistoryQuery.select(
`date_trunc('day', view.timestamp)::date::text`,
'date',
);
} else {
readHistoryQuery.select('view.timestamp', 'date');
}

return readHistoryQuery
.addSelect(`count(*) AS "reads"`)
.innerJoin(User, 'user', 'user.id = view.userId')
.where('view.userId = :userId', { userId })
Expand All @@ -1130,6 +1151,7 @@ interface ReadingHistyoryArgs {
after: string;
before: string;
limit?: number;
grouped?: boolean;
}

interface userStreakProfileArgs {
Expand Down Expand Up @@ -1443,14 +1465,15 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
},
userReadHistory: async (
source,
{ id, after, before }: ReadingHistyoryArgs,
{ id, after, before, grouped }: ReadingHistyoryArgs,
ctx: Context,
): Promise<GQLReadingRankHistory[]> =>
getUserReadHistory({
con: ctx.con,
userId: id,
after: new Date(after),
before: new Date(before),
grouped,
}),
userStreak: async (
_,
Expand Down

0 comments on commit ebef8be

Please sign in to comment.