new kysely pickTable utils

This commit is contained in:
Marco Pedone 2026-05-11 16:25:16 +02:00
parent 9d03d845d2
commit 2e316f274b
39 changed files with 387 additions and 39 deletions

View file

@ -50,7 +50,7 @@
"framer-motion": "^12.38.0", "framer-motion": "^12.38.0",
"frimousse": "^0.3.0", "frimousse": "^0.3.0",
"jose": "^6.0.12", "jose": "^6.0.12",
"kysely": "^0.28.17", "kysely": "^0.29.0",
"kysely-plugin-serialize": "^0.8.2", "kysely-plugin-serialize": "^0.8.2",
"leaflet": "^1.9.4", "leaflet": "^1.9.4",
"leaflet-defaulticon-compatibility": "^0.1.2", "leaflet-defaulticon-compatibility": "^0.1.2",
@ -9743,13 +9743,13 @@
} }
}, },
"node_modules/kysely": { "node_modules/kysely": {
"version": "0.28.17", "version": "0.29.0",
"resolved": "https://registry.npmjs.org/kysely/-/kysely-0.28.17.tgz", "resolved": "https://registry.npmjs.org/kysely/-/kysely-0.29.0.tgz",
"integrity": "sha512-nbD8lB9EB3wNdMhOCdx5Li8DxnLbvKByylRLcJ1h+4SkrowVeECAyZlyiKMThF7xFdRz0jSQ2MoJr+wXux2y0Q==", "integrity": "sha512-LrQfPUeTW7MXbMvT62moEMnpMTuj9TO3lqjCeLKjM975PJ4Alrl/43f2tlDX7xOsNptKgH4LSNGwIbXwEkLg4g==",
"license": "MIT", "license": "MIT",
"peer": true, "peer": true,
"engines": { "engines": {
"node": ">=20.0.0" "node": ">=22.0.0"
} }
}, },
"node_modules/kysely-plugin-serialize": { "node_modules/kysely-plugin-serialize": {

View file

@ -65,7 +65,7 @@
"framer-motion": "^12.38.0", "framer-motion": "^12.38.0",
"frimousse": "^0.3.0", "frimousse": "^0.3.0",
"jose": "^6.0.12", "jose": "^6.0.12",
"kysely": "^0.28.17", "kysely": "^0.29.0",
"kysely-plugin-serialize": "^0.8.2", "kysely-plugin-serialize": "^0.8.2",
"leaflet": "^1.9.4", "leaflet": "^1.9.4",
"leaflet-defaulticon-compatibility": "^0.1.2", "leaflet-defaulticon-compatibility": "^0.1.2",

View file

@ -60,6 +60,7 @@ export const annunciRouter = createTRPCRouter({
) )
.query(async ({ input }) => { .query(async ({ input }) => {
return await db return await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.selectAll() .selectAll()
.where("id", "=", input.id) .where("id", "=", input.id)
@ -147,6 +148,7 @@ export const annunciRouter = createTRPCRouter({
/**Used for cron job */ /**Used for cron job */
checkAnnunci: apiProcedure.query(async () => { checkAnnunci: apiProcedure.query(async () => {
const hasAnnunci = await db const hasAnnunci = await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select("id") .select("id")
.where("stato", "=", "Attivo") .where("stato", "=", "Attivo")

View file

@ -92,6 +92,7 @@ export const chatSSERouter = createTRPCRouter({
if (input.targetMsgId) { if (input.targetMsgId) {
// If a targetMsgId is provided, we want to fetch messages around that message // If a targetMsgId is provided, we want to fetch messages around that message
const msgs = await db const msgs = await db
.$pickTables<"messages" | "messages_attachments" | "users">()
.selectFrom("messages") .selectFrom("messages")
.innerJoin("users", "users.id", "messages.sender") .innerJoin("users", "users.id", "messages.sender")
.select((eb) => [ .select((eb) => [
@ -151,6 +152,7 @@ export const chatSSERouter = createTRPCRouter({
.orderBy("messages.messageid", "asc") .orderBy("messages.messageid", "asc")
.execute(); .execute();
const prevCheck = await db const prevCheck = await db
.$pickTables<"messages">()
.selectFrom("messages") .selectFrom("messages")
.where("chatid", "=", input.chatId) .where("chatid", "=", input.chatId)
.where("messageid", "<", input.targetMsgId) .where("messageid", "<", input.targetMsgId)
@ -168,6 +170,7 @@ export const chatSSERouter = createTRPCRouter({
const cursor = input.cursor; const cursor = input.cursor;
let qry = db let qry = db
.$pickTables<"messages" | "messages_attachments" | "users">()
.selectFrom("messages") .selectFrom("messages")
.innerJoin("users", "users.id", "messages.sender") .innerJoin("users", "users.id", "messages.sender")
.select((eb) => [ .select((eb) => [
@ -266,6 +269,7 @@ export const chatSSERouter = createTRPCRouter({
) )
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
const newMessage = await db const newMessage = await db
.$pickTables<"messages">()
.insertInto("messages") .insertInto("messages")
.values({ .values({
chatid: input.chatId, chatid: input.chatId,
@ -282,6 +286,7 @@ export const chatSSERouter = createTRPCRouter({
let hasAttachments = false; let hasAttachments = false;
if (input.attachments.length > 0) { if (input.attachments.length > 0) {
const newAttachments = await db const newAttachments = await db
.$pickTables<"messages_attachments">()
.insertInto("messages_attachments") .insertInto("messages_attachments")
.values( .values(
input.attachments.map(({ userStorageId }) => ({ input.attachments.map(({ userStorageId }) => ({
@ -294,6 +299,7 @@ export const chatSSERouter = createTRPCRouter({
hasAttachments = newAttachments.length > 0; hasAttachments = newAttachments.length > 0;
} }
const rs = await db const rs = await db
.$pickTables<"messages" | "users">()
.selectFrom("messages") .selectFrom("messages")
.innerJoin("users", "users.id", "messages.sender") .innerJoin("users", "users.id", "messages.sender")
.select([ .select([
@ -307,6 +313,7 @@ export const chatSSERouter = createTRPCRouter({
.executeTakeFirst(); .executeTakeFirst();
const userInfos = await db const userInfos = await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.select([ .select([
"nome as sender_nome", "nome as sender_nome",
@ -355,6 +362,7 @@ export const chatSSERouter = createTRPCRouter({
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
await db await db
.$pickTables<"messages">()
.deleteFrom("messages") .deleteFrom("messages")
.where("messageid", "=", input.messageId) .where("messageid", "=", input.messageId)
.execute(); .execute();
@ -532,6 +540,7 @@ export const chatSSERouter = createTRPCRouter({
})() ?? null; })() ?? null;
const newMessagesSinceLast = await db const newMessagesSinceLast = await db
.$pickTables<"messages" | "messages_attachments" | "users">()
.selectFrom("messages") .selectFrom("messages")
.innerJoin("users", "users.id", "messages.sender") .innerJoin("users", "users.id", "messages.sender")
.select((eb) => [ .select((eb) => [

View file

@ -119,6 +119,7 @@ export const comunicazioniRouter = createTRPCRouter({
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
const ordine = await db const ordine = await db
.$pickTables<"ordini" | "users" | "prezziario">()
.selectFrom("ordini") .selectFrom("ordini")
.selectAll("ordini") .selectAll("ordini")
.innerJoin("users", "users.id", "ordini.userid") .innerJoin("users", "users.id", "ordini.userid")
@ -242,6 +243,7 @@ export const comunicazioniRouter = createTRPCRouter({
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
const user = await db const user = await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.where("id", "=", input.userId) .where("id", "=", input.userId)

View file

@ -63,6 +63,7 @@ export const etichetteRouter = createTRPCRouter({
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
await db await db
.$pickTables<"user_etichette">()
.insertInto("user_etichette") .insertInto("user_etichette")
.values({ .values({
etichettaId: input.etichettaId, etichettaId: input.etichettaId,
@ -86,6 +87,7 @@ export const etichetteRouter = createTRPCRouter({
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
await db await db
.$pickTables<"user_etichette">()
.deleteFrom("user_etichette") .deleteFrom("user_etichette")
.where("etichettaId", "=", input.etichettaId) .where("etichettaId", "=", input.etichettaId)
.where("userId", "=", input.userId) .where("userId", "=", input.userId)

View file

@ -31,6 +31,7 @@ export const intrestsRouter = createTRPCRouter({
userId, userId,
}); });
const annuncio = await db const annuncio = await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select(["id", "codice", "titolo_it"]) .select(["id", "codice", "titolo_it"])
.where("id", "=", input.annuncioId) .where("id", "=", input.annuncioId)

View file

@ -24,6 +24,7 @@ export const pagamentiRouter = createTRPCRouter({
.query(async ({ input }) => { .query(async ({ input }) => {
try { try {
const ordine = await db const ordine = await db
.$pickTables<"ordini">()
.selectFrom("ordini") .selectFrom("ordini")
.selectAll("ordini") .selectAll("ordini")
.where("ordini.intent_id", "=", input.pIntentId) .where("ordini.intent_id", "=", input.pIntentId)

View file

@ -6,6 +6,7 @@ export const statsRouter = createTRPCRouter({
const userCount = const userCount =
( (
await db await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.select((eb) => [eb.fn.count<number>("id").as("count")]) .select((eb) => [eb.fn.count<number>("id").as("count")])
.executeTakeFirst() .executeTakeFirst()

View file

@ -32,6 +32,7 @@ export const usersRouter = createTRPCRouter({
getUsers: adminProcedure.query(async () => { getUsers: adminProcedure.query(async () => {
return await db return await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.orderBy("id", "desc") .orderBy("id", "desc")
@ -39,6 +40,7 @@ export const usersRouter = createTRPCRouter({
}), }),
getAdmins: adminProcedure.query(async () => { getAdmins: adminProcedure.query(async () => {
return await db return await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.where("isAdmin", "=", true) .where("isAdmin", "=", true)

View file

@ -30,6 +30,7 @@ export const getAnnunciListHandler = async (): Promise<
> => { > => {
try { try {
return await db return await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select([ .select([
"id", "id",
@ -52,6 +53,7 @@ export const getAnnunciListHandler = async (): Promise<
export const getCodici_AnnunciHandler = async (): Promise<string[]> => { export const getCodici_AnnunciHandler = async (): Promise<string[]> => {
try { try {
const codici = await db const codici = await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select("codice") .select("codice")
.where("annunci.web", "=", true) .where("annunci.web", "=", true)
@ -119,6 +121,7 @@ export const getAnnuncioData = async ({
}): Promise<Result<AnnuncioData, getAnnuncioDataError>> => { }): Promise<Result<AnnuncioData, getAnnuncioDataError>> => {
try { try {
const annuncio = await db const annuncio = await db
.$pickTables<"annunci" | "images_refs" | "videos_refs">()
.selectFrom("annunci") .selectFrom("annunci")
.select([ .select([
"anno", "anno",
@ -196,6 +199,7 @@ export const getAnnunciById_rawImgUrls = async ({
}): Promise<AnnunciWithMedia | null> => { }): Promise<AnnunciWithMedia | null> => {
try { try {
const annuncio = await db const annuncio = await db
.$pickTables<"annunci" | "images_refs" | "videos_refs">()
.selectFrom("annunci") .selectFrom("annunci")
.selectAll() .selectAll()
.select((_eb) => [withImages(), withVideos()]) .select((_eb) => [withImages(), withVideos()])
@ -238,6 +242,7 @@ export const get_AnnunciPositionsHandler = async ({
}): Promise<AnnuncioRicercaWPosition[]> => { }): Promise<AnnuncioRicercaWPosition[]> => {
try { try {
let query = db let query = db
.$pickTables<"annunci" | "images_refs" | "videos_refs">()
.selectFrom("annunci") .selectFrom("annunci")
.select((_eb) => [ .select((_eb) => [
"id", "id",
@ -342,6 +347,7 @@ export const getAnnunciRicerca = async ({
}): Promise<AnnuncioRicerca[]> => { }): Promise<AnnuncioRicerca[]> => {
try { try {
let query = db let query = db
.$pickTables<"annunci" | "images_refs" | "videos_refs">()
.selectFrom("annunci") .selectFrom("annunci")
.select((_eb) => [ .select((_eb) => [
"id", "id",
@ -417,6 +423,7 @@ export const getAnnunciRicerca = async ({
export const getOptions_AnnunciHandler = async () => { export const getOptions_AnnunciHandler = async () => {
try { try {
const comuni = await db const comuni = await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select(["comune", "tipo", "consegna", "prezzo"]) .select(["comune", "tipo", "consegna", "prezzo"])
.where("web", "=", true) .where("web", "=", true)
@ -459,6 +466,7 @@ export const editAnnuncioHandler = async ({
}) => { }) => {
try { try {
const annuncio = await db const annuncio = await db
.$pickTables<"annunci">()
.updateTable("annunci") .updateTable("annunci")
.set({ .set({
...data, ...data,
@ -488,6 +496,7 @@ export const getProprietarioDataHandler = async ({
}) => { }) => {
try { try {
const opened_at = await db const opened_at = await db
.$pickTables<"servizio_annunci">()
.selectFrom("servizio_annunci") .selectFrom("servizio_annunci")
.select("open_contatti_at") .select("open_contatti_at")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -498,6 +507,7 @@ export const getProprietarioDataHandler = async ({
} }
const propData = await db const propData = await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select([ .select([
"codice", "codice",
@ -541,10 +551,18 @@ export const removeAnnuncioMedia = async ({
}: ImgToRemove | VideoToRemove) => { }: ImgToRemove | VideoToRemove) => {
try { try {
if (type === "img") { if (type === "img") {
await db.deleteFrom("images_refs").where("img", "=", id).execute(); await db
.$pickTables<"images_refs">()
.deleteFrom("images_refs")
.where("img", "=", id)
.execute();
} }
if (type === "video") { if (type === "video") {
await db.deleteFrom("videos_refs").where("video", "=", id).execute(); await db
.$pickTables<"videos_refs">()
.deleteFrom("videos_refs")
.where("video", "=", id)
.execute();
} }
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -559,6 +577,7 @@ export const getAnnunciSchedaData = async (
): Promise<AnnuncioTemplateData> => { ): Promise<AnnuncioTemplateData> => {
try { try {
const data = await db const data = await db
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select([ .select([
"codice", "codice",

View file

@ -299,6 +299,7 @@ export const passwordChange = async ({
const salt = generateSalt(); const salt = generateSalt();
const hashedPassword = await hashPassword(newpassword, salt); const hashedPassword = await hashPassword(newpassword, salt);
await db await db
.$pickTables<"users">()
.updateTable("users") .updateTable("users")
.set({ .set({
password: hashedPassword, password: hashedPassword,

View file

@ -20,6 +20,7 @@ import { db } from "../db";
export const getComuni = async () => { export const getComuni = async () => {
try { try {
return await db return await db
.$pickTables<"comuni">()
.selectFrom("comuni") .selectFrom("comuni")
.selectAll() .selectAll()
.orderBy("nome", "asc") .orderBy("nome", "asc")
@ -40,7 +41,12 @@ export const editComune = async ({
data: ComuniUpdate; data: ComuniUpdate;
}) => { }) => {
try { try {
await db.updateTable("comuni").set(data).where("id", "=", id).execute(); await db
.$pickTables<"comuni">()
.updateTable("comuni")
.set(data)
.where("id", "=", id)
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -52,7 +58,11 @@ export const editComune = async ({
export const createComune = async (data: NewComuni) => { export const createComune = async (data: NewComuni) => {
const { id, ...rest } = data; const { id, ...rest } = data;
try { try {
await db.insertInto("comuni").values(rest).execute(); await db
.$pickTables<"comuni">()
.insertInto("comuni")
.values(rest)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -64,7 +74,11 @@ export const createComune = async (data: NewComuni) => {
export const deleteComune = async (id: ComuniId) => { export const deleteComune = async (id: ComuniId) => {
try { try {
await db.deleteFrom("comuni").where("id", "=", id).execute(); await db
.$pickTables<"comuni">()
.deleteFrom("comuni")
.where("id", "=", id)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -76,7 +90,11 @@ export const deleteComune = async (id: ComuniId) => {
export const getProvincie = async () => { export const getProvincie = async () => {
try { try {
return await db.selectFrom("provincie").selectAll().execute(); return await db
.$pickTables<"provincie">()
.selectFrom("provincie")
.selectAll()
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -93,7 +111,12 @@ export const editProvincia = async ({
data: ProvincieUpdate; data: ProvincieUpdate;
}) => { }) => {
try { try {
await db.updateTable("provincie").set(data).where("id", "=", id).execute(); await db
.$pickTables<"provincie">()
.updateTable("provincie")
.set(data)
.where("id", "=", id)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -106,7 +129,11 @@ export const editProvincia = async ({
export const createProvincia = async (data: NewProvincie) => { export const createProvincia = async (data: NewProvincie) => {
const { id, ...rest } = data; const { id, ...rest } = data;
try { try {
await db.insertInto("provincie").values(rest).execute(); await db
.$pickTables<"provincie">()
.insertInto("provincie")
.values(rest)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -118,7 +145,11 @@ export const createProvincia = async (data: NewProvincie) => {
export const deleteProvincia = async (id: ProvincieId) => { export const deleteProvincia = async (id: ProvincieId) => {
try { try {
await db.deleteFrom("provincie").where("id", "=", id).execute(); await db
.$pickTables<"provincie">()
.deleteFrom("provincie")
.where("id", "=", id)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -131,6 +162,7 @@ export const deleteProvincia = async (id: ProvincieId) => {
export const getNazioni = async () => { export const getNazioni = async () => {
try { try {
return await db return await db
.$pickTables<"nazioni">()
.selectFrom("nazioni") .selectFrom("nazioni")
.selectAll() .selectAll()
.orderBy(sql`nome = 'ITALIA'`, "desc") .orderBy(sql`nome = 'ITALIA'`, "desc")
@ -152,7 +184,12 @@ export const editNazione = async ({
data: NazioniUpdate; data: NazioniUpdate;
}) => { }) => {
try { try {
await db.updateTable("nazioni").set(data).where("id", "=", id).execute(); await db
.$pickTables<"nazioni">()
.updateTable("nazioni")
.set(data)
.where("id", "=", id)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -165,7 +202,11 @@ export const editNazione = async ({
export const createNazione = async (data: NewNazioni) => { export const createNazione = async (data: NewNazioni) => {
const { id, ...rest } = data; const { id, ...rest } = data;
try { try {
await db.insertInto("nazioni").values(rest).execute(); await db
.$pickTables<"nazioni">()
.insertInto("nazioni")
.values(rest)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -177,7 +218,11 @@ export const createNazione = async (data: NewNazioni) => {
export const deleteNazione = async (id: NazioniId) => { export const deleteNazione = async (id: NazioniId) => {
try { try {
await db.deleteFrom("nazioni").where("id", "=", id).execute(); await db
.$pickTables<"nazioni">()
.deleteFrom("nazioni")
.where("id", "=", id)
.execute();
return "ok"; return "ok";
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({

View file

@ -19,6 +19,9 @@ export type ActiveChatsType = Pick<Chats, "chatid" | "created_at"> &
export const getActiveInfos = async (): Promise<ActiveChatsType[]> => { export const getActiveInfos = async (): Promise<ActiveChatsType[]> => {
const chats = await db const chats = await db
.$pickTables<
"messages" | "chats" | "users" | "etichette" | "chats_etichette"
>()
.selectFrom("chats") .selectFrom("chats")
.innerJoin("users", "users.id", "chats.user_ref") .innerJoin("users", "users.id", "chats.user_ref")
.select((eb) => [ .select((eb) => [
@ -86,6 +89,7 @@ export const getChatInfo = async ({
export const getChatByUser = async ({ userId }: { userId: UsersId }) => { export const getChatByUser = async ({ userId }: { userId: UsersId }) => {
try { try {
const chat = await db const chat = await db
.$pickTables<"chats">()
.selectFrom("chats") .selectFrom("chats")
.select(["chatid", getUserInfo()]) .select(["chatid", getUserInfo()])
.where("chats.user_ref", "=", userId) .where("chats.user_ref", "=", userId)
@ -98,6 +102,7 @@ export const getChatByUser = async ({ userId }: { userId: UsersId }) => {
}); });
const chat = await db const chat = await db
.$pickTables<"chats">()
.selectFrom("chats") .selectFrom("chats")
.select(["chatid", getUserInfo()]) .select(["chatid", getUserInfo()])
.where("chats.user_ref", "=", userId) .where("chats.user_ref", "=", userId)
@ -124,6 +129,7 @@ export const deleteChatHandler = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"chats">()
.deleteFrom("chats") .deleteFrom("chats")
.where("chats.chatid", "=", chatId) .where("chats.chatid", "=", chatId)
.returning("chatid") .returning("chatid")
@ -143,6 +149,7 @@ export const getUserByChatHandler = async ({
chatId: ChatsChatid; chatId: ChatsChatid;
}) => { }) => {
const chat = await db const chat = await db
.$pickTables<"chats">()
.selectFrom("chats") .selectFrom("chats")
.select(["chatid", "created_at", "user_ref", getUserInfo()]) .select(["chatid", "created_at", "user_ref", getUserInfo()])
.where("chats.chatid", "=", chatId) .where("chats.chatid", "=", chatId)
@ -162,6 +169,7 @@ export const getChatByUserIdHandler = async ({
userId: UsersId; userId: UsersId;
}) => { }) => {
const chat = await db const chat = await db
.$pickTables<"chats">()
.selectFrom("chats") .selectFrom("chats")
.select("chatid") .select("chatid")
.where("chats.user_ref", "=", userId) .where("chats.user_ref", "=", userId)

View file

@ -5,7 +5,11 @@ import type { Querier } from "~/server/db";
export const getAllEtichette = async ({ db }: { db: Querier }) => { export const getAllEtichette = async ({ db }: { db: Querier }) => {
try { try {
return await db.selectFrom("etichette").selectAll().execute(); return await db
.$pickTables<"etichette">()
.selectFrom("etichette")
.selectAll()
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -25,6 +29,7 @@ export const addEtichetta = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"etichette">()
.insertInto("etichette") .insertInto("etichette")
.returning("id_etichetta") .returning("id_etichetta")
.values({ color_hex, title }) .values({ color_hex, title })
@ -50,6 +55,7 @@ export const updateEtichetta = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"etichette">()
.updateTable("etichette") .updateTable("etichette")
.returning("id_etichetta") .returning("id_etichetta")
.set({ color_hex, title }) .set({ color_hex, title })
@ -72,6 +78,7 @@ export const removeEtichetta = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"etichette">()
.deleteFrom("etichette") .deleteFrom("etichette")
.where("id_etichetta", "=", etichettaid) .where("id_etichetta", "=", etichettaid)
.execute(); .execute();
@ -93,6 +100,7 @@ export const getChatEtichette = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"etichette" | "chats_etichette">()
.selectFrom("etichette") .selectFrom("etichette")
.innerJoin( .innerJoin(
"chats_etichette", "chats_etichette",
@ -125,6 +133,7 @@ export const addChatEtichette = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"chats_etichette">()
.insertInto("chats_etichette") .insertInto("chats_etichette")
.returning("chatid") .returning("chatid")
.values({ chatid, etichettaid }) .values({ chatid, etichettaid })
@ -149,6 +158,7 @@ export const removeChatEtichette = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"chats_etichette">()
.deleteFrom("chats_etichette") .deleteFrom("chats_etichette")
.where("chatid", "=", chatid) .where("chatid", "=", chatid)
.where("etichettaid", "=", etichettaid) .where("etichettaid", "=", etichettaid)

View file

@ -115,7 +115,11 @@ type NewEventQueueProps = Override<NewEventQueue, { data: EventDataTypes }>;
export const addEventToQueue = async (data: NewEventQueueProps) => { export const addEventToQueue = async (data: NewEventQueueProps) => {
try { try {
await db.insertInto("event_queue").values(data).execute(); await db
.$pickTables<"event_queue">()
.insertInto("event_queue")
.values(data)
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -127,6 +131,7 @@ export const addEventToQueue = async (data: NewEventQueueProps) => {
const getEventsFromQueue = async () => { const getEventsFromQueue = async () => {
try { try {
const events = await db const events = await db
.$pickTables<"event_queue">()
.selectFrom("event_queue") .selectFrom("event_queue")
.selectAll() .selectAll()
.where("event_queue.isActive", "=", true) .where("event_queue.isActive", "=", true)
@ -200,6 +205,7 @@ export const processEvents = async () => {
export const getEmailQueue = async () => { export const getEmailQueue = async () => {
try { try {
const events = await db const events = await db
.$pickTables<"event_queue" | "users">()
.selectFrom("event_queue") .selectFrom("event_queue")
.selectAll("event_queue") .selectAll("event_queue")
.select((eb) => [ .select((eb) => [
@ -227,6 +233,7 @@ export const getEmailQueue = async () => {
export const userEmailQueue = async (userId: UsersId) => { export const userEmailQueue = async (userId: UsersId) => {
try { try {
const events = await db const events = await db
.$pickTables<"event_queue">()
.selectFrom("event_queue") .selectFrom("event_queue")
.selectAll() .selectAll()
.where(sql`data->>'tipo'`, "=", "email") .where(sql`data->>'tipo'`, "=", "email")
@ -244,6 +251,7 @@ export const userEmailQueue = async (userId: UsersId) => {
export const removeEventFromQueue = async (eventId: EventQueueEventId) => { export const removeEventFromQueue = async (eventId: EventQueueEventId) => {
try { try {
await db await db
.$pickTables<"event_queue">()
.deleteFrom("event_queue") .deleteFrom("event_queue")
.where("event_id", "=", eventId) .where("event_id", "=", eventId)
.execute(); .execute();
@ -259,6 +267,7 @@ export const removeEventFromQueue = async (eventId: EventQueueEventId) => {
export const clearEmailQueue = async () => { export const clearEmailQueue = async () => {
try { try {
await db await db
.$pickTables<"event_queue">()
.deleteFrom("event_queue") .deleteFrom("event_queue")
.where(sql`data->>'tipo'`, "=", "email") .where(sql`data->>'tipo'`, "=", "email")
.execute(); .execute();
@ -274,6 +283,7 @@ export const clearEmailQueue = async () => {
export const clearUserEmailQueue = async (userId: UsersId) => { export const clearUserEmailQueue = async (userId: UsersId) => {
try { try {
await db await db
.$pickTables<"event_queue">()
.deleteFrom("event_queue") .deleteFrom("event_queue")
.where(sql`data->>'tipo'`, "=", "email") .where(sql`data->>'tipo'`, "=", "email")
.where(sql`data->'data'->>'userId'`, "=", userId) .where(sql`data->'data'->>'userId'`, "=", userId)
@ -291,6 +301,7 @@ export const clearUserEmailQueue = async (userId: UsersId) => {
export const disableUserEmailQueue = async (userId: UsersId) => { export const disableUserEmailQueue = async (userId: UsersId) => {
try { try {
await db await db
.$pickTables<"event_queue">()
.updateTable("event_queue") .updateTable("event_queue")
.set({ isActive: false }) .set({ isActive: false })
.where(sql`data->>'tipo'`, "=", "email") .where(sql`data->>'tipo'`, "=", "email")
@ -311,6 +322,7 @@ export const updateEvent = async (
) => { ) => {
try { try {
await db await db
.$pickTables<"event_queue">()
.updateTable("event_queue") .updateTable("event_queue")
.set(data) .set(data)
.where("event_id", "=", eventId) .where("event_id", "=", eventId)

View file

@ -47,6 +47,7 @@ export const getClientFromCF = async (cf: string) => {
export const exportUserToFIC = async (userId: UsersId) => { export const exportUserToFIC = async (userId: UsersId) => {
try { try {
const user = await db const user = await db
.$pickTables<"users" | "users_anagrafica" | "nazioni" | "comuni">()
.selectFrom("users") .selectFrom("users")
.innerJoin("users_anagrafica", "users_anagrafica.userid", "users.id") .innerJoin("users_anagrafica", "users_anagrafica.userid", "users.id")
.select([ .select([

View file

@ -5,6 +5,7 @@ import { db } from "~/server/db";
export const EditFlagHandler = async (input: Flags) => { export const EditFlagHandler = async (input: Flags) => {
try { try {
return await db return await db
.$pickTables<"flags">()
.updateTable("flags") .updateTable("flags")
.set({ .set({
value: input.value, value: input.value,
@ -21,7 +22,11 @@ export const EditFlagHandler = async (input: Flags) => {
}; };
export const GetFlagHandler = async () => { export const GetFlagHandler = async () => {
try { try {
return await db.selectFrom("flags").selectAll().execute(); return await db
.$pickTables<"flags">()
.selectFrom("flags")
.selectAll()
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -33,6 +38,7 @@ export const GetFlagHandler = async () => {
export const GetFlagValueHandler = async (id: string) => { export const GetFlagValueHandler = async (id: string) => {
try { try {
const flag = await db const flag = await db
.$pickTables<"flags">()
.selectFrom("flags") .selectFrom("flags")
.select("value") .select("value")
.where("id", "=", id as FlagsId) .where("id", "=", id as FlagsId)
@ -48,7 +54,11 @@ export const GetFlagValueHandler = async (id: string) => {
export const RemoveFlagHandler = async (id: FlagsId) => { export const RemoveFlagHandler = async (id: FlagsId) => {
try { try {
await db.deleteFrom("flags").where("id", "=", id).execute(); await db
.$pickTables<"flags">()
.deleteFrom("flags")
.where("id", "=", id)
.execute();
return true; return true;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -60,6 +70,7 @@ export const RemoveFlagHandler = async (id: FlagsId) => {
export const AddFlagHandler = async (input: Flags) => { export const AddFlagHandler = async (input: Flags) => {
try { try {
await db await db
.$pickTables<"flags">()
.insertInto("flags") .insertInto("flags")
.values({ .values({
id: input.id, id: input.id,

View file

@ -21,6 +21,7 @@ export async function createInviteToken(email: string) {
try { try {
// Store in database // Store in database
await db await db
.$pickTables<"user_invites">()
.insertInto("user_invites") .insertInto("user_invites")
.values({ .values({
email, email,
@ -142,6 +143,7 @@ async function verifyInviteToken(
): Promise<Result<UserInvites, TokenVerErrors>> { ): Promise<Result<UserInvites, TokenVerErrors>> {
try { try {
const invite = await db const invite = await db
.$pickTables<"user_invites">()
.selectFrom("user_invites") .selectFrom("user_invites")
.selectAll("user_invites") .selectAll("user_invites")
.where("token", "=", token) .where("token", "=", token)
@ -166,7 +168,11 @@ async function verifyInviteToken(
async function consumeInviteToken(token: string) { async function consumeInviteToken(token: string) {
try { try {
await db.deleteFrom("user_invites").where("token", "=", token).execute(); await db
.$pickTables<"user_invites">()
.deleteFrom("user_invites")
.where("token", "=", token)
.execute();
} catch (e) { } catch (e) {
console.error("Error consuming invite token:", e); console.error("Error consuming invite token:", e);
throw new TRPCError({ throw new TRPCError({

View file

@ -61,6 +61,7 @@ export const preparePayment = async (
const { servizioId, type } = props; const { servizioId, type } = props;
const servizio = await getServizioById(servizioId); const servizio = await getServizioById(servizioId);
const inactiveOrders = await db const inactiveOrders = await db
.$pickTables<"ordini">()
.selectFrom("ordini") .selectFrom("ordini")
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -331,6 +332,7 @@ export const preparePayment = async (
const { rinnovoId } = props; const { rinnovoId } = props;
const ordiniRinnovo = await db const ordiniRinnovo = await db
.$pickTables<"ordini">()
.selectFrom("ordini") .selectFrom("ordini")
.selectAll() .selectAll()
.where("rinnovo_id", "=", rinnovoId) .where("rinnovo_id", "=", rinnovoId)
@ -380,6 +382,7 @@ export const preparePayment = async (
} }
// nessun rinnovo preesistente, crearne uno nuovo // nessun rinnovo preesistente, crearne uno nuovo
const rinnovo = await db const rinnovo = await db
.$pickTables<"rinnovi">()
.selectFrom("rinnovi") .selectFrom("rinnovi")
.selectAll() .selectAll()
.where("id", "=", rinnovoId) .where("id", "=", rinnovoId)
@ -429,6 +432,7 @@ export const preparePayment = async (
export const getPagamentoDataForCheckout = async (ordineId: OrdiniOrdineId) => { export const getPagamentoDataForCheckout = async (ordineId: OrdiniOrdineId) => {
try { try {
return await db return await db
.$pickTables<"ordini" | "users" | "prezziario">()
.selectFrom("ordini") .selectFrom("ordini")
.select([ .select([
"ordini.ordine_id", "ordini.ordine_id",
@ -486,6 +490,7 @@ export const AccontoSolver = async (
switch (tipologia) { switch (tipologia) {
case tipologiaPosizioneEnum.enum.Transitorio: { case tipologiaPosizioneEnum.enum.Transitorio: {
const pack = await db const pack = await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("isActive", "=", true) .where("isActive", "=", true)
@ -505,6 +510,7 @@ export const AccontoSolver = async (
case tipologiaPosizioneEnum.enum.Stabile: { case tipologiaPosizioneEnum.enum.Stabile: {
const pack = await db const pack = await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("isActive", "=", true) .where("isActive", "=", true)
@ -545,6 +551,7 @@ export const SaldoSolver = async ({
} }
const pack = await db const pack = await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("isActive", "=", true) .where("isActive", "=", true)
@ -563,6 +570,7 @@ export const SaldoSolver = async ({
case tipologiaPosizioneEnum.enum.Stabile: { case tipologiaPosizioneEnum.enum.Stabile: {
const budget_step = budget > 700 ? 2 : budget > 500 ? 1 : 0; const budget_step = budget > 700 ? 2 : budget > 500 ? 1 : 0;
const pack = await db const pack = await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("isActive", "=", true) .where("isActive", "=", true)
@ -608,6 +616,7 @@ export const RinnovoSolver = async (
const permanenza = rinnovoDurationToPermanenza(decorrenza, scadenza); const permanenza = rinnovoDurationToPermanenza(decorrenza, scadenza);
const pack = await db const pack = await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("isActive", "=", true) .where("isActive", "=", true)
@ -674,6 +683,7 @@ export const getRicevutaData = async (
): Promise<{ title: string; data: RicevutaProps }> => { ): Promise<{ title: string; data: RicevutaProps }> => {
try { try {
const ordine = await db const ordine = await db
.$pickTables<"prezziario" | "ordini">()
.selectFrom("ordini") .selectFrom("ordini")
.select([ .select([
"ordini.ordine_id", "ordini.ordine_id",
@ -693,6 +703,7 @@ export const getRicevutaData = async (
); );
const anagrafica = await db const anagrafica = await db
.$pickTables<"users" | "users_anagrafica">()
.selectFrom("users") .selectFrom("users")
.select("users.username") .select("users.username")
.innerJoin("users_anagrafica", "users_anagrafica.userid", "users.id") .innerJoin("users_anagrafica", "users_anagrafica.userid", "users.id")

View file

@ -48,6 +48,7 @@ export const getDataPerAcquisto = async (servizioId: ServizioServizioId) => {
} }
const userData = await db const userData = await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.select([ .select([
"users.id", "users.id",
@ -68,6 +69,7 @@ export const getDataPerAcquisto = async (servizioId: ServizioServizioId) => {
}); });
} }
const anagrafica = await db const anagrafica = await db
.$pickTables<"users_anagrafica">()
.selectFrom("users_anagrafica") .selectFrom("users_anagrafica")
.selectAll("users_anagrafica") .selectAll("users_anagrafica")
.where("users_anagrafica.userid", "=", servizio.user_id) .where("users_anagrafica.userid", "=", servizio.user_id)
@ -102,6 +104,7 @@ export const processServizioOnboard = async ({
await db.transaction().execute(async (tx) => { await db.transaction().execute(async (tx) => {
// Update user and anagrafica data // Update user and anagrafica data
await tx await tx
.$pickTables<"users">()
.updateTable("users") .updateTable("users")
.set(user) .set(user)
.where("id", "=", userId) .where("id", "=", userId)
@ -110,6 +113,7 @@ export const processServizioOnboard = async ({
const { idanagrafica, ...rest } = anagrafica; const { idanagrafica, ...rest } = anagrafica;
if (idanagrafica) { if (idanagrafica) {
await tx await tx
.$pickTables<"users_anagrafica">()
.updateTable("users_anagrafica") .updateTable("users_anagrafica")
.set({ .set({
...rest, ...rest,
@ -125,6 +129,7 @@ export const processServizioOnboard = async ({
); );
} else { } else {
await tx await tx
.$pickTables<"users_anagrafica">()
.insertInto("users_anagrafica") .insertInto("users_anagrafica")
.values({ .values({
...rest, ...rest,
@ -137,6 +142,7 @@ export const processServizioOnboard = async ({
} }
// Update servizio data // Update servizio data
return await tx return await tx
.$pickTables<"servizio">()
.updateTable("servizio") .updateTable("servizio")
.set({ ...servizio, onboardOk: true }) .set({ ...servizio, onboardOk: true })
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -167,6 +173,7 @@ export const attivaServizio_SaltaPagamentoAcconto = async (
try { try {
return await db.transaction().execute(async (tx) => { return await db.transaction().execute(async (tx) => {
const ordine = await tx const ordine = await tx
.$pickTables<"ordini" | "users" | "prezziario">()
.selectFrom("ordini") .selectFrom("ordini")
.innerJoin("users", "users.id", "ordini.userid") .innerJoin("users", "users.id", "ordini.userid")
.innerJoin("prezziario", "prezziario.idprezziario", "ordini.packid") .innerJoin("prezziario", "prezziario.idprezziario", "ordini.packid")
@ -186,6 +193,7 @@ export const attivaServizio_SaltaPagamentoAcconto = async (
const user = await getUser({ db: tx, userId: ordine.userId }); const user = await getUser({ db: tx, userId: ordine.userId });
const servizio = await tx const servizio = await tx
.$pickTables<"servizio">()
.updateTable("servizio") .updateTable("servizio")
.set({ .set({
decorrenza: new Date(), decorrenza: new Date(),
@ -268,6 +276,7 @@ export const AdminAttivaServizio_Ufficio = async (
try { try {
return await db.transaction().execute(async (tx) => { return await db.transaction().execute(async (tx) => {
const data = await tx const data = await tx
.$pickTables<"users" | "servizio">()
.selectFrom("servizio") .selectFrom("servizio")
.select(["servizio.user_id", "servizio.interruzioneDays"]) .select(["servizio.user_id", "servizio.interruzioneDays"])
.innerJoin("users", "users.id", "servizio.user_id") .innerJoin("users", "users.id", "servizio.user_id")
@ -369,6 +378,15 @@ export const getAllServizioAnnunci = async (
try { try {
const data = await withParsedDates( const data = await withParsedDates(
db db
.$pickTables<
| "servizio"
| "users_anagrafica"
| "users_storage"
| "servizio_annunci"
| "prezziario"
| "ordini"
| "annunci"
>()
.selectFrom("servizio") .selectFrom("servizio")
.where("user_id", "=", userId) .where("user_id", "=", userId)
.leftJoin( .leftJoin(
@ -507,6 +525,15 @@ export const getServizioDataById = async (
try { try {
const data = await withParsedDates( const data = await withParsedDates(
db db
.$pickTables<
| "users_anagrafica"
| "servizio"
| "servizio_annunci"
| "users_storage"
| "annunci"
| "ordini"
| "prezziario"
>()
.selectFrom("servizio") .selectFrom("servizio")
.leftJoin( .leftJoin(
"users_anagrafica", "users_anagrafica",
@ -654,6 +681,7 @@ export const addServizioAnnunci = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"servizio_annunci">()
.insertInto("servizio_annunci") .insertInto("servizio_annunci")
.values( .values(
annunci.map((annuncio, idx) => ({ annunci.map((annuncio, idx) => ({
@ -682,6 +710,7 @@ export const getAnnunciAvailableToAdd = async (
try { try {
return await db.transaction().execute(async (tx) => { return await db.transaction().execute(async (tx) => {
const servizioAnnunci = await tx const servizioAnnunci = await tx
.$pickTables<"servizio_annunci">()
.selectFrom("servizio_annunci") .selectFrom("servizio_annunci")
.select("annunci_id") .select("annunci_id")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -689,6 +718,7 @@ export const getAnnunciAvailableToAdd = async (
const ids = servizioAnnunci.map((item) => item.annunci_id); const ids = servizioAnnunci.map((item) => item.annunci_id);
let qry = tx let qry = tx
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.select(["id", "codice", "titolo_it"]) .select(["id", "codice", "titolo_it"])
.where("annunci.web", "=", true) .where("annunci.web", "=", true)
@ -716,6 +746,7 @@ export const SbloccaContatti = async ({
}) => { }) => {
try { try {
const alreadyUnlocked = await db const alreadyUnlocked = await db
.$pickTables<"servizio_annunci">()
.selectFrom("servizio_annunci") .selectFrom("servizio_annunci")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.where("open_contatti_at", "is not", null) .where("open_contatti_at", "is not", null)
@ -727,6 +758,7 @@ export const SbloccaContatti = async ({
const data = await db.transaction().execute(async (tx) => { const data = await db.transaction().execute(async (tx) => {
await tx await tx
.$pickTables<"servizio_annunci">()
.updateTable("servizio_annunci") .updateTable("servizio_annunci")
.set({ .set({
open_contatti_at: new Date(), open_contatti_at: new Date(),
@ -736,24 +768,28 @@ export const SbloccaContatti = async ({
.execute(); .execute();
const servizio = await tx const servizio = await tx
.$pickTables<"servizio">()
.selectFrom("servizio") .selectFrom("servizio")
.select("user_id") .select("user_id")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.executeTakeFirstOrThrow(() => new Error("Servizio not found")); .executeTakeFirstOrThrow(() => new Error("Servizio not found"));
const user = await tx const user = await tx
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.select(["id", "username", "email", "telefono", "comms_enabled"]) .select(["id", "username", "email", "telefono", "comms_enabled"])
.where("id", "=", servizio.user_id) .where("id", "=", servizio.user_id)
.executeTakeFirstOrThrow(() => new Error("User not found")); .executeTakeFirstOrThrow(() => new Error("User not found"));
const anagrafica = await tx const anagrafica = await tx
.$pickTables<"users_anagrafica">()
.selectFrom("users_anagrafica") .selectFrom("users_anagrafica")
.select("sesso") .select("sesso")
.where("userid", "=", servizio.user_id) .where("userid", "=", servizio.user_id)
.executeTakeFirstOrThrow(() => new Error("Anagrafica not found")); .executeTakeFirstOrThrow(() => new Error("Anagrafica not found"));
const annuncio = await tx const annuncio = await tx
.$pickTables<"annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.where("id", "=", annuncioId) .where("id", "=", annuncioId)
.select([ .select([
@ -886,6 +922,7 @@ export const interruzioneServizio = async (
} }
await db await db
.$pickTables<"servizio">()
.updateTable("servizio") .updateTable("servizio")
.set({ .set({
isInterrotto: true, isInterrotto: true,
@ -954,6 +991,7 @@ export const InteractionLogicTipologia = async ({
return { status: "invalid" as const }; return { status: "invalid" as const };
} }
const servizi = await db const servizi = await db
.$pickTables<"servizio" | "servizio_annunci">()
.selectFrom("servizio") .selectFrom("servizio")
.select(["tipologia", "servizio_id"]) .select(["tipologia", "servizio_id"])
.where("tipologia", "=", parsedTipologia.data) .where("tipologia", "=", parsedTipologia.data)
@ -1053,6 +1091,7 @@ export const getCompatibileAnnunci = async ({
const interests = await GetUserInterests(servizio.user_id); const interests = await GetUserInterests(servizio.user_id);
let qry = db let qry = db
.$pickTables<"annunci" | "servizio_annunci">()
.selectFrom("annunci") .selectFrom("annunci")
.distinctOn("annunci.id") .distinctOn("annunci.id")
.select((eb) => [ .select((eb) => [
@ -1178,6 +1217,14 @@ export const getRichieste = async (): Promise<RichiesteData[]> => {
try { try {
const data = await withParsedDates( const data = await withParsedDates(
db db
.$pickTables<
| "servizio"
| "users"
| "servizio_annunci"
| "annunci"
| "ordini"
| "prezziario"
>()
.selectFrom("servizio") .selectFrom("servizio")
.innerJoin("users", "users.id", "servizio.user_id") .innerJoin("users", "users.id", "servizio.user_id")
.select([ .select([
@ -1290,6 +1337,7 @@ export const getRichieste = async (): Promise<RichiesteData[]> => {
export const getIncrociAnnuncio = async (annuncioId: AnnunciId) => { export const getIncrociAnnuncio = async (annuncioId: AnnunciId) => {
try { try {
const data = await db const data = await db
.$pickTables<"servizio_annunci" | "users" | "servizio">()
.selectFrom("servizio_annunci") .selectFrom("servizio_annunci")
.innerJoin( .innerJoin(
"servizio", "servizio",

View file

@ -14,6 +14,7 @@ export const getMultipleWithRelations = async ({
}) => { }) => {
const files = await fetchFiles(); const files = await fetchFiles();
const userStorage = await db const userStorage = await db
.$pickTables<"users_storage" | "users">()
.selectFrom("users_storage") .selectFrom("users_storage")
.selectAll("users_storage") .selectAll("users_storage")
.innerJoin("users", "users.id", "users_storage.userId") .innerJoin("users", "users.id", "users_storage.userId")
@ -48,6 +49,7 @@ export const retrieveUserFiles = async ({ userId }: { userId: UsersId }) => {
try { try {
//all files linked to the user //all files linked to the user
const files = await db const files = await db
.$pickTables<"users_storage">()
.selectFrom("users_storage") .selectFrom("users_storage")
.select(["storageId", "from_admin"]) .select(["storageId", "from_admin"])
.where("userId", "=", userId) .where("userId", "=", userId)
@ -58,6 +60,7 @@ export const retrieveUserFiles = async ({ userId }: { userId: UsersId }) => {
//remove entries in users_storage for files that no longer exist in storage //remove entries in users_storage for files that no longer exist in storage
await db await db
.$pickTables<"users_storage">()
.deleteFrom("users_storage") .deleteFrom("users_storage")
.where("userId", "=", userId) .where("userId", "=", userId)
.where("storageId", "in", unavailable) .where("storageId", "in", unavailable)
@ -91,6 +94,7 @@ export const retrieveChatMessageFiles = async ({
}) => { }) => {
try { try {
const messageFiles = await db const messageFiles = await db
.$pickTables<"messages_attachments" | "users_storage">()
.selectFrom("messages_attachments") .selectFrom("messages_attachments")
.innerJoin( .innerJoin(
"users_storage", "users_storage",
@ -108,6 +112,7 @@ export const retrieveChatMessageFiles = async ({
//remove entries in users_storage for files that no longer exist in storage //remove entries in users_storage for files that no longer exist in storage
await db await db
.$pickTables<"users_storage">()
.deleteFrom("users_storage") .deleteFrom("users_storage")
.where("storageId", "in", unavailable) .where("storageId", "in", unavailable)
.execute(); .execute();
@ -124,6 +129,7 @@ export const retrieveChatMessageFiles = async ({
export const getAllWithFromAdmin = async () => { export const getAllWithFromAdmin = async () => {
try { try {
const files = await db const files = await db
.$pickTables<"users_storage">()
.selectFrom("users_storage") .selectFrom("users_storage")
.select(["storageId", "from_admin"]) .select(["storageId", "from_admin"])
.execute(); .execute();
@ -149,6 +155,7 @@ export const getAllWithFromAdmin = async () => {
export const addUserStorage = async (data: NewUsersStorage) => { export const addUserStorage = async (data: NewUsersStorage) => {
try { try {
return await db return await db
.$pickTables<"users_storage">()
.insertInto("users_storage") .insertInto("users_storage")
.values(data) .values(data)
.onConflict((oc) => oc.columns(["userId", "storageId"]).doNothing()) .onConflict((oc) => oc.columns(["userId", "storageId"]).doNothing())
@ -171,6 +178,7 @@ export const deleteUserStorageEntry = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"users_storage">()
.deleteFrom("users_storage") .deleteFrom("users_storage")
.where("userId", "=", userId) .where("userId", "=", userId)
.where("storageId", "=", storageId) .where("storageId", "=", storageId)
@ -191,6 +199,7 @@ export const purgeFileFromUserStorages = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"users_storage">()
.deleteFrom("users_storage") .deleteFrom("users_storage")
.where("storageId", "=", storageId) .where("storageId", "=", storageId)
.execute(); .execute();

View file

@ -42,6 +42,7 @@ export const whIntentCreatedHandler = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"ordini">()
.updateTable("ordini") .updateTable("ordini")
.set({ .set({
intent_id: pIntentId, intent_id: pIntentId,
@ -72,6 +73,7 @@ export const whIntentSucceededHandler = async ({
await db.transaction().execute(async (trx) => { await db.transaction().execute(async (trx) => {
const ordine = await trx const ordine = await trx
.$pickTables<"ordini">()
.updateTable("ordini") .updateTable("ordini")
.set({ .set({
paid_at: new Date(), paid_at: new Date(),
@ -90,6 +92,7 @@ export const whIntentSucceededHandler = async ({
const condizioniFilename = `condizioni_contrattuali_${(ordine.paid_at || ordine.created_at).toISOString()}.pdf`; const condizioniFilename = `condizioni_contrattuali_${(ordine.paid_at || ordine.created_at).toISOString()}.pdf`;
const ricevutaFilename = `ricevuta_cortesia_${(ordine.paid_at || ordine.created_at).toISOString()}.pdf`; const ricevutaFilename = `ricevuta_cortesia_${(ordine.paid_at || ordine.created_at).toISOString()}.pdf`;
const condizioni = await trx const condizioni = await trx
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.select("testo_condizioni") .select("testo_condizioni")
.where("idprezziario", "=", ordine.packid) .where("idprezziario", "=", ordine.packid)
@ -102,6 +105,7 @@ export const whIntentSucceededHandler = async ({
// cleanup ordini non pagati e non attivi per lo stesso utente, stesso tipo e servizio (es. se paga acconto, cancellare altri ordini acconto non pagati) // cleanup ordini non pagati e non attivi per lo stesso utente, stesso tipo e servizio (es. se paga acconto, cancellare altri ordini acconto non pagati)
await trx await trx
.$pickTables<"ordini">()
.deleteFrom("ordini") .deleteFrom("ordini")
.where("userid", "=", ordine.userid) .where("userid", "=", ordine.userid)
.where("isActive", "=", false) .where("isActive", "=", false)
@ -121,6 +125,7 @@ export const whIntentSucceededHandler = async ({
} }
const servizio = await trx const servizio = await trx
.$pickTables<"servizio">()
.updateTable("servizio") .updateTable("servizio")
.where("servizio_id", "=", ordine.servizio_id) .where("servizio_id", "=", ordine.servizio_id)
.set({ .set({
@ -204,6 +209,7 @@ export const whIntentSucceededHandler = async ({
); );
} }
const servizio = await trx const servizio = await trx
.$pickTables<"servizio">()
.selectFrom("servizio") .selectFrom("servizio")
.select(["servizio.tipologia"]) .select(["servizio.tipologia"])
.where("servizio_id", "=", ordine.servizio_id) .where("servizio_id", "=", ordine.servizio_id)
@ -214,6 +220,7 @@ export const whIntentSucceededHandler = async ({
}); });
await trx await trx
.$pickTables<"servizio">()
.updateTable("servizio") .updateTable("servizio")
.where("servizio_id", "=", ordine.servizio_id) .where("servizio_id", "=", ordine.servizio_id)
.set({ .set({
@ -294,6 +301,7 @@ export const whIntentSucceededHandler = async ({
} }
await trx await trx
.$pickTables<"servizio">()
.updateTable("servizio") .updateTable("servizio")
.where("servizio_id", "=", ordine.servizio_id) .where("servizio_id", "=", ordine.servizio_id)
.set({ .set({
@ -399,6 +407,7 @@ export const whIntentFailedHandler = async ({
pIntentId: Ordini["intent_id"]; pIntentId: Ordini["intent_id"];
}) => { }) => {
const userId = await db const userId = await db
.$pickTables<"ordini">()
.updateTable("ordini") .updateTable("ordini")
.set({ .set({
paymentstatus: paymentStatusEnum.enum.failed, paymentstatus: paymentStatusEnum.enum.failed,

View file

@ -22,6 +22,7 @@ export const editAccountHandler = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"users">()
.updateTable("users") .updateTable("users")
.set({ .set({
...data, ...data,
@ -55,6 +56,7 @@ export const editAnagraficaHandler = async ({
} }
return await db return await db
.$pickTables<"users_anagrafica">()
.insertInto("users_anagrafica") .insertInto("users_anagrafica")
.values({ .values({
...rest, ...rest,
@ -77,7 +79,11 @@ export const editAnagraficaHandler = async ({
export const deleteUserHandler = async (id: UsersId) => { export const deleteUserHandler = async (id: UsersId) => {
try { try {
await db.deleteFrom("users").where("id", "=", id).execute(); await db
.$pickTables<"users">()
.deleteFrom("users")
.where("id", "=", id)
.execute();
return { success: true }; return { success: true };
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -90,6 +96,7 @@ export const deleteUserHandler = async (id: UsersId) => {
export const blockUserHandler = async (id: UsersId) => { export const blockUserHandler = async (id: UsersId) => {
return await db.transaction().execute(async (trx) => { return await db.transaction().execute(async (trx) => {
return await trx return await trx
.$pickTables<"users">()
.updateTable("users") .updateTable("users")
.set((eb) => ({ .set((eb) => ({
isBlocked: eb.not("isBlocked"), isBlocked: eb.not("isBlocked"),
@ -104,6 +111,7 @@ export const blockUserHandler = async (id: UsersId) => {
export const getUserHandler = async (id: UsersId) => { export const getUserHandler = async (id: UsersId) => {
try { try {
const user = await db const user = await db
.$pickTables<"users" | "users_anagrafica">()
.selectFrom("users") .selectFrom("users")
.leftJoin("users_anagrafica", "users_anagrafica.userid", "users.id") .leftJoin("users_anagrafica", "users_anagrafica.userid", "users.id")
.where("id", "=", id) .where("id", "=", id)
@ -131,6 +139,7 @@ export type NonNullableUser = {
export const getActiveUserWithoutChatHandler = async () => { export const getActiveUserWithoutChatHandler = async () => {
try { try {
const users = await db const users = await db
.$pickTables<"users" | "chats">()
.selectFrom("users") .selectFrom("users")
.fullJoin("chats", "chats.user_ref", "users.id") .fullJoin("chats", "chats.user_ref", "users.id")
.select(["users.id", "users.username", "users.nome"]) .select(["users.id", "users.username", "users.nome"])
@ -169,6 +178,9 @@ export const getUsersWithChatInfoHandler = async (): Promise<
> => { > => {
try { try {
return await db return await db
.$pickTables<
"users" | "chats" | "etichette" | "chats_etichette" | "user_etichette"
>()
.selectFrom("users") .selectFrom("users")
.select([ .select([
"users.id", "users.id",
@ -216,6 +228,7 @@ export const getUsersWithChatInfoHandler = async (): Promise<
export const genUserIntestazione = async (userId: UsersId) => { export const genUserIntestazione = async (userId: UsersId) => {
try { try {
const data = await db const data = await db
.$pickTables<"users" | "users_anagrafica">()
.selectFrom("users") .selectFrom("users")
.innerJoin("users_anagrafica", "users_anagrafica.userid", "users.id") .innerJoin("users_anagrafica", "users_anagrafica.userid", "users.id")
.where("id", "=", userId) .where("id", "=", userId)

View file

@ -21,6 +21,7 @@ export type AppuntiData = Omit<Appunti, "appunti_group_id"> & {
export const getAppunti = async (): Promise<AppuntiData[]> => { export const getAppunti = async (): Promise<AppuntiData[]> => {
try { try {
const appunti = await db const appunti = await db
.$pickTables<"appunti" | "appunti_groups" | "users">()
.selectFrom("appunti") .selectFrom("appunti")
.select([ .select([
"appunti.id", "appunti.id",
@ -57,7 +58,11 @@ export const getAppunti = async (): Promise<AppuntiData[]> => {
export const getAppuntiGroups = async (): Promise<AppuntiGroups[]> => { export const getAppuntiGroups = async (): Promise<AppuntiGroups[]> => {
try { try {
return await db.selectFrom("appunti_groups").selectAll().execute(); return await db
.$pickTables<"appunti_groups">()
.selectFrom("appunti_groups")
.selectAll()
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -68,7 +73,11 @@ export const getAppuntiGroups = async (): Promise<AppuntiGroups[]> => {
export const addAppuntiGroup = async (data: NewAppuntiGroups) => { export const addAppuntiGroup = async (data: NewAppuntiGroups) => {
try { try {
await db.insertInto("appunti_groups").values(data).execute(); await db
.$pickTables<"appunti_groups">()
.insertInto("appunti_groups")
.values(data)
.execute();
return { status: "success" }; return { status: "success" };
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -80,7 +89,11 @@ export const addAppuntiGroup = async (data: NewAppuntiGroups) => {
export const deleteAppuntiGroup = async (id: AppuntiGroupsId) => { export const deleteAppuntiGroup = async (id: AppuntiGroupsId) => {
try { try {
await db.deleteFrom("appunti_groups").where("id", "=", id).execute(); await db
.$pickTables<"appunti_groups">()
.deleteFrom("appunti_groups")
.where("id", "=", id)
.execute();
return { status: "success" }; return { status: "success" };
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -92,7 +105,11 @@ export const deleteAppuntiGroup = async (id: AppuntiGroupsId) => {
export const addAppunti = async (data: NewAppunti) => { export const addAppunti = async (data: NewAppunti) => {
try { try {
await db.insertInto("appunti").values(data).execute(); await db
.$pickTables<"appunti">()
.insertInto("appunti")
.values(data)
.execute();
return { status: "success" }; return { status: "success" };
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -104,7 +121,11 @@ export const addAppunti = async (data: NewAppunti) => {
export const deleteAppunti = async (id: AppuntiId) => { export const deleteAppunti = async (id: AppuntiId) => {
try { try {
await db.deleteFrom("appunti").where("id", "=", id).execute(); await db
.$pickTables<"appunti">()
.deleteFrom("appunti")
.where("id", "=", id)
.execute();
return { status: "success" }; return { status: "success" };
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -116,7 +137,12 @@ export const deleteAppunti = async (id: AppuntiId) => {
export const updateAppunto = async (id: AppuntiId, data: AppuntiUpdate) => { export const updateAppunto = async (id: AppuntiId, data: AppuntiUpdate) => {
try { try {
await db.updateTable("appunti").set(data).where("id", "=", id).execute(); await db
.$pickTables<"appunti">()
.updateTable("appunti")
.set(data)
.where("id", "=", id)
.execute();
return { status: "success" }; return { status: "success" };
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -136,6 +162,7 @@ export const updateAppunti = async (data: AppuntiData[]) => {
for (const item of dataWithPositions) { for (const item of dataWithPositions) {
const { column, username: _username, ...rest } = item; const { column, username: _username, ...rest } = item;
await db await db
.$pickTables<"appunti">()
.updateTable("appunti") .updateTable("appunti")
.set({ .set({
...rest, ...rest,

View file

@ -17,6 +17,7 @@ export const genPasswordResetToken = async ({ id }: { id: UsersId }) => {
const newToken = randomBytes(20).toString("hex"); const newToken = randomBytes(20).toString("hex");
try { try {
await db await db
.$pickTables<"users">()
.updateTable("users") .updateTable("users")
.set({ .set({
reset_password_expires: reset_expiry, reset_password_expires: reset_expiry,
@ -36,6 +37,7 @@ export const genPasswordResetToken = async ({ id }: { id: UsersId }) => {
export const isTokenValid = async ({ token }: { token: string }) => { export const isTokenValid = async ({ token }: { token: string }) => {
try { try {
const user = await db const user = await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.select("id") .select("id")
.where("reset_password_token", "=", token) .where("reset_password_token", "=", token)
@ -63,6 +65,7 @@ export const resetPasswordFromTokenHandler = async ({
}) => { }) => {
try { try {
const user = await db const user = await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.select("id") .select("id")
.where("reset_password_token", "=", token) .where("reset_password_token", "=", token)
@ -78,6 +81,7 @@ export const resetPasswordFromTokenHandler = async ({
const salt = generateSalt(); const salt = generateSalt();
const hashedPassword = await hashPassword(newpassword, salt); const hashedPassword = await hashPassword(newpassword, salt);
await db await db
.$pickTables<"users">()
.updateTable("users") .updateTable("users")
.set({ .set({
password: hashedPassword, password: hashedPassword,
@ -133,6 +137,7 @@ export const passwordOverrideHandler = async ({
const hashedPassword = await hashPassword(password, salt); const hashedPassword = await hashPassword(password, salt);
try { try {
await db await db
.$pickTables<"users">()
.updateTable("users") .updateTable("users")
.set({ .set({
password: hashedPassword, password: hashedPassword,

View file

@ -5,7 +5,11 @@ import type { Querier } from "~/server/db";
export const getBanList = async ({ db }: { db: Querier }) => { export const getBanList = async ({ db }: { db: Querier }) => {
try { try {
return await db.selectFrom("banlist").selectAll().execute(); return await db
.$pickTables<"banlist">()
.selectFrom("banlist")
.selectAll()
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -25,6 +29,7 @@ export const addBan = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"banlist">()
.insertInto("banlist") .insertInto("banlist")
.values({ .values({
type, type,
@ -54,6 +59,7 @@ export const updateBan = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"banlist">()
.updateTable("banlist") .updateTable("banlist")
.set({ .set({
type, type,
@ -72,7 +78,11 @@ export const updateBan = async ({
export const removeBan = async ({ db, id }: { db: Querier; id: BanlistId }) => { export const removeBan = async ({ db, id }: { db: Querier; id: BanlistId }) => {
try { try {
await db.deleteFrom("banlist").where("id", "=", id).executeTakeFirst(); await db
.$pickTables<"banlist">()
.deleteFrom("banlist")
.where("id", "=", id)
.executeTakeFirst();
return true; return true;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({

View file

@ -11,6 +11,7 @@ export const getBanner = async ({
}) => { }) => {
try { try {
let query = db let query = db
.$pickTables<"banners">()
.selectFrom("banners") .selectFrom("banners")
.selectAll() .selectAll()
.where("banners.is_active", "=", true); .where("banners.is_active", "=", true);
@ -30,7 +31,11 @@ export const getBanner = async ({
export const getAllBanners = async ({ db }: { db: Querier }) => { export const getAllBanners = async ({ db }: { db: Querier }) => {
try { try {
return await db.selectFrom("banners").selectAll().execute(); return await db
.$pickTables<"banners">()
.selectFrom("banners")
.selectAll()
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -48,6 +53,7 @@ export const addBanner = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"banners">()
.insertInto("banners") .insertInto("banners")
.values({ .values({
...input, ...input,
@ -72,6 +78,7 @@ export const updateBanner = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"banners">()
.updateTable("banners") .updateTable("banners")
.set({ .set({
...input, ...input,
@ -97,6 +104,7 @@ export const deleteBanner = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"banners">()
.deleteFrom("banners") .deleteFrom("banners")
.where("banners.idbanner", "=", input.idbanner) .where("banners.idbanner", "=", input.idbanner)
.execute(); .execute();

View file

@ -18,6 +18,7 @@ export const getRaw = async ({
}) => { }) => {
try { try {
return db return db
.$pickTables<"chats">()
.selectFrom("chats") .selectFrom("chats")
.selectAll() .selectAll()
.where("chats.chatid", "=", chatId) .where("chats.chatid", "=", chatId)
@ -35,6 +36,7 @@ export const getRaw = async ({
export const add = async ({ db, userId }: { db: Querier; userId: UsersId }) => { export const add = async ({ db, userId }: { db: Querier; userId: UsersId }) => {
try { try {
const newChat = await db const newChat = await db
.$pickTables<"chats">()
.insertInto("chats") .insertInto("chats")
.values({ .values({
created_at: new Date(), created_at: new Date(),

View file

@ -14,6 +14,7 @@ export const storeEmail = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"emails">()
.insertInto("emails") .insertInto("emails")
.values({ .values({
data, data,
@ -32,6 +33,7 @@ export const storeEmail = async ({
export const getEmails = async () => { export const getEmails = async () => {
try { try {
const mails = await db const mails = await db
.$pickTables<"emails" | "users">()
.selectFrom("emails") .selectFrom("emails")
.innerJoin("users", "users.id", "emails.user_id") .innerJoin("users", "users.id", "emails.user_id")
.selectAll("emails") .selectAll("emails")
@ -55,6 +57,7 @@ export const getEmails = async () => {
if (obsoleteMails.length > 0) { if (obsoleteMails.length > 0) {
await db await db
.$pickTables<"emails">()
.deleteFrom("emails") .deleteFrom("emails")
.where( .where(
"id_email", "id_email",
@ -76,6 +79,7 @@ export const getEmails = async () => {
export const getUserEmails = async ({ userId }: { userId: UsersId }) => { export const getUserEmails = async ({ userId }: { userId: UsersId }) => {
try { try {
const mails = await db const mails = await db
.$pickTables<"emails">()
.selectFrom("emails") .selectFrom("emails")
.selectAll() .selectAll()
.where("user_id", "=", userId) .where("user_id", "=", userId)
@ -96,6 +100,7 @@ export const getUserEmails = async ({ userId }: { userId: UsersId }) => {
if (obsoleteMails.length > 0) { if (obsoleteMails.length > 0) {
await db await db
.$pickTables<"emails">()
.deleteFrom("emails") .deleteFrom("emails")
.where( .where(
"id_email", "id_email",
@ -120,7 +125,11 @@ export const deleteEmail = async ({
id_email: EmailsIdEmail; id_email: EmailsIdEmail;
}) => { }) => {
try { try {
await db.deleteFrom("emails").where("id_email", "=", id_email).execute(); await db
.$pickTables<"emails">()
.deleteFrom("emails")
.where("id_email", "=", id_email)
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -131,7 +140,11 @@ export const deleteEmail = async ({
export const deleteAllUserEmails = async ({ userId }: { userId: UsersId }) => { export const deleteAllUserEmails = async ({ userId }: { userId: UsersId }) => {
try { try {
await db.deleteFrom("emails").where("user_id", "=", userId).execute(); await db
.$pickTables<"emails">()
.deleteFrom("emails")
.where("user_id", "=", userId)
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",

View file

@ -13,6 +13,7 @@ export const AddIntrest = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"servizio_interessi">()
.insertInto("servizio_interessi") .insertInto("servizio_interessi")
.values({ .values({
annuncioId, annuncioId,
@ -38,6 +39,7 @@ export const RemoveInterest = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"servizio_interessi">()
.deleteFrom("servizio_interessi") .deleteFrom("servizio_interessi")
.where("annuncioId", "=", annuncioId) .where("annuncioId", "=", annuncioId)
.where("userId", "=", userId) .where("userId", "=", userId)
@ -61,6 +63,7 @@ export const HasUserInterest = async ({
}) => { }) => {
try { try {
const interest = await db const interest = await db
.$pickTables<"servizio_interessi">()
.selectFrom("servizio_interessi") .selectFrom("servizio_interessi")
.where("annuncioId", "=", annuncioId) .where("annuncioId", "=", annuncioId)
.where("userId", "=", userId) .where("userId", "=", userId)
@ -80,6 +83,7 @@ export const GetUserInterests = async (userId: UsersId) => {
try { try {
return ( return (
await db await db
.$pickTables<"servizio_interessi">()
.selectFrom("servizio_interessi") .selectFrom("servizio_interessi")
.where("userId", "=", userId) .where("userId", "=", userId)
.select("annuncioId") .select("annuncioId")
@ -99,6 +103,7 @@ export const GetUserInterestsAnnunci = async (userId: UsersId) => {
return []; return [];
} }
const annunci = await db const annunci = await db
.$pickTables<"annunci" | "images_refs" | "videos_refs">()
.selectFrom("annunci") .selectFrom("annunci")
.select((_eb) => [ .select((_eb) => [
"annunci.id", "annunci.id",

View file

@ -27,6 +27,7 @@ export const getOrdini = async (
): Promise<Ordini_w_Utente[]> => { ): Promise<Ordini_w_Utente[]> => {
try { try {
let qry = db let qry = db
.$pickTables<"ordini" | "users">()
.selectFrom("ordini") .selectFrom("ordini")
.selectAll("ordini") .selectAll("ordini")
.innerJoin("users", "users.id", "ordini.userid") .innerJoin("users", "users.id", "ordini.userid")
@ -47,6 +48,7 @@ export const getOrdini = async (
export const getOrdineById = async (ordineId: OrdiniOrdineId) => { export const getOrdineById = async (ordineId: OrdiniOrdineId) => {
try { try {
const data = await db const data = await db
.$pickTables<"ordini" | "users" | "users_anagrafica">()
.selectFrom("ordini") .selectFrom("ordini")
.selectAll("ordini") .selectAll("ordini")
.innerJoin("users", "users.id", "ordini.userid") .innerJoin("users", "users.id", "ordini.userid")
@ -89,6 +91,7 @@ export const createOrdine = async (
) => { ) => {
try { try {
return await db return await db
.$pickTables<"ordini">()
.insertInto("ordini") .insertInto("ordini")
.values(data) .values(data)
.returningAll() .returningAll()
@ -103,7 +106,11 @@ export const createOrdine = async (
export const removeOrder = async (ordineId: OrdiniOrdineId) => { export const removeOrder = async (ordineId: OrdiniOrdineId) => {
try { try {
await db.deleteFrom("ordini").where("ordine_id", "=", ordineId).execute(); await db
.$pickTables<"ordini">()
.deleteFrom("ordini")
.where("ordine_id", "=", ordineId)
.execute();
return true; return true;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -122,6 +129,7 @@ export const updateOrder = async ({
}) => { }) => {
try { try {
const updated = await db const updated = await db
.$pickTables<"ordini">()
.updateTable("ordini") .updateTable("ordini")
.set(data) .set(data)
.where("ordine_id", "=", ordineId) .where("ordine_id", "=", ordineId)

View file

@ -15,6 +15,7 @@ export const CONDIZIONI_DEFAULT = "CONDIZIONI_CERCHI" as TestiEStringheStingaId;
export const getPrezziario = async () => { export const getPrezziario = async () => {
try { try {
return await db return await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("isActive", "=", true) .where("isActive", "=", true)
@ -31,6 +32,7 @@ export const getPrezziario = async () => {
export const getPrezziarioByIdHandler = async (id: PrezziarioIdprezziario) => { export const getPrezziarioByIdHandler = async (id: PrezziarioIdprezziario) => {
try { try {
return await db return await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("prezziario.idprezziario", "=", id) .where("prezziario.idprezziario", "=", id)
@ -52,6 +54,7 @@ export const addPrezziario = async ({
}): Promise<Prezziario> => { }): Promise<Prezziario> => {
try { try {
return await db return await db
.$pickTables<"prezziario">()
.insertInto("prezziario") .insertInto("prezziario")
.values(input) .values(input)
.returningAll() .returningAll()
@ -76,6 +79,7 @@ export const updatePrezziario = async ({
}): Promise<Prezziario> => { }): Promise<Prezziario> => {
try { try {
return await db return await db
.$pickTables<"prezziario">()
.updateTable("prezziario") .updateTable("prezziario")
.set(input) .set(input)
.where("prezziario.idprezziario", "=", prezziarioId) .where("prezziario.idprezziario", "=", prezziarioId)
@ -131,6 +135,7 @@ export const getPrezziarioByTipologia = async ({
try { try {
const { acconto, saldi } = await db.transaction().execute(async (trx) => { const { acconto, saldi } = await db.transaction().execute(async (trx) => {
const acconto = await trx const acconto = await trx
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.select(["idprezziario", "prezzo_cent", "nome_it", "nome_en", "sconto"]) .select(["idprezziario", "prezzo_cent", "nome_it", "nome_en", "sconto"])
.where("order_type", "=", orderTypeEnum.enum.Acconto) .where("order_type", "=", orderTypeEnum.enum.Acconto)
@ -145,6 +150,7 @@ export const getPrezziarioByTipologia = async ({
}); });
} }
const saldi = await trx const saldi = await trx
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.select(["idprezziario", "prezzo_cent", "nome_it", "nome_en", "sconto"]) .select(["idprezziario", "prezzo_cent", "nome_it", "nome_en", "sconto"])
.where("order_type", "=", orderTypeEnum.enum.Saldo) .where("order_type", "=", orderTypeEnum.enum.Saldo)
@ -177,6 +183,7 @@ export const getPrezziarioConsulenza = async ({ db }: { db: Querier }) => {
try { try {
return await db.transaction().execute(async (trx) => { return await db.transaction().execute(async (trx) => {
const prezzi = await trx const prezzi = await trx
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.select(["idprezziario", "prezzo_cent", "nome_it", "nome_en", "sconto"]) .select(["idprezziario", "prezzo_cent", "nome_it", "nome_en", "sconto"])
.where("order_type", "=", orderTypeEnum.enum.Consulenza) .where("order_type", "=", orderTypeEnum.enum.Consulenza)

View file

@ -50,6 +50,7 @@ export async function setTyping(
username: string, username: string,
): Promise<void> { ): Promise<void> {
await db await db
.$pickTables<"typing_status">()
.insertInto("typing_status") .insertInto("typing_status")
.values({ .values({
chatid: chatId, chatid: chatId,
@ -70,6 +71,7 @@ export async function clearTyping(
userId: UsersId, userId: UsersId,
): Promise<void> { ): Promise<void> {
await db await db
.$pickTables<"typing_status">()
.deleteFrom("typing_status") .deleteFrom("typing_status")
.where("chatid", "=", chatId) .where("chatid", "=", chatId)
.where("userid", "=", userId) .where("userid", "=", userId)
@ -85,6 +87,7 @@ export async function pruneStaleTyping(
): Promise<void> { ): Promise<void> {
const cutoff = new Date(Date.now() - maxAgeMs); const cutoff = new Date(Date.now() - maxAgeMs);
await db await db
.$pickTables<"typing_status">()
.deleteFrom("typing_status") .deleteFrom("typing_status")
.where("chatid", "=", chatId) .where("chatid", "=", chatId)
.where("last_ping", "<", cutoff) .where("last_ping", "<", cutoff)
@ -99,6 +102,7 @@ export async function getTypingList(
chatId: ChatsChatid, chatId: ChatsChatid,
): Promise<Array<{ userId: UsersId; username: string }>> { ): Promise<Array<{ userId: UsersId; username: string }>> {
const rows = await db const rows = await db
.$pickTables<"typing_status">()
.selectFrom("typing_status") .selectFrom("typing_status")
.select(["userid as userId", "username"]) .select(["userid as userId", "username"])
.where("chatid", "=", chatId) .where("chatid", "=", chatId)

View file

@ -17,6 +17,7 @@ import {
export const getRinnoviUser = async (userId: UsersId) => { export const getRinnoviUser = async (userId: UsersId) => {
try { try {
return await db return await db
.$pickTables<"rinnovi">()
.selectFrom("rinnovi") .selectFrom("rinnovi")
.where("userId", "=", userId) .where("userId", "=", userId)
.selectAll() .selectAll()
@ -34,6 +35,7 @@ export type RinnovoData = Awaited<ReturnType<typeof getRinnovoData>>;
export const getRinnovoData = async (rinnovoId: RinnoviId) => { export const getRinnovoData = async (rinnovoId: RinnoviId) => {
try { try {
return await db return await db
.$pickTables<"rinnovi" | "ordini">()
.selectFrom("rinnovi") .selectFrom("rinnovi")
.where("id", "=", rinnovoId) .where("id", "=", rinnovoId)
.selectAll("rinnovi") .selectAll("rinnovi")
@ -59,6 +61,7 @@ export const getRinnovoData = async (rinnovoId: RinnoviId) => {
export const createRinnovo = async (data: NewRinnovi) => { export const createRinnovo = async (data: NewRinnovi) => {
try { try {
const rinnovo = await db const rinnovo = await db
.$pickTables<"rinnovi">()
.insertInto("rinnovi") .insertInto("rinnovi")
.values(data) .values(data)
.returningAll() .returningAll()
@ -98,6 +101,7 @@ export const updateRinnovo = async ({
try { try {
if (data.decorrenza && data.scadenza) { if (data.decorrenza && data.scadenza) {
const rinnovo = await db const rinnovo = await db
.$pickTables<"rinnovi">()
.selectFrom("rinnovi") .selectFrom("rinnovi")
.where("id", "=", rinnovoId) .where("id", "=", rinnovoId)
.selectAll() .selectAll()
@ -130,6 +134,7 @@ export const updateRinnovo = async ({
if (existingPack.pack.idprezziario !== newPack.pack.idprezziario) { if (existingPack.pack.idprezziario !== newPack.pack.idprezziario) {
//update ordine with new pack and price if permanenza is being updated and ordine is not active yet //update ordine with new pack and price if permanenza is being updated and ordine is not active yet
await db await db
.$pickTables<"ordini">()
.updateTable("ordini") .updateTable("ordini")
.set({ .set({
amount_cent: newPack.pack.prezzo_cent, amount_cent: newPack.pack.prezzo_cent,
@ -143,6 +148,7 @@ export const updateRinnovo = async ({
} }
return await db return await db
.$pickTables<"rinnovi">()
.updateTable("rinnovi") .updateTable("rinnovi")
.set(data) .set(data)
.where("id", "=", rinnovoId) .where("id", "=", rinnovoId)
@ -160,7 +166,11 @@ export const updateRinnovo = async ({
export const deleteRinnovo = async (rinnovoId: RinnoviId) => { export const deleteRinnovo = async (rinnovoId: RinnoviId) => {
try { try {
await db.deleteFrom("rinnovi").where("id", "=", rinnovoId).execute(); await db
.$pickTables<"rinnovi">()
.deleteFrom("rinnovi")
.where("id", "=", rinnovoId)
.execute();
return true; return true;
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({

View file

@ -14,6 +14,7 @@ export const addServizio = async (data: NewServizio) => {
try { try {
return await db.transaction().execute(async (tx) => { return await db.transaction().execute(async (tx) => {
const servizio = await tx const servizio = await tx
.$pickTables<"servizio">()
.insertInto("servizio") .insertInto("servizio")
.values(data) .values(data)
.returningAll() .returningAll()
@ -22,6 +23,7 @@ export const addServizio = async (data: NewServizio) => {
); );
const acconto = await db const acconto = await db
.$pickTables<"prezziario">()
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("isActive", "=", true) .where("isActive", "=", true)
@ -57,6 +59,7 @@ export const addServizio = async (data: NewServizio) => {
export const getServizioById = async (servizioId: ServizioServizioId) => { export const getServizioById = async (servizioId: ServizioServizioId) => {
try { try {
return await db return await db
.$pickTables<"servizio">()
.selectFrom("servizio") .selectFrom("servizio")
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -80,6 +83,7 @@ export const updateServizio = async ({
}) => { }) => {
try { try {
const isDisabledPrev = await db const isDisabledPrev = await db
.$pickTables<"servizio">()
.selectFrom("servizio") .selectFrom("servizio")
.select("isDisabled") .select("isDisabled")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -87,6 +91,7 @@ export const updateServizio = async ({
() => new Error(`Servizio not found - servizioId: ${servizioId}`), () => new Error(`Servizio not found - servizioId: ${servizioId}`),
); );
const updated = await db const updated = await db
.$pickTables<"servizio">()
.updateTable("servizio") .updateTable("servizio")
.set(data) .set(data)
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -111,6 +116,7 @@ export const updateServizio = async ({
export const deleteServizio = async (servizioId: ServizioServizioId) => { export const deleteServizio = async (servizioId: ServizioServizioId) => {
try { try {
await db await db
.$pickTables<"servizio">()
.deleteFrom("servizio") .deleteFrom("servizio")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.execute(); .execute();
@ -134,6 +140,7 @@ export const getServizioAnnuncio = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"servizio_annunci">()
.selectFrom("servizio_annunci") .selectFrom("servizio_annunci")
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -163,6 +170,7 @@ export const updateServizioAnnuncio = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"servizio_annunci">()
.updateTable("servizio_annunci") .updateTable("servizio_annunci")
.set(data) .set(data)
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
@ -184,6 +192,7 @@ export const deleteServizioAnnuncio = async (
) => { ) => {
try { try {
await db await db
.$pickTables<"servizio_annunci">()
.deleteFrom("servizio_annunci") .deleteFrom("servizio_annunci")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.where("annunci_id", "=", annuncioId) .where("annunci_id", "=", annuncioId)

View file

@ -4,7 +4,11 @@ import type { Querier } from "~/server/db";
export const getStringhe = async ({ db }: { db: Querier }) => { export const getStringhe = async ({ db }: { db: Querier }) => {
try { try {
return await db.selectFrom("testi_e_stringhe").selectAll().execute(); return await db
.$pickTables<"testi_e_stringhe">()
.selectFrom("testi_e_stringhe")
.selectAll()
.execute();
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -21,6 +25,7 @@ export const getStringheFiltered = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"testi_e_stringhe">()
.selectFrom("testi_e_stringhe") .selectFrom("testi_e_stringhe")
.where("stinga_id", "like", `%${contains}%` as TestiEStringheStingaId) .where("stinga_id", "like", `%${contains}%` as TestiEStringheStingaId)
.selectAll() .selectAll()
@ -42,6 +47,7 @@ export const getStringa = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"testi_e_stringhe">()
.selectFrom("testi_e_stringhe") .selectFrom("testi_e_stringhe")
.selectAll() .selectAll()
.where("stinga_id", "=", stringaId) .where("stinga_id", "=", stringaId)
@ -65,6 +71,7 @@ export const newStringa = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"testi_e_stringhe">()
.insertInto("testi_e_stringhe") .insertInto("testi_e_stringhe")
.values({ stinga_id: stringaId, stringa_value: stringaValue }) .values({ stinga_id: stringaId, stringa_value: stringaValue })
.returning("stinga_id") .returning("stinga_id")
@ -88,6 +95,7 @@ export const updateStringhe = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"testi_e_stringhe">()
.updateTable("testi_e_stringhe") .updateTable("testi_e_stringhe")
.set({ stringa_value: stringaValue }) .set({ stringa_value: stringaValue })
.where("stinga_id", "=", stringaId) .where("stinga_id", "=", stringaId)
@ -110,6 +118,7 @@ export const deleteStringa = async ({
}) => { }) => {
try { try {
await db await db
.$pickTables<"testi_e_stringhe">()
.deleteFrom("testi_e_stringhe") .deleteFrom("testi_e_stringhe")
.where("stinga_id", "=", stringaId) .where("stinga_id", "=", stringaId)
.execute(); .execute();

View file

@ -6,6 +6,7 @@ import { db } from "~/server/db";
export async function get(key: string) { export async function get(key: string) {
try { try {
const row = await db const row = await db
.$pickTables<"tiles_cache">()
.selectFrom("tiles_cache") .selectFrom("tiles_cache")
.select(["value", "expires_at"]) .select(["value", "expires_at"])
.where("key", "=", key as TilesCacheKey) .where("key", "=", key as TilesCacheKey)
@ -36,6 +37,7 @@ export async function set(
try { try {
await db await db
.$pickTables<"tiles_cache">()
.insertInto("tiles_cache") .insertInto("tiles_cache")
.values({ .values({
key: key as TilesCacheKey, key: key as TilesCacheKey,

View file

@ -7,6 +7,7 @@ import { db, type Querier } from "~/server/db";
export const findUser_byId = async ({ id }: { id: string }) => { export const findUser_byId = async ({ id }: { id: string }) => {
try { try {
return await db return await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.where("id", "=", id as UsersId) .where("id", "=", id as UsersId)
@ -21,6 +22,7 @@ export const findUser_byId = async ({ id }: { id: string }) => {
export const findUser_byEmail = async ({ email }: { email: string }) => { export const findUser_byEmail = async ({ email }: { email: string }) => {
try { try {
return await db return await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.where("email", "=", email) .where("email", "=", email)
@ -39,6 +41,7 @@ export const findUser_byId_MINI = async ({
}): Promise<Session | undefined> => { }): Promise<Session | undefined> => {
try { try {
return await db return await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.select([ .select([
"id", "id",
@ -68,6 +71,7 @@ export const getUser = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"users">()
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.where("id", "=", userId) .where("id", "=", userId)
@ -91,6 +95,7 @@ export const addUser = async ({
}) => { }) => {
try { try {
const user = await db const user = await db
.$pickTables<"users">()
.insertInto("users") .insertInto("users")
.values({ .values({
...data, ...data,
@ -118,6 +123,7 @@ export const getClientProfilo = async ({
}) => { }) => {
try { try {
return await db return await db
.$pickTables<"users" | "users_anagrafica" | "users_storage">()
.selectFrom("users") .selectFrom("users")
.select([ .select([
"id", "id",