Merge branch 'dev' into fix/3981-login-email-case
This commit is contained in:
545 files changed
+33675
-142714
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
export async function findBoard(criteria: Prisma.BoardFindUniqueArgs) {
|
||||
return prisma.client.board.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getBoard(boardId: string) {
|
||||
return findBoard({
|
||||
where: {
|
||||
id: boardId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getBoards(criteria: Prisma.BoardFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where: Prisma.BoardWhereInput = {
|
||||
...criteria.where,
|
||||
...getSearchParameters(search, [{ name: 'contains' }, { description: 'contains' }]),
|
||||
};
|
||||
|
||||
return pagedQuery('board', { ...criteria, where }, filters);
|
||||
}
|
||||
|
||||
export async function getUserBoards(userId: string, filters?: QueryFilters) {
|
||||
return getBoards(
|
||||
{
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
filters,
|
||||
);
|
||||
}
|
||||
|
||||
export async function getTeamBoards(teamId: string, filters?: QueryFilters) {
|
||||
return getBoards(
|
||||
{
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
},
|
||||
filters,
|
||||
);
|
||||
}
|
||||
|
||||
export async function createBoard(data: Prisma.BoardUncheckedCreateInput) {
|
||||
return prisma.client.board.create({ data });
|
||||
}
|
||||
|
||||
export async function updateBoard(boardId: string, data: any) {
|
||||
return prisma.client.board.update({ where: { id: boardId }, data });
|
||||
}
|
||||
|
||||
export async function deleteBoard(boardId: string) {
|
||||
return prisma.client.board.delete({ where: { id: boardId } });
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
export * from './board';
|
||||
export * from './link';
|
||||
export * from './pixel';
|
||||
export * from './report';
|
||||
export * from './segment';
|
||||
export * from './share';
|
||||
export * from './team';
|
||||
export * from './teamUser';
|
||||
export * from './user';
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
export async function findShare(criteria: Prisma.ShareFindUniqueArgs) {
|
||||
return prisma.client.share.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getShare(shareId: string) {
|
||||
return findShare({
|
||||
where: {
|
||||
id: shareId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getShareByCode(slug: string) {
|
||||
return findShare({
|
||||
where: {
|
||||
slug,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getShareByEntityId(entityId: string) {
|
||||
return prisma.client.share.findFirst({
|
||||
where: {
|
||||
entityId,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSharesByEntityId(entityId: string, filters?: QueryFilters) {
|
||||
const { pagedQuery } = prisma;
|
||||
|
||||
return pagedQuery(
|
||||
'share',
|
||||
{
|
||||
where: {
|
||||
entityId,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
},
|
||||
filters,
|
||||
);
|
||||
}
|
||||
|
||||
export async function createShare(
|
||||
data: Prisma.ShareCreateInput | Prisma.ShareUncheckedCreateInput,
|
||||
) {
|
||||
return prisma.client.share.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateShare(
|
||||
shareId: string,
|
||||
data: Prisma.ShareUpdateInput | Prisma.ShareUncheckedUpdateInput,
|
||||
) {
|
||||
return prisma.client.share.update({
|
||||
where: {
|
||||
id: shareId,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteShare(shareId: string) {
|
||||
return prisma.client.share.delete({ where: { id: shareId } });
|
||||
}
|
||||
|
||||
export async function deleteSharesByEntityId(entityId: string) {
|
||||
return prisma.client.share.deleteMany({
|
||||
where: {
|
||||
entityId,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -21,7 +21,7 @@ async function findUser(
|
||||
...criteria,
|
||||
where: {
|
||||
...criteria.where,
|
||||
...(showDeleted && { deletedAt: null }),
|
||||
...(showDeleted ? {} : { deletedAt: null }),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
+124
-51
@@ -1,4 +1,4 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import type { Prisma, Website } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import prisma from '@/lib/prisma';
|
||||
import redis from '@/lib/redis';
|
||||
@@ -9,20 +9,17 @@ export async function findWebsite(criteria: Prisma.WebsiteFindUniqueArgs) {
|
||||
}
|
||||
|
||||
export async function getWebsite(websiteId: string) {
|
||||
return findWebsite({
|
||||
const website = await findWebsite({
|
||||
where: {
|
||||
id: websiteId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSharedWebsite(shareId: string) {
|
||||
return findWebsite({
|
||||
where: {
|
||||
shareId,
|
||||
deletedAt: null,
|
||||
},
|
||||
});
|
||||
if (!website) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return attachShareIdToWebsite(website);
|
||||
}
|
||||
|
||||
export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters: QueryFilters) {
|
||||
@@ -40,7 +37,9 @@ export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters:
|
||||
deletedAt: null,
|
||||
};
|
||||
|
||||
return pagedQuery('website', { ...criteria, where }, filters);
|
||||
const websites = await pagedQuery('website', { ...criteria, where }, filters);
|
||||
|
||||
return attachShareIdToWebsites(websites);
|
||||
}
|
||||
|
||||
export async function getAllUserWebsitesIncludingTeamOwner(userId: string, filters?: QueryFilters) {
|
||||
@@ -132,42 +131,46 @@ export async function updateWebsite(
|
||||
}
|
||||
|
||||
export async function resetWebsite(websiteId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const { transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
return transaction(
|
||||
[
|
||||
client.revenue.deleteMany({
|
||||
async tx => {
|
||||
await tx.revenue.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.eventData.deleteMany({
|
||||
});
|
||||
|
||||
await tx.eventData.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.sessionData.deleteMany({
|
||||
});
|
||||
|
||||
await tx.sessionData.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.websiteEvent.deleteMany({
|
||||
});
|
||||
|
||||
await tx.websiteEvent.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
});
|
||||
|
||||
await tx.session.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.website.update({
|
||||
});
|
||||
|
||||
const website = await tx.website.update({
|
||||
where: { id: websiteId },
|
||||
data: {
|
||||
resetAt: new Date(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
return website;
|
||||
},
|
||||
{
|
||||
timeout: 30000,
|
||||
},
|
||||
).then(async data => {
|
||||
if (cloudMode) {
|
||||
await redis.client.set(
|
||||
`website:${websiteId}`,
|
||||
data.find(website => website.id),
|
||||
);
|
||||
await redis.client.set(`website:${websiteId}`, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
@@ -175,43 +178,56 @@ export async function resetWebsite(websiteId: string) {
|
||||
}
|
||||
|
||||
export async function deleteWebsite(websiteId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const { transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
return transaction(
|
||||
[
|
||||
client.revenue.deleteMany({
|
||||
async tx => {
|
||||
await tx.revenue.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.eventData.deleteMany({
|
||||
});
|
||||
|
||||
await tx.eventData.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.sessionData.deleteMany({
|
||||
});
|
||||
|
||||
await tx.sessionData.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.websiteEvent.deleteMany({
|
||||
});
|
||||
|
||||
await tx.websiteEvent.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
});
|
||||
|
||||
await tx.session.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.report.deleteMany({
|
||||
});
|
||||
|
||||
await tx.report.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.segment.deleteMany({
|
||||
});
|
||||
|
||||
await tx.segment.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
cloudMode
|
||||
? client.website.update({
|
||||
});
|
||||
|
||||
await tx.share.deleteMany({
|
||||
where: { entityId: websiteId },
|
||||
});
|
||||
|
||||
const website = cloudMode
|
||||
? await tx.website.update({
|
||||
data: {
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: { id: websiteId },
|
||||
})
|
||||
: client.website.delete({
|
||||
: await tx.website.delete({
|
||||
where: { id: websiteId },
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
return website;
|
||||
},
|
||||
{
|
||||
timeout: 30000,
|
||||
},
|
||||
@@ -232,3 +248,60 @@ export async function getWebsiteCount(userId: string) {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function attachShareIdToWebsite(website: Website) {
|
||||
const share = await prisma.client.share.findFirst({
|
||||
where: {
|
||||
entityId: website.id,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
select: {
|
||||
slug: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...website,
|
||||
shareId: share?.slug ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export async function attachShareIdToWebsites(websites: {
|
||||
data: any;
|
||||
count: any;
|
||||
page: number;
|
||||
pageSize: number;
|
||||
orderBy: string;
|
||||
search: string;
|
||||
}) {
|
||||
const websiteIds = websites.data.map(website => website.id);
|
||||
|
||||
if (websiteIds.length === 0) {
|
||||
return {
|
||||
...websites,
|
||||
data: websites.data.map(website => ({ ...website, shareId: null })),
|
||||
};
|
||||
}
|
||||
|
||||
const shares = await prisma.client.share.findMany({
|
||||
where: {
|
||||
entityId: { in: websiteIds },
|
||||
},
|
||||
distinct: ['entityId'],
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
|
||||
const shareByWebsiteId = new Map(shares.map(share => [share.entityId, share.slug]));
|
||||
|
||||
return {
|
||||
...websites,
|
||||
data: websites.data.map(website => ({
|
||||
...website,
|
||||
shareId: shareByWebsiteId.get(website.id) ?? null,
|
||||
})),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user