rename all db columns
This commit is contained in:
@@ -1,38 +1,20 @@
|
||||
import type { Client } from '../prisma-client';
|
||||
import type { Client, Prisma } from '../prisma-client';
|
||||
import { db } from '../prisma-client';
|
||||
import { transformProject } from './project.service';
|
||||
import type { IServiceProject } from './project.service';
|
||||
|
||||
export type IServiceClient = ReturnType<typeof transformClient>;
|
||||
export type IServiceClientWithProject = IServiceClient & {
|
||||
project: Exclude<IServiceProject, null>;
|
||||
};
|
||||
|
||||
export function transformClient({ organization_slug, ...client }: Client) {
|
||||
return {
|
||||
...client,
|
||||
organizationSlug: organization_slug,
|
||||
export type IServiceClient = Client;
|
||||
export type IServiceClientWithProject = Prisma.ClientGetPayload<{
|
||||
include: {
|
||||
project: true;
|
||||
};
|
||||
}
|
||||
}>;
|
||||
|
||||
export async function getClientsByOrganizationId(organizationId: string) {
|
||||
const clients = await db.client.findMany({
|
||||
return db.client.findMany({
|
||||
where: {
|
||||
organization_slug: organizationId,
|
||||
organizationSlug: organizationId,
|
||||
},
|
||||
include: {
|
||||
project: true,
|
||||
},
|
||||
});
|
||||
|
||||
return clients
|
||||
.map((client) => {
|
||||
return {
|
||||
...transformClient(client),
|
||||
project: transformProject(client.project),
|
||||
};
|
||||
})
|
||||
.filter(
|
||||
(client): client is IServiceClientWithProject => client.project !== null
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import type { Dashboard, Prisma } from '../prisma-client';
|
||||
import { db } from '../prisma-client';
|
||||
|
||||
export type IServiceDashboard = Awaited<ReturnType<typeof getDashboardById>>;
|
||||
export type IServiceDashboards = Awaited<
|
||||
ReturnType<typeof getDashboardsByProjectId>
|
||||
>;
|
||||
export type IServiceDashboard = Dashboard;
|
||||
export type IServiceDashboards = Prisma.DashboardGetPayload<{
|
||||
include: {
|
||||
project: true;
|
||||
};
|
||||
}>[];
|
||||
|
||||
export async function getDashboardById(id: string, projectId: string) {
|
||||
const dashboard = await db.dashboard.findUnique({
|
||||
where: {
|
||||
id,
|
||||
project_id: projectId,
|
||||
projectId,
|
||||
},
|
||||
include: {
|
||||
project: true,
|
||||
@@ -26,7 +29,7 @@ export async function getDashboardById(id: string, projectId: string) {
|
||||
export function getDashboardsByProjectId(projectId: string) {
|
||||
return db.dashboard.findMany({
|
||||
where: {
|
||||
project_id: projectId,
|
||||
projectId,
|
||||
},
|
||||
include: {
|
||||
project: true,
|
||||
|
||||
@@ -144,7 +144,7 @@ export async function getEvents(
|
||||
name: {
|
||||
in: names,
|
||||
},
|
||||
project_id: events[0]?.project_id,
|
||||
projectId: events[0]?.project_id,
|
||||
},
|
||||
select: options.meta === true ? undefined : options.meta,
|
||||
});
|
||||
@@ -265,7 +265,7 @@ export async function getEventList({
|
||||
sb.where.projectId = `project_id = ${escape(projectId)}`;
|
||||
|
||||
if (profileId) {
|
||||
sb.where.deviceId = `device_id IN (SELECT device_id as did FROM openpanel.events WHERE profile_id = ${escape(profileId)} group by did)`;
|
||||
sb.where.deviceId = `device_id IN (SELECT device_id as did FROM events WHERE profile_id = ${escape(profileId)} group by did)`;
|
||||
}
|
||||
|
||||
if (events && events.length > 0) {
|
||||
@@ -357,7 +357,7 @@ export function createBotEvent({
|
||||
export function getConversionEventNames(projectId: string) {
|
||||
return db.eventMeta.findMany({
|
||||
where: {
|
||||
project_id: projectId,
|
||||
projectId,
|
||||
conversion: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ import { db } from '../prisma-client';
|
||||
export type IServiceOrganization = ReturnType<typeof transformOrganization>;
|
||||
export type IServiceInvite = ReturnType<typeof transformInvite>;
|
||||
export type IServiceMember = ReturnType<typeof transformMember>;
|
||||
export type IServiceProjectAccess = ReturnType<typeof transformAccess>;
|
||||
export type IServiceProjectAccess = ProjectAccess;
|
||||
|
||||
export function transformOrganization(org: Organization) {
|
||||
return {
|
||||
@@ -21,15 +21,6 @@ export function transformOrganization(org: Organization) {
|
||||
};
|
||||
}
|
||||
|
||||
export function transformAccess(access: ProjectAccess) {
|
||||
return {
|
||||
projectId: access.project_id,
|
||||
userId: access.user_id,
|
||||
level: access.level,
|
||||
organizationSlug: access.organization_slug,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getCurrentOrganizations() {
|
||||
const session = auth();
|
||||
const organizations = await clerkClient.users.getOrganizationMembershipList({
|
||||
@@ -53,7 +44,7 @@ export async function getOrganizationByProjectId(projectId: string) {
|
||||
});
|
||||
|
||||
return clerkClient.organizations.getOrganization({
|
||||
slug: project.organization_slug,
|
||||
slug: project.organizationSlug,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -110,7 +101,7 @@ export async function getMembers(organizationSlug: string) {
|
||||
}),
|
||||
db.projectAccess.findMany({
|
||||
where: {
|
||||
organization_slug: organizationSlug,
|
||||
organizationSlug,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
@@ -118,11 +109,11 @@ export async function getMembers(organizationSlug: string) {
|
||||
return members
|
||||
.map((member) => {
|
||||
const projectAccess = access.filter(
|
||||
(item) => item.user_id === member.publicUserData?.userId
|
||||
(item) => item.userId === member.publicUserData?.userId
|
||||
);
|
||||
return {
|
||||
...member,
|
||||
access: projectAccess.map(transformAccess),
|
||||
access: projectAccess,
|
||||
};
|
||||
})
|
||||
.map(transformMember);
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import { auth } from '@clerk/nextjs';
|
||||
import { project } from 'ramda';
|
||||
|
||||
import type { Project } from '../prisma-client';
|
||||
import { db } from '../prisma-client';
|
||||
|
||||
export type IServiceProject = ReturnType<typeof transformProject>;
|
||||
|
||||
export function transformProject({ organization_slug, ...project }: Project) {
|
||||
return {
|
||||
organizationSlug: organization_slug,
|
||||
...project,
|
||||
};
|
||||
}
|
||||
export type IServiceProject = Project;
|
||||
|
||||
export async function getProjectById(id: string) {
|
||||
const res = await db.project.findUnique({
|
||||
@@ -24,46 +16,32 @@ export async function getProjectById(id: string) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return transformProject(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getProjectsByOrganizationSlug(slug: string) {
|
||||
const res = await db.project.findMany({
|
||||
export async function getProjectsByOrganizationSlug(organizationSlug: string) {
|
||||
return db.project.findMany({
|
||||
where: {
|
||||
organization_slug: slug,
|
||||
organizationSlug,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
|
||||
return res.map(transformProject);
|
||||
}
|
||||
|
||||
export async function getCurrentProjects(slug: string) {
|
||||
export async function getCurrentProjects(organizationSlug: string) {
|
||||
const session = auth();
|
||||
if (!session.userId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const access = await db.projectAccess.findMany({
|
||||
return db.project.findMany({
|
||||
where: {
|
||||
organization_slug: slug,
|
||||
user_id: session.userId,
|
||||
organizationSlug,
|
||||
},
|
||||
include: {
|
||||
access: true,
|
||||
},
|
||||
});
|
||||
|
||||
const res = await db.project.findMany({
|
||||
where: {
|
||||
organization_slug: slug,
|
||||
},
|
||||
});
|
||||
|
||||
if (access.length === 0) {
|
||||
return res.map(transformProject);
|
||||
}
|
||||
|
||||
return res
|
||||
.filter((project) => access.some((a) => a.project_id === project.id))
|
||||
.map(transformProject);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,7 @@
|
||||
import type { Prisma, Reference } from '../prisma-client';
|
||||
import { db } from '../prisma-client';
|
||||
|
||||
export type IServiceReference = Omit<Reference, 'project_id'> & {
|
||||
projectId: string;
|
||||
};
|
||||
|
||||
export function transformReference({
|
||||
project_id,
|
||||
...item
|
||||
}: Reference): IServiceReference {
|
||||
return {
|
||||
...item,
|
||||
projectId: project_id,
|
||||
};
|
||||
}
|
||||
export type IServiceReference = Reference;
|
||||
|
||||
export async function getReferenceById(id: string) {
|
||||
const reference = await db.reference.findUnique({
|
||||
@@ -26,7 +14,7 @@ export async function getReferenceById(id: string) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return transformReference(reference);
|
||||
return reference;
|
||||
}
|
||||
|
||||
export async function getReferences({
|
||||
@@ -38,11 +26,9 @@ export async function getReferences({
|
||||
take?: number;
|
||||
skip?: number;
|
||||
}) {
|
||||
const references = await db.reference.findMany({
|
||||
return db.reference.findMany({
|
||||
where,
|
||||
take: take ?? 50,
|
||||
skip,
|
||||
});
|
||||
|
||||
return references.map(transformReference);
|
||||
}
|
||||
|
||||
@@ -45,11 +45,11 @@ export function transformReport(
|
||||
): IChartInput & { id: string } {
|
||||
return {
|
||||
id: report.id,
|
||||
projectId: report.project_id,
|
||||
projectId: report.projectId,
|
||||
events: (report.events as IChartEvent[]).map(transformReportEvent),
|
||||
breakdowns: report.breakdowns as IChartBreakdown[],
|
||||
chartType: report.chart_type,
|
||||
lineType: (report.line_type as IChartLineType) ?? lineTypes.monotone,
|
||||
chartType: report.chartType,
|
||||
lineType: (report.lineType as IChartLineType) ?? lineTypes.monotone,
|
||||
interval: report.interval,
|
||||
name: report.name || 'Untitled',
|
||||
range: (report.range as IChartRange) ?? timeRanges['1m'],
|
||||
@@ -64,7 +64,7 @@ export function getReportsByDashboardId(dashboardId: string) {
|
||||
return db.report
|
||||
.findMany({
|
||||
where: {
|
||||
dashboard_id: dashboardId,
|
||||
dashboardId,
|
||||
},
|
||||
})
|
||||
.then((reports) => reports.map(transformReport));
|
||||
|
||||
@@ -10,3 +10,11 @@ export function getShareOverviewById(id: string) {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function getShareByProjectId(projectId: string) {
|
||||
return db.shareOverview.findUnique({
|
||||
where: {
|
||||
projectId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user