- Error boundary with Macedonian fallback screen and retry button wrapping all navigation - Pull-to-refresh on all list/detail screens (home, explore, posts, chat, profile, ad detail, post detail) - Accessibility: labels on all interactive elements, roles on Pressable/Button, selectable text for user data - Deep linking: mojmajstor:// scheme configured in app.json, ad/[id] and chat/[id] routes work via Expo Router auto-routing - Pagination: cursor-based paginated queries for ads, posts, messages, chats, reviews with Load More buttons on frontend - Profile: shows customer posts with PostCard, loading/error states, create-post navigation - Auth error messages in Macedonian (Невалидни акредитиви, итн.) All UI text in Macedonian. TypeScript clean, Convex functions deployed.
84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import { query, mutation } from "./_generated/server";
|
||
import { v } from "convex/values";
|
||
import { paginationOptsValidator } from "convex/server";
|
||
|
||
export const listByChat = query({
|
||
args: { token: v.string(), chatId: v.id("chats") },
|
||
handler: async (ctx, args) => {
|
||
const session = await ctx.db
|
||
.query("sessions")
|
||
.withIndex("by_token", (q) => q.eq("token", args.token))
|
||
.first();
|
||
if (!session) throw new Error("Неавторизиран");
|
||
|
||
const chat = await ctx.db.get(args.chatId);
|
||
if (!chat) throw new Error("Разговорот не е пронајден");
|
||
if (!chat.participantIds.includes(session.userId)) {
|
||
throw new Error("Немате пристап до овој разговор");
|
||
}
|
||
|
||
return await ctx.db
|
||
.query("messages")
|
||
.withIndex("by_chat", (q) => q.eq("chatId", args.chatId))
|
||
.order("desc")
|
||
.collect();
|
||
},
|
||
});
|
||
|
||
export const listByChatPaginated = query({
|
||
args: { token: v.string(), chatId: v.id("chats"), paginationOpts: paginationOptsValidator },
|
||
handler: async (ctx, args) => {
|
||
const session = await ctx.db
|
||
.query("sessions")
|
||
.withIndex("by_token", (q) => q.eq("token", args.token))
|
||
.first();
|
||
if (!session) throw new Error("Неавторизиран");
|
||
|
||
const chat = await ctx.db.get(args.chatId);
|
||
if (!chat) throw new Error("Разговорот не е пронајден");
|
||
if (!chat.participantIds.includes(session.userId)) {
|
||
throw new Error("Немате пристап до овој разговор");
|
||
}
|
||
|
||
return await ctx.db
|
||
.query("messages")
|
||
.withIndex("by_chat", (q) => q.eq("chatId", args.chatId))
|
||
.order("desc")
|
||
.paginate(args.paginationOpts);
|
||
},
|
||
});
|
||
|
||
export const send = mutation({
|
||
args: {
|
||
token: v.string(),
|
||
chatId: v.id("chats"),
|
||
content: v.string(),
|
||
imageId: v.optional(v.string()),
|
||
},
|
||
handler: async (ctx, args) => {
|
||
const session = await ctx.db
|
||
.query("sessions")
|
||
.withIndex("by_token", (q) => q.eq("token", args.token))
|
||
.first();
|
||
if (!session) throw new Error("Неавторизиран");
|
||
|
||
const chat = await ctx.db.get(args.chatId);
|
||
if (!chat) throw new Error("Разговорот не е пронајден");
|
||
if (!chat.participantIds.includes(session.userId)) {
|
||
throw new Error("Немате пристап до овој разговор");
|
||
}
|
||
|
||
const now = Date.now();
|
||
const messageId = await ctx.db.insert("messages", {
|
||
chatId: args.chatId,
|
||
senderId: session.userId,
|
||
content: args.content,
|
||
...(args.imageId ? { imageId: args.imageId } : {}),
|
||
createdAt: now,
|
||
});
|
||
|
||
await ctx.db.patch(args.chatId, { lastMessageAt: now });
|
||
|
||
return messageId;
|
||
},
|
||
}); |