Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 6x 20x 20x 20x 20x 20x 7x 7x 7x 1x 6x 6x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 12x 13x 6x 6x 6x 6x 12x 7x 7x 7x 7x 16x | import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { limitSchema } from "~app/utils/validation";
import type { PeerId } from "~db/ids";
import { getAccessRole } from "~web/handlers/receipts/utils";
import { authProcedure } from "~web/handlers/trpc";
import { peerIdSchema, receiptIdSchema } from "~web/handlers/validation";
export const procedure = authProcedure
.meta({
title: "Suggest top peers",
description:
"Returns the current user's peers most recently active in debts or a given receipt, for quick selection.",
})
.input(
z.strictObject({
limit: limitSchema,
filterIds: z.array(peerIdSchema).optional(),
options: z
.discriminatedUnion("type", [
z.strictObject({
type: z.literal("not-connected"),
}),
z.strictObject({
type: z.literal("not-connected-receipt"),
receiptId: receiptIdSchema,
}),
])
.optional(),
}),
)
.output(z.strictObject({ items: z.array(peerIdSchema) }))
.query(async ({ input, ctx }) => {
const { database } = ctx;
const filterIds = [...(input.filterIds || []), ctx.auth.userId as PeerId];
const monthAgo = Temporal.Now.plainDateISO().subtract({ months: 1 });
const options = input.options || { type: "all" };
if (options.type === "not-connected-receipt") {
const { receiptId } = options;
const receipt = await database
.selectFrom("receipts")
.select(["ownerUserId", "id"])
.where("id", "=", receiptId)
.limit(1)
.executeTakeFirst();
if (!receipt) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Receipt "${receiptId}" does not exist.`,
});
}
const accessRole = await getAccessRole(
database,
receipt,
ctx.auth.userId,
);
if (!accessRole) {
throw new TRPCError({
code: "FORBIDDEN",
message: `Not enough rights to view receipt "${receiptId}".`,
});
}
const peers = await database
.with("orderedPeers", (qc) =>
qc
.selectFrom("peers")
.where((eb) =>
eb("peers.ownerUserId", "=", ctx.auth.userId).and(
"peers.id",
"not in",
(ebb) =>
ebb
.selectFrom("receiptParticipants")
.innerJoin("peers", (jb) =>
jb.onRef("peers.id", "=", "receiptParticipants.peerId"),
)
.where("receiptParticipants.receiptId", "=", receiptId)
.select("peers.id"),
),
)
.$if(filterIds.length !== 0, (qb) =>
qb.where("peers.id", "not in", filterIds),
)
.leftJoin("receiptParticipants", (qb) =>
qb.onRef("receiptParticipants.peerId", "=", "peers.id"),
)
.leftJoin("receipts", (qb) =>
qb
.onRef("receiptParticipants.receiptId", "=", "receipts.id")
.on("receipts.issued", ">", monthAgo),
)
.distinctOn(["peers.id"])
.select([
"peers.id",
database.fn.count<number>("receipts.id").as("latestCount"),
])
.groupBy("peers.id")
.orderBy("peers.id"),
)
.selectFrom("orderedPeers")
.select(["id"])
.orderBy("latestCount", "desc")
.limit(input.limit)
.execute();
return {
items: peers.map(({ id }) => id),
};
}
if (options.type === "not-connected") {
const peers = await database
.selectFrom("peers")
.where((eb) =>
eb.and([
eb("peers.ownerUserId", "=", ctx.auth.userId),
eb("peers.connectedUserId", "is", null),
]),
)
.leftJoin("debts", (qb) =>
qb
.onRef("debts.peerId", "=", "peers.id")
.on("debts.timestamp", ">", monthAgo),
)
.$if(filterIds.length !== 0, (qb) =>
qb.where("peers.id", "not in", filterIds),
)
.select([
"peers.id",
database.fn.count<number>("debts.id").as("latestCount"),
])
.groupBy(["peers.id"])
.orderBy("latestCount", "desc")
.orderBy("peers.id")
.limit(input.limit)
.execute();
return { items: peers.map(({ id }) => id) };
}
const peers = await database
.selectFrom("peers")
.where((eb) =>
eb("peers.ownerUserId", "=", ctx.auth.userId).and(
"peers.id",
"<>",
ctx.auth.userId as PeerId,
),
)
.leftJoin("debts", (qb) =>
qb
.onRef("peers.id", "=", "debts.peerId")
.on("debts.timestamp", ">", monthAgo),
)
.$if(filterIds.length !== 0, (qb) =>
qb.where("peers.id", "not in", filterIds),
)
.select([
"peers.id",
database.fn.count<number>("debts.id").as("latestCount"),
])
.groupBy("peers.id")
.orderBy("latestCount", "desc")
.orderBy("peers.id")
.limit(input.limit)
.execute();
return { items: peers.map(({ id }) => id) };
});
|