This commit is contained in:
 Илья Шалонин 2025-10-29 13:43:43 +05:00
commit 8538ab969a
434 changed files with 44051 additions and 0 deletions

View file

@ -0,0 +1,92 @@
import { db } from "../client";
import type { AiChat } from "../zod";
export async function getAiChatsByUserId({
limit,
offset,
userId,
}: {
limit: number;
offset: number;
userId: string;
}) {
return await db.aiChat.findMany({
where: {
userId,
},
take: limit,
skip: offset,
});
}
export async function getAiChatsByOrganizationId({
limit,
offset,
organizationId,
}: {
limit: number;
offset: number;
organizationId: string;
}) {
return await db.aiChat.findMany({
where: {
organizationId,
},
take: limit,
skip: offset,
});
}
export async function getAiChatById(id: string) {
return await db.aiChat.findUnique({
where: {
id,
},
});
}
export async function createAiChat({
organizationId,
userId,
title,
}: {
organizationId?: string;
userId: string;
title?: string;
}) {
return await db.aiChat.create({
data: {
organizationId,
userId,
title,
},
});
}
export async function updateAiChat({
id,
title,
messages,
}: {
id: string;
title?: string;
messages?: AiChat["messages"];
}) {
return await db.aiChat.update({
where: {
id,
},
data: {
title,
messages,
},
});
}
export async function deleteAiChat(id: string) {
return await db.aiChat.delete({
where: {
id,
},
});
}

View file

@ -0,0 +1,4 @@
export * from "./ai-chats";
export * from "./organizations";
export * from "./purchases";
export * from "./users";

View file

@ -0,0 +1,128 @@
import type { z } from "zod";
import { db } from "../client";
import type { OrganizationUpdateInputSchema } from "../zod";
export async function getOrganizations({
limit,
offset,
query,
}: {
limit: number;
offset: number;
query?: string;
}) {
return db.organization
.findMany({
where: {
name: { contains: query, mode: "insensitive" },
},
include: {
_count: {
select: {
members: true,
},
},
},
take: limit,
skip: offset,
})
.then((res) =>
res.map((org) => ({
...org,
membersCount: org._count.members,
})),
);
}
export async function countAllOrganizations() {
return db.organization.count();
}
export async function getOrganizationById(id: string) {
return db.organization.findUnique({
where: { id },
include: {
members: true,
invitations: true,
},
});
}
export async function getInvitationById(id: string) {
return db.invitation.findUnique({
where: { id },
include: {
organization: true,
},
});
}
export async function getOrganizationBySlug(slug: string) {
return db.organization.findUnique({
where: { slug },
});
}
export async function getOrganizationMembership(
organizationId: string,
userId: string,
) {
return db.member.findUnique({
where: {
organizationId_userId: {
organizationId,
userId,
},
},
include: {
organization: true,
},
});
}
export async function getOrganizationWithPurchasesAndMembersCount(
organizationId: string,
) {
const organization = await db.organization.findUnique({
where: {
id: organizationId,
},
include: {
purchases: true,
_count: {
select: {
members: true,
},
},
},
});
return organization
? {
...organization,
membersCount: organization._count.members,
}
: null;
}
export async function getPendingInvitationByEmail(email: string) {
return db.invitation.findFirst({
where: {
email,
status: "pending",
},
});
}
export async function updateOrganization(
organization: z.infer<typeof OrganizationUpdateInputSchema> & {
id: string;
},
) {
return db.organization.update({
where: {
id: organization.id,
},
data: organization,
});
}

View file

@ -0,0 +1,69 @@
import type { z } from "zod";
import { db } from "../client";
import type {
PurchaseUncheckedCreateInputSchema,
PurchaseUncheckedUpdateInputSchema,
} from "../zod";
export async function getPurchaseById(id: string) {
return db.purchase.findUnique({
where: { id },
});
}
export async function getPurchasesByOrganizationId(organizationId: string) {
return db.purchase.findMany({
where: {
organizationId,
},
});
}
export async function getPurchasesByUserId(userId: string) {
return db.purchase.findMany({
where: {
userId,
},
});
}
export async function getPurchaseBySubscriptionId(subscriptionId: string) {
return db.purchase.findFirst({
where: {
subscriptionId,
},
});
}
export async function createPurchase(
purchase: z.infer<typeof PurchaseUncheckedCreateInputSchema>,
) {
const created = await db.purchase.create({
data: purchase,
});
return getPurchaseById(created.id);
}
export async function updatePurchase(
purchase: z.infer<typeof PurchaseUncheckedUpdateInputSchema> & {
id: string;
},
) {
const updated = await db.purchase.update({
where: {
id: purchase.id,
},
data: purchase,
});
return getPurchaseById(updated.id);
}
export async function deletePurchaseBySubscriptionId(subscriptionId: string) {
await db.purchase.delete({
where: {
subscriptionId,
},
});
}

View file

@ -0,0 +1,113 @@
import type { z } from "zod";
import { db } from "../client";
import type { UserUncheckedUpdateInputSchema } from "../zod";
export async function getUsers({
limit,
offset,
query,
}: {
limit: number;
offset: number;
query?: string;
}) {
return await db.user.findMany({
where: query
? {
name: {
contains: query,
},
}
: undefined,
take: limit,
skip: offset,
});
}
export async function countAllUsers() {
return await db.user.count();
}
export async function getUserById(id: string) {
return await db.user.findUnique({
where: {
id,
},
});
}
export async function getUserByEmail(email: string) {
return await db.user.findUnique({
where: {
email,
},
});
}
export async function createUser({
email,
name,
role,
emailVerified,
onboardingComplete,
}: {
email: string;
name: string;
role: "admin" | "user";
emailVerified: boolean;
onboardingComplete: boolean;
}) {
return await db.user.create({
data: {
email,
name,
role,
emailVerified,
onboardingComplete,
createdAt: new Date(),
updatedAt: new Date(),
},
});
}
export async function getAccountById(id: string) {
return await db.account.findUnique({
where: {
id,
},
});
}
export async function createUserAccount({
userId,
providerId,
accountId,
hashedPassword,
}: {
userId: string;
providerId: string;
accountId: string;
hashedPassword?: string;
}) {
return await db.account.create({
data: {
userId,
accountId,
providerId,
password: hashedPassword,
createdAt: new Date(),
updatedAt: new Date(),
},
});
}
export async function updateUser(
user: z.infer<typeof UserUncheckedUpdateInputSchema> & { id: string },
) {
return await db.user.update({
where: {
id: user.id,
},
data: user,
});
}