Merge branch 'dev' into feat-2FA
This commit is contained in:
559 files changed
+21955
-9347
No files matched your search
@@ -1,8 +1,11 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import { BOARD_TYPES } from '@/lib/boards';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const BOARD_SORT_FIELDS = ['name', 'description', 'type', 'createdAt'] as const;
|
||||
|
||||
export async function findBoard(criteria: Prisma.BoardFindUniqueArgs) {
|
||||
return prisma.client.board.findUnique(criteria);
|
||||
}
|
||||
@@ -16,7 +19,8 @@ export async function getBoard(boardId: string) {
|
||||
}
|
||||
|
||||
export async function getBoards(criteria: Prisma.BoardFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, BOARD_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where: Prisma.BoardWhereInput = {
|
||||
@@ -24,7 +28,7 @@ export async function getBoards(criteria: Prisma.BoardFindManyArgs, filters: Que
|
||||
...getSearchParameters(search, [{ name: 'contains' }, { description: 'contains' }]),
|
||||
};
|
||||
|
||||
return pagedQuery('board', { ...criteria, where }, filters);
|
||||
return pagedQuery('board', { ...criteria, where }, sortFilters);
|
||||
}
|
||||
|
||||
export async function getUserBoards(userId: string, filters?: QueryFilters) {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const LINK_SORT_FIELDS = ['name', 'slug', 'url', 'createdAt'] as const;
|
||||
|
||||
export async function findLink(criteria: Prisma.LinkFindUniqueArgs) {
|
||||
return prisma.client.link.findUnique(criteria);
|
||||
}
|
||||
@@ -15,7 +18,8 @@ export async function getLink(linkId: string) {
|
||||
}
|
||||
|
||||
export async function getLinks(criteria: Prisma.LinkFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, LINK_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where: Prisma.LinkWhereInput = {
|
||||
@@ -27,7 +31,7 @@ export async function getLinks(criteria: Prisma.LinkFindManyArgs, filters: Query
|
||||
]),
|
||||
};
|
||||
|
||||
return pagedQuery('link', { ...criteria, where }, filters);
|
||||
return pagedQuery('link', { ...criteria, where }, sortFilters);
|
||||
}
|
||||
|
||||
export async function getUserLinks(userId: string, filters?: QueryFilters) {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const PIXEL_SORT_FIELDS = ['name', 'slug', 'createdAt'] as const;
|
||||
|
||||
export async function findPixel(criteria: Prisma.PixelFindUniqueArgs) {
|
||||
return prisma.client.pixel.findUnique(criteria);
|
||||
}
|
||||
@@ -15,14 +18,15 @@ export async function getPixel(pixelId: string) {
|
||||
}
|
||||
|
||||
export async function getPixels(criteria: Prisma.PixelFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, PIXEL_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
|
||||
const where: Prisma.PixelWhereInput = {
|
||||
...criteria.where,
|
||||
...prisma.getSearchParameters(search, [{ name: 'contains' }, { slug: 'contains' }]),
|
||||
};
|
||||
|
||||
return prisma.pagedQuery('pixel', { ...criteria, where }, filters);
|
||||
return prisma.pagedQuery('pixel', { ...criteria, where }, sortFilters);
|
||||
}
|
||||
|
||||
export async function getUserPixels(userId: string, filters?: QueryFilters) {
|
||||
|
||||
@@ -18,6 +18,7 @@ export async function getReport(reportId: string) {
|
||||
|
||||
export async function getReports(criteria: ReportFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const orderBy = criteria.orderBy ?? [{ name: 'asc' }, { id: 'asc' }];
|
||||
|
||||
const where: Prisma.ReportWhereInput = {
|
||||
...criteria.where,
|
||||
@@ -43,7 +44,7 @@ export async function getReports(criteria: ReportFindManyArgs, filters: QueryFil
|
||||
]),
|
||||
};
|
||||
|
||||
return prisma.pagedQuery('report', { ...criteria, where }, filters);
|
||||
return prisma.pagedQuery('report', { ...criteria, where, orderBy }, filters);
|
||||
}
|
||||
|
||||
export async function getUserReports(userId: string, filters?: QueryFilters) {
|
||||
|
||||
@@ -2,10 +2,14 @@ import { Prisma, type Team } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import prisma from '@/lib/prisma';
|
||||
import redis from '@/lib/redis';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { PageResult, QueryFilters } from '@/lib/types';
|
||||
|
||||
import TeamFindManyArgs = Prisma.TeamFindManyArgs;
|
||||
|
||||
const TEAM_SORT_FIELDS = ['name', 'createdAt'] as const;
|
||||
|
||||
export async function findTeam(criteria: Prisma.TeamFindUniqueArgs): Promise<Team> {
|
||||
return prisma.client.team.findUnique(criteria);
|
||||
}
|
||||
@@ -29,7 +33,8 @@ export async function getTeams(
|
||||
filters: QueryFilters,
|
||||
): Promise<PageResult<Team[]>> {
|
||||
const { getSearchParameters } = prisma;
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, TEAM_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
|
||||
const where: Prisma.TeamWhereInput = {
|
||||
...criteria.where,
|
||||
@@ -42,7 +47,7 @@ export async function getTeams(
|
||||
...criteria,
|
||||
where,
|
||||
},
|
||||
filters,
|
||||
sortFilters,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -100,6 +105,17 @@ export async function getAllUserTeams(userId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserOwnedTeamCount(userId: string) {
|
||||
return prisma.client.team.count({
|
||||
where: {
|
||||
deletedAt: null,
|
||||
members: {
|
||||
some: { userId, role: ROLES.teamOwner },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamOwner(teamId: string) {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where: { teamId, role: ROLES.teamOwner },
|
||||
@@ -144,6 +160,31 @@ export async function deleteTeam(teamId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
const [links, pixels, boards] = await Promise.all([
|
||||
client.link.findMany({
|
||||
where: { teamId },
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.pixel.findMany({
|
||||
where: { teamId },
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.board.findMany({ where: { teamId }, select: { id: true } }),
|
||||
]);
|
||||
const entityIds = [...links.map(l => l.id), ...pixels.map(p => p.id), ...boards.map(b => b.id)];
|
||||
// Only invalidate Redis cache for slugs that are still live (not already soft-deleted).
|
||||
const linkSlugs = links.filter(l => !l.deletedAt).map(l => l.slug);
|
||||
const pixelSlugs = pixels.filter(p => !p.deletedAt).map(p => p.slug);
|
||||
|
||||
const invalidateRedis = async () => {
|
||||
if (redis.enabled && (linkSlugs.length || pixelSlugs.length)) {
|
||||
await Promise.all([
|
||||
...linkSlugs.map(slug => redis.client.del(`link:${slug}`)),
|
||||
...pixelSlugs.map(slug => redis.client.del(`pixel:${slug}`)),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
if (cloudMode) {
|
||||
return transaction([
|
||||
client.team.update({
|
||||
@@ -154,7 +195,21 @@ export async function deleteTeam(teamId: string) {
|
||||
id: teamId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
// deletedAt: null avoids restamping rows that were already soft-deleted earlier.
|
||||
client.link.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { teamId, deletedAt: null },
|
||||
}),
|
||||
client.pixel.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { teamId, deletedAt: null },
|
||||
}),
|
||||
client.board.deleteMany({ where: { teamId } }),
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
return transaction([
|
||||
@@ -163,10 +218,17 @@ export async function deleteTeam(teamId: string) {
|
||||
teamId,
|
||||
},
|
||||
}),
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
client.link.deleteMany({ where: { teamId } }),
|
||||
client.pixel.deleteMany({ where: { teamId } }),
|
||||
client.board.deleteMany({ where: { teamId } }),
|
||||
client.team.delete({
|
||||
where: {
|
||||
id: teamId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
@@ -2,10 +2,14 @@ import { Prisma } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import { getRandomChars } from '@/lib/generate';
|
||||
import prisma from '@/lib/prisma';
|
||||
import redis from '@/lib/redis';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters, Role } from '@/lib/types';
|
||||
|
||||
import UserFindManyArgs = Prisma.UserFindManyArgs;
|
||||
|
||||
const USER_SORT_FIELDS = ['username', 'role', 'createdAt'] as const;
|
||||
|
||||
export interface GetUserOptions {
|
||||
includePassword?: boolean;
|
||||
showDeleted?: boolean;
|
||||
@@ -43,11 +47,15 @@ export async function getUser(userId: string, options: GetUserOptions = {}) {
|
||||
}
|
||||
|
||||
export async function getUserByUsername(username: string, options: GetUserOptions = {}) {
|
||||
return findUser({ where: { username } }, options);
|
||||
return findUser({ where: { username: username.toLowerCase() } }, options);
|
||||
}
|
||||
|
||||
export async function getUsers(criteria: UserFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, USER_SORT_FIELDS, {
|
||||
orderBy: 'createdAt',
|
||||
sortDescending: true,
|
||||
});
|
||||
const { search } = sortFilters;
|
||||
|
||||
const where: Prisma.UserWhereInput = {
|
||||
...criteria.where,
|
||||
@@ -61,11 +69,7 @@ export async function getUsers(criteria: UserFindManyArgs, filters: QueryFilters
|
||||
...criteria,
|
||||
where,
|
||||
},
|
||||
{
|
||||
orderBy: 'createdAt',
|
||||
sortDescending: true,
|
||||
...filters,
|
||||
},
|
||||
sortFilters,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -127,6 +131,33 @@ export async function deleteUser(userId: string) {
|
||||
|
||||
const teamIds = teams.map(a => a.id);
|
||||
|
||||
const ownedFilter = cloudMode ? { userId } : { OR: [{ userId }, { teamId: { in: teamIds } }] };
|
||||
|
||||
const [links, pixels, boards] = await Promise.all([
|
||||
client.link.findMany({
|
||||
where: ownedFilter,
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.pixel.findMany({
|
||||
where: ownedFilter,
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.board.findMany({ where: ownedFilter, select: { id: true } }),
|
||||
]);
|
||||
const entityIds = [...links.map(l => l.id), ...pixels.map(p => p.id), ...boards.map(b => b.id)];
|
||||
// Only invalidate Redis cache for slugs that are still live (not already soft-deleted).
|
||||
const linkSlugs = links.filter(l => !l.deletedAt).map(l => l.slug);
|
||||
const pixelSlugs = pixels.filter(p => !p.deletedAt).map(p => p.slug);
|
||||
|
||||
const invalidateRedis = async () => {
|
||||
if (redis.enabled && (linkSlugs.length || pixelSlugs.length)) {
|
||||
await Promise.all([
|
||||
...linkSlugs.map(slug => redis.client.del(`link:${slug}`)),
|
||||
...pixelSlugs.map(slug => redis.client.del(`pixel:${slug}`)),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
if (cloudMode) {
|
||||
return transaction([
|
||||
client.website.updateMany({
|
||||
@@ -144,7 +175,21 @@ export async function deleteUser(userId: string) {
|
||||
id: userId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
// deletedAt: null avoids restamping rows that were already soft-deleted earlier.
|
||||
client.link.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { userId, deletedAt: null },
|
||||
}),
|
||||
client.pixel.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { userId, deletedAt: null },
|
||||
}),
|
||||
client.board.deleteMany({ where: { userId } }),
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
return transaction([
|
||||
@@ -195,6 +240,10 @@ export async function deleteUser(userId: string) {
|
||||
],
|
||||
},
|
||||
}),
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
client.link.deleteMany({ where: ownedFilter }),
|
||||
client.pixel.deleteMany({ where: ownedFilter }),
|
||||
client.board.deleteMany({ where: ownedFilter }),
|
||||
client.website.deleteMany({
|
||||
where: { id: { in: websiteIds } },
|
||||
}),
|
||||
@@ -203,5 +252,8 @@ export async function deleteUser(userId: string) {
|
||||
id: userId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
@@ -2,8 +2,11 @@ import type { Prisma, Website } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import prisma from '@/lib/prisma';
|
||||
import redis from '@/lib/redis';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const WEBSITE_SORT_FIELDS = ['name', 'domain', 'createdAt'] as const;
|
||||
|
||||
export async function findWebsite(criteria: Prisma.WebsiteFindUniqueArgs) {
|
||||
return prisma.client.website.findUnique(criteria);
|
||||
}
|
||||
@@ -23,7 +26,8 @@ export async function getWebsite(websiteId: string) {
|
||||
}
|
||||
|
||||
export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters: QueryFilters) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, WEBSITE_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where: Prisma.WebsiteWhereInput = {
|
||||
@@ -37,12 +41,15 @@ export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters:
|
||||
deletedAt: null,
|
||||
};
|
||||
|
||||
const websites = await pagedQuery('website', { ...criteria, where }, filters);
|
||||
const websites = await pagedQuery('website', { ...criteria, where }, sortFilters);
|
||||
|
||||
return attachShareIdToWebsites(websites);
|
||||
}
|
||||
|
||||
export async function getAllUserWebsitesIncludingTeamOwner(userId: string, filters?: QueryFilters) {
|
||||
export async function getAllUserWebsitesIncludingTeamAccess(
|
||||
userId: string,
|
||||
filters?: QueryFilters,
|
||||
) {
|
||||
return getWebsites(
|
||||
{
|
||||
where: {
|
||||
@@ -53,7 +60,7 @@ export async function getAllUserWebsitesIncludingTeamOwner(userId: string, filte
|
||||
deletedAt: null,
|
||||
members: {
|
||||
some: {
|
||||
role: ROLES.teamOwner,
|
||||
role: { in: [ROLES.teamOwner, ROLES.teamManager] },
|
||||
userId,
|
||||
},
|
||||
},
|
||||
@@ -62,10 +69,7 @@ export async function getAllUserWebsitesIncludingTeamOwner(userId: string, filte
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
orderBy: 'name',
|
||||
...filters,
|
||||
},
|
||||
sanitizeSortFilters(filters, WEBSITE_SORT_FIELDS, { orderBy: 'name' }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -84,10 +88,7 @@ export async function getUserWebsites(userId: string, filters?: QueryFilters) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
orderBy: 'name',
|
||||
...filters,
|
||||
},
|
||||
sanitizeSortFilters(filters, WEBSITE_SORT_FIELDS, { orderBy: 'name' }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -265,6 +266,15 @@ export async function getWebsiteCount(userId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamWebsiteCount(teamId: string) {
|
||||
return prisma.client.website.count({
|
||||
where: {
|
||||
teamId,
|
||||
deletedAt: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function attachShareIdToWebsite(website: Website) {
|
||||
const share = await prisma.client.share.findFirst({
|
||||
where: {
|
||||
|
||||
Reference in new issue
Block a user