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 ability to sort questions by update / edit date #507

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
2 changes: 2 additions & 0 deletions apps/api/modules/questions/questions.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const questionsPlugin: FastifyPluginAsync = async (fastify) => {
levelId: true,
statusId: true,
acceptedAt: true,
updatedAt: true,
_count: {
select: {
QuestionVote: true,
Expand All @@ -66,6 +67,7 @@ const questionsPlugin: FastifyPluginAsync = async (fastify) => {
_levelId: q.levelId,
_statusId: q.statusId,
acceptedAt: q.acceptedAt?.toISOString(),
updatedAt: q.updatedAt?.toISOString(),
votesCount: q._count.QuestionVote,
};
});
Expand Down
2 changes: 2 additions & 0 deletions apps/api/modules/questions/questions.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const generateGetQuestionsQuerySchema = <
Type.Literal("acceptedAt"),
Type.Literal("level"),
Type.Literal("votesCount"),
Type.Literal("updatedAt"),
]),
order: Type.Union([Type.Literal("asc"), Type.Literal("desc")]),
userId: Type.Integer(),
Expand All @@ -51,6 +52,7 @@ const generateQuestionShape = <
_levelId: Type.Union(args.levels.map((val) => Type.Literal(val))),
_statusId: Type.Union(args.statuses.map((val) => Type.Literal(val))),
acceptedAt: Type.Optional(Type.String({ format: "date-time" })),
updatedAt: Type.Optional(Type.String({ format: "date-time" })),
} as const;
};

Expand Down
4 changes: 3 additions & 1 deletion apps/app/src/lib/order.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryParam } from "../types";

const ordersBy = ["acceptedAt", "level", "votesCount"] as const;
const ordersBy = ["acceptedAt", "level", "votesCount", "updatedAt"] as const;
const orders = ["asc", "desc"] as const;

export const DEFAULT_SORT_BY_QUERY = "acceptedAt*desc";
Expand All @@ -11,6 +11,8 @@ export const sortByLabels: Record<`${OrderBy}*${Order}`, string> = {
"level*desc": "od najtrudniejszych",
"votesCount*asc": "od najmniej popularnych",
"votesCount*desc": "od najpopularniejszych",
"updatedAt*desc": "daty edycji (najnowsze)",
"updatedAt*asc": "daty edycji (najstarsze)",
grzegorzpokorski marked this conversation as resolved.
Show resolved Hide resolved
};

type OrderBy = typeof ordersBy[number];
Expand Down
12 changes: 10 additions & 2 deletions packages/openapi-types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface paths {
level?: string;
limit?: number;
offset?: number;
orderBy?: "acceptedAt" | "level" | "votesCount";
orderBy?: "acceptedAt" | "level" | "votesCount" | "updatedAt";
order?: "asc" | "desc";
userId?: number;
};
Expand All @@ -97,6 +97,8 @@ export interface paths {
_statusId: "pending" | "accepted";
/** Format: date-time */
acceptedAt?: string;
/** Format: date-time */
updatedAt?: string;
votesCount: number;
}[];
meta: {
Expand Down Expand Up @@ -130,6 +132,8 @@ export interface paths {
_statusId: "pending" | "accepted";
/** Format: date-time */
acceptedAt?: string;
/** Format: date-time */
updatedAt?: string;
votesCount: number;
};
};
Expand All @@ -147,7 +151,7 @@ export interface paths {
level?: string;
limit?: number;
offset?: number;
orderBy?: "acceptedAt" | "level" | "votesCount";
orderBy?: "acceptedAt" | "level" | "votesCount" | "updatedAt";
order?: "asc" | "desc";
userId?: number;
};
Expand Down Expand Up @@ -246,6 +250,8 @@ export interface paths {
_statusId: "pending" | "accepted";
/** Format: date-time */
acceptedAt?: string;
/** Format: date-time */
updatedAt?: string;
votesCount: number;
};
};
Expand Down Expand Up @@ -293,6 +299,8 @@ export interface paths {
_statusId: "pending" | "accepted";
/** Format: date-time */
acceptedAt?: string;
/** Format: date-time */
updatedAt?: string;
votesCount: number;
};
};
Expand Down