Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add grouped argument so we can extract grouped dates #2601

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 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,35 @@ 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', ${timestampAtTimezone})::date::text`,
'date',
);
} else {
// format to ISO 8601 because we can't use DateTime of gql due to grouped format
readHistoryQuery
.select(
`to_char(view.timestamp, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`,
'date',
)
// limit to 30 events for ungrouped views, reading streak
.limit(30);
}

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

interface userStreakProfileArgs {
Expand Down Expand Up @@ -1443,14 +1472,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
Loading