website API shareId compatibility updates
This commit is contained in:
1 parent
fdb99607bc
commit
2489601ae6
4 files changed
+147
-11
No files matched your search
@@ -22,6 +22,17 @@ export async function getShareByCode(slug: string) {
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -62,3 +73,11 @@ export async function updateShare(
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -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,11 +9,13 @@ export async function findWebsite(criteria: Prisma.WebsiteFindUniqueArgs) {
|
||||
}
|
||||
|
||||
export async function getWebsite(websiteId: string) {
|
||||
return findWebsite({
|
||||
const website = await findWebsite({
|
||||
where: {
|
||||
id: websiteId,
|
||||
},
|
||||
});
|
||||
|
||||
return attachShareIdToWebsite(website);
|
||||
}
|
||||
|
||||
export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters: QueryFilters) {
|
||||
@@ -31,7 +33,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) {
|
||||
@@ -203,6 +207,10 @@ export async function deleteWebsite(websiteId: string) {
|
||||
where: { websiteId },
|
||||
});
|
||||
|
||||
await tx.share.deleteMany({
|
||||
where: { entityId: websiteId },
|
||||
});
|
||||
|
||||
const website = cloudMode
|
||||
? await tx.website.update({
|
||||
data: {
|
||||
@@ -236,3 +244,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