Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+424
@@ -0,0 +1,424 @@
|
||||
import { format } from "date-fns";
|
||||
import { db } from "../../db.server";
|
||||
import { adminGetAllUsersNamesInIds } from "../../db/users.db.server";
|
||||
import DateUtils from "../../shared/DateUtils";
|
||||
import PeriodHelper, { PeriodFilter } from "../PeriodHelper";
|
||||
import { AnalyticsInfoDto } from "~/application/dtos/marketing/AnalyticsInfoDto";
|
||||
import { getClientIPAddress } from "~/utils/server/IpUtils";
|
||||
import AnalyticsHelper from "../AnalyticsHelper";
|
||||
import { AppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
||||
import { cachified, clearCacheKey } from "~/utils/cache.server";
|
||||
import { IpAddressDto } from "~/modules/ipAddress/dtos/IpAddressDto";
|
||||
import IpAddressServiceServer from "~/modules/ipAddress/services/IpAddressService.server";
|
||||
|
||||
async function getOrCreateUniqueVisitor({
|
||||
cookie,
|
||||
fromUrl,
|
||||
fromRoute,
|
||||
analytics,
|
||||
}: {
|
||||
cookie: string;
|
||||
fromUrl?: string;
|
||||
fromRoute?: string;
|
||||
analytics: AnalyticsInfoDto;
|
||||
}) {
|
||||
const visitor = await cachified({
|
||||
key: `analytics:uniqueVisitor:${cookie}`,
|
||||
ttl: 60 * 60,
|
||||
getFreshValue: async () => {
|
||||
return db.analyticsUniqueVisitor.findUnique({
|
||||
where: {
|
||||
cookie,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
if (visitor) {
|
||||
return visitor;
|
||||
}
|
||||
// console.log("Creating unique visitor", { referrer: analytics.referrer });
|
||||
return await db.analyticsUniqueVisitor
|
||||
.create({
|
||||
data: {
|
||||
cookie,
|
||||
httpReferrer: analytics.referrer?.http,
|
||||
via: analytics.referrer?.via,
|
||||
browser: analytics.userAgent?.browser.name,
|
||||
browserVersion: analytics.userAgent?.browser.version,
|
||||
os: analytics.userAgent?.os.family,
|
||||
osVersion: analytics.userAgent?.os.version,
|
||||
device: analytics.userAgent?.type,
|
||||
source: analytics.referrer?.source,
|
||||
medium: analytics.referrer?.utm_medium,
|
||||
campaign: analytics.referrer?.utm_campaign,
|
||||
content: analytics.referrer?.utm_content,
|
||||
term: analytics.referrer?.utm_term,
|
||||
country: analytics.region?.country,
|
||||
city: analytics.region?.city,
|
||||
fromUrl,
|
||||
fromRoute,
|
||||
userId: analytics.userId ?? null,
|
||||
portalId: analytics.portalId ?? null,
|
||||
portalUserId: analytics.portalUserId ?? null,
|
||||
},
|
||||
})
|
||||
.then((e) => {
|
||||
clearCacheKey(`analytics:uniqueVisitor:${cookie}`);
|
||||
return e;
|
||||
});
|
||||
}
|
||||
|
||||
export type AnalyticsOverviewDto = {
|
||||
uniqueVisitors: number;
|
||||
pageViews: number;
|
||||
events: number;
|
||||
liveVisitors: number;
|
||||
top: {
|
||||
sources: { name: string | null; count: number }[];
|
||||
httpReferrers: { name: string | null; count: number }[];
|
||||
urls: { name: string | null; count: number }[];
|
||||
routes: { name: string | null; count: number }[];
|
||||
os: { name: string | null; count: number }[];
|
||||
devices: { name: string | null; count: number }[];
|
||||
countries: { name: string | null; count: number }[];
|
||||
via: { name: string | null; count: number }[];
|
||||
medium: { name: string | null; count: number }[];
|
||||
campaign: { name: string | null; count: number }[];
|
||||
user: { name: string | null; count: number }[];
|
||||
};
|
||||
charts?: {
|
||||
pageViewsAndUniqueVisitors: {
|
||||
date: string;
|
||||
"Page views": number; // "Page Views"
|
||||
"Unique visitors": number; // "Unique Visitors"
|
||||
}[];
|
||||
};
|
||||
};
|
||||
type AnalyticsChart = "visitors" | "pages" | "events";
|
||||
async function getAnalyticsOverview({ withUsers, period, portalId }: { withUsers: boolean; period: PeriodFilter; portalId: string | null | undefined }) {
|
||||
const createdAt = PeriodHelper.getCreatedAt(period);
|
||||
if (period === "all-time") {
|
||||
const firstDate = await db.analyticsUniqueVisitor.findFirst({
|
||||
where: { portalId },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
if (firstDate) {
|
||||
createdAt.gte = firstDate.createdAt;
|
||||
}
|
||||
}
|
||||
const uniqueVisitors = await db.analyticsUniqueVisitor.count({
|
||||
where: { portalId, createdAt },
|
||||
});
|
||||
const pageViews = await db.analyticsPageView.count({
|
||||
where: {
|
||||
portalId,
|
||||
createdAt,
|
||||
},
|
||||
});
|
||||
const events = await db.analyticsEvent.count({
|
||||
where: {
|
||||
portalId,
|
||||
createdAt,
|
||||
},
|
||||
});
|
||||
const liveVisitors = await db.analyticsPageView.groupBy({
|
||||
by: ["uniqueVisitorId"],
|
||||
where: {
|
||||
portalId,
|
||||
createdAt: {
|
||||
gte: new Date(Date.now() - 1 * 60000),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const topHttpReferrers = (
|
||||
await db.analyticsUniqueVisitor.groupBy({
|
||||
by: ["httpReferrer"],
|
||||
_count: true,
|
||||
where: { portalId, createdAt },
|
||||
})
|
||||
).map((f) => {
|
||||
return { name: f.httpReferrer, count: f._count };
|
||||
});
|
||||
const topSources = (
|
||||
await db.analyticsUniqueVisitor.groupBy({
|
||||
by: ["source"],
|
||||
_count: true,
|
||||
where: { portalId, createdAt },
|
||||
})
|
||||
).map((f) => {
|
||||
return { name: f.source, count: f._count };
|
||||
});
|
||||
let topUrls = (
|
||||
await db.analyticsPageView.groupBy({
|
||||
by: ["url"],
|
||||
_count: true,
|
||||
where: { portalId, createdAt },
|
||||
})
|
||||
)
|
||||
.map((f) => {
|
||||
return { name: f.url, count: f._count };
|
||||
})
|
||||
.sort((a, b) => b.count - a.count);
|
||||
|
||||
if (!withUsers) {
|
||||
if (topUrls.length > 10) {
|
||||
topUrls = topUrls.slice(0, 10);
|
||||
}
|
||||
}
|
||||
const topRoutes = (
|
||||
await db.analyticsPageView.groupBy({
|
||||
by: ["route"],
|
||||
_count: true,
|
||||
where: { portalId, createdAt },
|
||||
})
|
||||
).map((f) => {
|
||||
return { name: f.route, count: f._count };
|
||||
});
|
||||
const topOs = (await db.analyticsUniqueVisitor.groupBy({ by: ["os"], _count: true, where: { portalId, createdAt } })).map((f) => {
|
||||
return { name: f.os, count: f._count };
|
||||
});
|
||||
const topDevices = (await db.analyticsUniqueVisitor.groupBy({ by: ["device"], _count: true, where: { portalId, createdAt } })).map((f) => {
|
||||
return { name: f.device, count: f._count };
|
||||
});
|
||||
const topCountries = (await db.analyticsUniqueVisitor.groupBy({ by: ["country"], _count: true, where: { portalId, createdAt } })).map((f) => {
|
||||
return { name: f.country, count: f._count };
|
||||
});
|
||||
const topVia = (await db.analyticsUniqueVisitor.groupBy({ by: ["via"], _count: true, where: { portalId, createdAt } })).map((f) => {
|
||||
return { name: f.via, count: f._count };
|
||||
});
|
||||
const topMedium = (await db.analyticsUniqueVisitor.groupBy({ by: ["medium"], _count: true, where: { portalId, createdAt } })).map((f) => {
|
||||
return { name: f.medium, count: f._count };
|
||||
});
|
||||
const topCampaign = (await db.analyticsUniqueVisitor.groupBy({ by: ["campaign"], _count: true, where: { portalId, createdAt } })).map((f) => {
|
||||
return { name: f.campaign, count: f._count };
|
||||
});
|
||||
let topUserIds: {
|
||||
id: string;
|
||||
count: number;
|
||||
}[] = [];
|
||||
if (withUsers) {
|
||||
topUserIds = (await db.analyticsUniqueVisitor.groupBy({ by: ["userId"], _count: true, where: { portalId, createdAt } })).map((f) => {
|
||||
return { id: f.userId ?? "", count: f._count };
|
||||
});
|
||||
}
|
||||
const users = await adminGetAllUsersNamesInIds(topUserIds.map((f) => f.id));
|
||||
const topUsers = topUserIds.map((f) => {
|
||||
const user = users.find((u) => u.id === f.id);
|
||||
return { name: user?.email ?? "", count: f.count };
|
||||
});
|
||||
|
||||
const charts = await getCharts({ period, portalId });
|
||||
|
||||
const data: AnalyticsOverviewDto = {
|
||||
uniqueVisitors,
|
||||
pageViews,
|
||||
events,
|
||||
liveVisitors: liveVisitors.length,
|
||||
top: {
|
||||
httpReferrers: topHttpReferrers.sort((a, b) => b.count - a.count),
|
||||
sources: topSources.sort((a, b) => b.count - a.count),
|
||||
urls: topUrls,
|
||||
routes: topRoutes.sort((a, b) => b.count - a.count),
|
||||
os: topOs.sort((a, b) => b.count - a.count),
|
||||
devices: topDevices.sort((a, b) => b.count - a.count),
|
||||
countries: topCountries.sort((a, b) => b.count - a.count),
|
||||
via: topVia.sort((a, b) => b.count - a.count),
|
||||
medium: topMedium.sort((a, b) => b.count - a.count),
|
||||
campaign: topCampaign.sort((a, b) => b.count - a.count),
|
||||
user: topUsers.sort((a, b) => b.count - a.count),
|
||||
},
|
||||
charts,
|
||||
};
|
||||
return data;
|
||||
}
|
||||
|
||||
async function getCharts({ period, portalId }: { period: PeriodFilter; portalId: string | null | undefined }) {
|
||||
const createdAt = PeriodHelper.getCreatedAt(period);
|
||||
if (period === "all-time") {
|
||||
const firstDate = await db.analyticsUniqueVisitor.findFirst({
|
||||
where: { portalId },
|
||||
orderBy: {
|
||||
createdAt: "asc",
|
||||
},
|
||||
});
|
||||
if (firstDate) {
|
||||
createdAt.gte = firstDate.createdAt;
|
||||
}
|
||||
}
|
||||
const pageViewsByDay = await db.analyticsPageView.groupBy({
|
||||
by: ["createdAt"],
|
||||
_count: true,
|
||||
where: { portalId, createdAt },
|
||||
});
|
||||
|
||||
const uniqueVisitorsByDay = await db.analyticsUniqueVisitor.groupBy({
|
||||
by: ["createdAt"],
|
||||
_count: true,
|
||||
where: { portalId, createdAt },
|
||||
});
|
||||
|
||||
const toDateString = (date: Date) => {
|
||||
if (period === "last-24-hours") {
|
||||
return format(date, "ha");
|
||||
} else if (period === "last-hour" || period === "last-10-minutes") {
|
||||
return format(date, "h:mm a");
|
||||
}
|
||||
return DateUtils.dateMonthDay(date);
|
||||
};
|
||||
|
||||
// Aggregate counts by date
|
||||
const aggregateCounts = (data: { createdAt: Date; _count: number }[]) => {
|
||||
const countMap = new Map();
|
||||
data.forEach((item) => {
|
||||
const dateKey = toDateString(item.createdAt);
|
||||
countMap.set(dateKey, (countMap.get(dateKey) || 0) + item._count);
|
||||
});
|
||||
return countMap;
|
||||
};
|
||||
|
||||
const pageViewsMap = aggregateCounts(pageViewsByDay);
|
||||
const uniqueVisitorsMap = aggregateCounts(uniqueVisitorsByDay);
|
||||
|
||||
const charts: AnalyticsOverviewDto["charts"] = {
|
||||
pageViewsAndUniqueVisitors: [],
|
||||
};
|
||||
|
||||
// from createdAt.gte to createdAt.lte (or today), each day
|
||||
const currentDate = new Date();
|
||||
let date = new Date(createdAt.gte ?? currentDate);
|
||||
|
||||
let i = 0;
|
||||
if (period === "last-24-hours") {
|
||||
date = new Date();
|
||||
date.setHours(date.getHours() - 23);
|
||||
for (let i = 1; i <= 24; i++) {
|
||||
const dateKey = toDateString(date);
|
||||
charts.pageViewsAndUniqueVisitors.push({
|
||||
date: dateKey,
|
||||
"Page views": pageViewsMap.get(dateKey) ?? 0,
|
||||
"Unique visitors": uniqueVisitorsMap.get(dateKey) ?? 0,
|
||||
});
|
||||
date.setHours(date.getHours() + 1);
|
||||
}
|
||||
return charts;
|
||||
} else if (period === "last-hour") {
|
||||
date = new Date();
|
||||
date.setMinutes(date.getMinutes() - 59);
|
||||
for (let i = 1; i <= 60; i++) {
|
||||
const dateKey = toDateString(date);
|
||||
charts.pageViewsAndUniqueVisitors.push({
|
||||
date: dateKey,
|
||||
"Page views": pageViewsMap.get(dateKey) ?? 0,
|
||||
"Unique visitors": uniqueVisitorsMap.get(dateKey) ?? 0,
|
||||
});
|
||||
date.setMinutes(date.getMinutes() + 1);
|
||||
}
|
||||
return charts;
|
||||
} else if (period === "last-10-minutes") {
|
||||
date = new Date();
|
||||
date.setMinutes(date.getMinutes() - 9);
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
const dateKey = toDateString(date);
|
||||
charts.pageViewsAndUniqueVisitors.push({
|
||||
date: dateKey,
|
||||
"Page views": pageViewsMap.get(dateKey) ?? 0,
|
||||
"Unique visitors": uniqueVisitorsMap.get(dateKey) ?? 0,
|
||||
});
|
||||
date.setMinutes(date.getMinutes() + 1);
|
||||
}
|
||||
return charts;
|
||||
} else {
|
||||
while (date <= (createdAt.lte ?? currentDate) && i < 1000) {
|
||||
const dateKey = toDateString(date);
|
||||
charts.pageViewsAndUniqueVisitors.push({
|
||||
date: dateKey,
|
||||
"Page views": pageViewsMap.get(dateKey) ?? 0,
|
||||
"Unique visitors": uniqueVisitorsMap.get(dateKey) ?? 0,
|
||||
});
|
||||
date.setDate(date.getDate() + 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return charts;
|
||||
}
|
||||
|
||||
async function getPageViews(filters: {
|
||||
url?: {
|
||||
startsWith?: string;
|
||||
contains?: string;
|
||||
};
|
||||
route?: {
|
||||
startsWith?: string;
|
||||
contains?: string;
|
||||
};
|
||||
}) {
|
||||
return await db.analyticsPageView.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{ url: { startsWith: filters.url?.startsWith, contains: filters.url?.contains } },
|
||||
{ route: { startsWith: filters.route?.startsWith, contains: filters.route?.contains } },
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function getFromRequest({ appConfiguration, request, userId }: { appConfiguration: AppConfiguration; request: Request; userId: string | null }) {
|
||||
const searchParams = new URL(request.url).searchParams;
|
||||
|
||||
// console.log({ headers: request.headers });
|
||||
let ip = getClientIPAddress(request)?.toString() || "";
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
ip = "134.201.250.155";
|
||||
}
|
||||
const userAgent = request.headers.get("user-agent") ?? navigator.userAgent;
|
||||
const httpReferrer = request.headers.get("referer")?.toString();
|
||||
const ref = searchParams.get("ref")?.toString();
|
||||
const via = searchParams.get("via")?.toString();
|
||||
const source = searchParams.get("source")?.toString();
|
||||
const utm_source = searchParams.get("utm_source")?.toString();
|
||||
const utm_medium = searchParams.get("utm_medium")?.toString();
|
||||
const utm_campaign = searchParams.get("utm_campaign")?.toString();
|
||||
const utm_content = searchParams.get("utm_content")?.toString();
|
||||
const utm_term = searchParams.get("utm_term")?.toString();
|
||||
let mergedSource = "";
|
||||
if (ref && ref?.length > 0) {
|
||||
mergedSource = ref;
|
||||
} else if (source && source?.length > 0) {
|
||||
mergedSource = source;
|
||||
} else if (utm_source && utm_source?.length > 0) {
|
||||
mergedSource = utm_source;
|
||||
}
|
||||
let ipAddress: IpAddressDto | null = null;
|
||||
if (ip && appConfiguration.analytics.ipLookup) {
|
||||
ipAddress = await IpAddressServiceServer.getOrCreateIpAddressLookup(ip);
|
||||
}
|
||||
|
||||
const analytics: AnalyticsInfoDto = {
|
||||
ip,
|
||||
userAgent: AnalyticsHelper.getUserAgentDetails(userAgent),
|
||||
referrer: {
|
||||
http: httpReferrer,
|
||||
via,
|
||||
source: mergedSource,
|
||||
utm_medium,
|
||||
utm_campaign,
|
||||
utm_content,
|
||||
utm_term,
|
||||
},
|
||||
userId: userId && userId.length > 0 ? userId : null,
|
||||
region: {
|
||||
country: ipAddress?.countryCode || undefined,
|
||||
city: ipAddress?.city || undefined,
|
||||
},
|
||||
};
|
||||
return analytics;
|
||||
}
|
||||
|
||||
export default {
|
||||
getOrCreateUniqueVisitor,
|
||||
getAnalyticsOverview,
|
||||
getPageViews,
|
||||
getFromRequest,
|
||||
};
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import { ApiKeyEntity, Entity } from "@toolctrl/db/prisma-browser";
|
||||
import { ApiKeyEntityPermissionDto } from "~/application/dtos/apiKeys/ApiKeyEntityPermissionDto";
|
||||
import { getEntitiesInIds } from "~/utils/db/entities/entities.db.server";
|
||||
|
||||
export async function getApiKeyEntityPermissions(
|
||||
entities:
|
||||
| {
|
||||
entityId: string;
|
||||
create: boolean;
|
||||
read: boolean;
|
||||
update: boolean;
|
||||
delete: boolean;
|
||||
}[]
|
||||
| (ApiKeyEntity & { entity: Entity })[]
|
||||
): Promise<ApiKeyEntityPermissionDto[]> {
|
||||
const apiKeyEntities = await getEntitiesInIds(entities.map((f) => f.entityId));
|
||||
const entityPermissions: ApiKeyEntityPermissionDto[] = [];
|
||||
entities.forEach((apiKeyEntity) => {
|
||||
const entity = apiKeyEntities.find((f) => f.id === apiKeyEntity.entityId);
|
||||
entityPermissions.push({
|
||||
id: apiKeyEntity.entityId,
|
||||
name: entity?.name ?? "",
|
||||
create: apiKeyEntity.create,
|
||||
read: apiKeyEntity.read,
|
||||
update: apiKeyEntity.update,
|
||||
delete: apiKeyEntity.delete,
|
||||
});
|
||||
});
|
||||
return entityPermissions;
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
import { cachified, clearCacheKey } from "~/utils/cache.server";
|
||||
import { db } from "~/utils/db.server";
|
||||
|
||||
export type IpAddressDto = {
|
||||
ip: string;
|
||||
provider: string;
|
||||
type: string;
|
||||
countryCode: string;
|
||||
countryName: string;
|
||||
regionCode: string;
|
||||
regionName: string;
|
||||
city: string;
|
||||
zipCode: string;
|
||||
latitude: number | null;
|
||||
longitude: number | null;
|
||||
metadata: { [key: string]: any } | null;
|
||||
};
|
||||
export async function getOrCreateIpAddressLookup(ip: string): Promise<IpAddressDto | null> {
|
||||
let provider: string | null = null;
|
||||
if (process.env.IPSTACK_API_KEY) {
|
||||
provider = "ipstack";
|
||||
}
|
||||
if (!provider) {
|
||||
return null;
|
||||
}
|
||||
const item = await cachified({
|
||||
key: `ipAddress:${ip}`,
|
||||
getFreshValue: () =>
|
||||
db.ipAddress.findUnique({
|
||||
where: { ip },
|
||||
}),
|
||||
});
|
||||
if (item) {
|
||||
// eslint-disable-next-line no-console
|
||||
// console.log("[getOrCreateIpAddressLookup] Found in cache", ip);
|
||||
try {
|
||||
const dto: IpAddressDto = {
|
||||
ip,
|
||||
provider,
|
||||
type: item.type,
|
||||
countryCode: item.countryCode,
|
||||
countryName: item.countryName,
|
||||
regionCode: item.regionCode,
|
||||
regionName: item.regionName,
|
||||
city: item.city,
|
||||
zipCode: item.zipCode,
|
||||
latitude: item.latitude ? Number(item.latitude) : null,
|
||||
longitude: item.longitude ? Number(item.longitude) : null,
|
||||
metadata: JSON.parse(item.metadata),
|
||||
};
|
||||
return dto;
|
||||
} catch (error: any) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("[getOrCreateIpAddressLookup] Error parsing metadata", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
// console.log("[getOrCreateIpAddressLookup] Not found in cache, fetching", ip);
|
||||
try {
|
||||
const response = await fetch(`http://api.ipstack.com/${ip}?access_key=${process.env.IPSTACK_API_KEY}`);
|
||||
const data = await response.json();
|
||||
const dto: IpAddressDto = {
|
||||
ip,
|
||||
provider,
|
||||
type: data.type,
|
||||
countryCode: data.country_code,
|
||||
countryName: data.country_name,
|
||||
regionCode: data.region_code,
|
||||
regionName: data.region_name,
|
||||
city: data.city,
|
||||
zipCode: data.zip,
|
||||
latitude: data.latitude,
|
||||
longitude: data.longitude,
|
||||
metadata: data,
|
||||
};
|
||||
await db.ipAddress
|
||||
.create({
|
||||
data: {
|
||||
ip,
|
||||
provider,
|
||||
type: data.type,
|
||||
countryCode: data.country_code,
|
||||
countryName: data.country_name,
|
||||
regionCode: data.region_code,
|
||||
regionName: data.region_name,
|
||||
city: data.city,
|
||||
zipCode: data.zip,
|
||||
latitude: data.latitude,
|
||||
longitude: data.longitude,
|
||||
metadata: JSON.stringify(data),
|
||||
},
|
||||
})
|
||||
.then((item) => {
|
||||
clearCacheKey(`ipAddress:${ip}`);
|
||||
return item;
|
||||
});
|
||||
return dto;
|
||||
} catch (error: any) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("[getOrCreateIpAddressLookup] Error fetching IP data", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
import { Entity } from "@toolctrl/db/prisma-browser";
|
||||
import { RowWithDetails, updateRowMedia } from "~/utils/db/entities/rows.db.server";
|
||||
import { deleteFile, hasStorageCredentials, putBufferAsFile } from "~/utils/integrations/storageService";
|
||||
import { fetchOutboundHttpWithRedirects } from "~/utils/outboundHttp.server";
|
||||
|
||||
// Media sources come from client-supplied row JSON. A bare fetch() of a
|
||||
// caller-controlled URL is an SSRF vector (cloud metadata, internal services),
|
||||
// so only inline data: URIs are fetched directly; any remote URL is routed
|
||||
// through the SSRF-safe helper that validates the scheme and rejects
|
||||
// private/loopback/link-local/metadata addresses (incl. across redirects).
|
||||
async function fetchMediaSource(source: string): Promise<Blob> {
|
||||
const trimmed = (source ?? "").trim();
|
||||
if (trimmed.toLowerCase().startsWith("data:")) {
|
||||
return await (await fetch(trimmed)).blob();
|
||||
}
|
||||
const response = await fetchOutboundHttpWithRedirects(trimmed, {});
|
||||
return await response.blob();
|
||||
}
|
||||
|
||||
export async function storeRowMediaInStorageProvider(entity: Entity, row: RowWithDetails | null) {
|
||||
if (!row) {
|
||||
return;
|
||||
}
|
||||
const mediaRowValues = row.values.filter((f) => f.media && f.media.length > 0);
|
||||
return await Promise.all(
|
||||
mediaRowValues.map(async (mediaRowValue) => {
|
||||
if (mediaRowValue.media) {
|
||||
return await Promise.all(
|
||||
mediaRowValue.media
|
||||
.filter((f) => !f.publicUrl)
|
||||
.map(async (media) => {
|
||||
if (hasStorageCredentials()) {
|
||||
try {
|
||||
media.name = sanitizeFileName(media.name);
|
||||
const blob = await fetchMediaSource(media.publicUrl || media.file);
|
||||
const body = new Uint8Array(await blob.arrayBuffer());
|
||||
const key = media.id + "-" + media.name;
|
||||
const stored = await putBufferAsFile({ bucket: entity.name, key, body, contentType: blob.type || undefined });
|
||||
if (stored.publicUrl) {
|
||||
media.file = "";
|
||||
media.publicUrl = stored.publicUrl;
|
||||
media.storageBucket = entity.name;
|
||||
media.storageProvider = "s3";
|
||||
return await updateRowMedia({
|
||||
id: media.id,
|
||||
rowId: mediaRowValue.rowId,
|
||||
entityId: row.entityId,
|
||||
tenantId: row.tenantId,
|
||||
}, {
|
||||
file: "",
|
||||
publicUrl: stored.publicUrl,
|
||||
storageBucket: entity.name,
|
||||
storageProvider: "s3",
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("Could not store row media: " + e);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteRowMediaFromStorageProvider(row: RowWithDetails | null) {
|
||||
if (!row) {
|
||||
return;
|
||||
}
|
||||
const mediaRowValues = row.values.filter((f) => f.media && f.media.length > 0);
|
||||
return await Promise.all(
|
||||
mediaRowValues.map(async (mediaRowValue) => {
|
||||
if (mediaRowValue.media) {
|
||||
return await Promise.all(
|
||||
mediaRowValue.media.map(async (media) => {
|
||||
if (media.publicUrl && media.storageBucket && hasStorageCredentials()) {
|
||||
try {
|
||||
await deleteFile({ bucket: media.storageBucket, key: media.id + "-" + media.name });
|
||||
return await updateRowMedia({
|
||||
id: media.id,
|
||||
rowId: mediaRowValue.rowId,
|
||||
entityId: row.entityId,
|
||||
tenantId: row.tenantId,
|
||||
}, {
|
||||
file: "",
|
||||
publicUrl: "",
|
||||
storageBucket: "",
|
||||
storageProvider: "",
|
||||
});
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("Could not delete stored media: " + e);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// export async function getMediaFromZipFiles(zipFiles: MediaDto[]) {
|
||||
// let createdFiles: MediaDto[] = [];
|
||||
// await Promise.all(
|
||||
// zipFiles.map(async (item) => {
|
||||
// const filePath = "zip-uploads/" + item.name.replace(" ", "");
|
||||
// deleteFile(filePath);
|
||||
// const path = await saveZip(filePath, { type: item.type, content: item.file.split(",")[1] });
|
||||
// if (path) {
|
||||
// const folderPath = path + "-unzip";
|
||||
// deleteFolder(folderPath, { recursive: true, force: true });
|
||||
// const entries = await extractZip(path);
|
||||
// entries.forEach((entry) => {
|
||||
// if (entry.name.startsWith("._")) {
|
||||
// return;
|
||||
// }
|
||||
// var file = entry.getData().toString("base64");
|
||||
// let type = "";
|
||||
// if (entry.name.toLowerCase().endsWith("xml")) {
|
||||
// type = "text/xml";
|
||||
// } else if (entry.name.toLowerCase().endsWith("pdf")) {
|
||||
// type = "application/pdf";
|
||||
// }
|
||||
// createdFiles.push({
|
||||
// title: entry.name,
|
||||
// name: entry.name,
|
||||
// file: `data:${type};base64,${file}`,
|
||||
// type,
|
||||
// });
|
||||
// });
|
||||
// deleteFile(path);
|
||||
// }
|
||||
// deleteFile(filePath);
|
||||
// })
|
||||
// );
|
||||
// return createdFiles;
|
||||
// }
|
||||
|
||||
function sanitizeFileName(fileName: string): string {
|
||||
// Replace spaces with underscores
|
||||
const sanitized = fileName
|
||||
.replace(/\s+/g, "_")
|
||||
// Remove or replace other invalid characters as needed, e.g., commas, semicolons, etc.
|
||||
.replace(/[^a-zA-Z0-9._-]/g, "");
|
||||
// Optionally, you could add more specific replacements for known bad characters
|
||||
// .replace(/specific-bad-character/g, 'replacement')
|
||||
|
||||
return sanitized;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildPermissionDeniedUrl } from "./PermissionsService";
|
||||
|
||||
describe("permission denied redirects", () => {
|
||||
it("keeps tenant scope when building tenant permission denial URLs", () => {
|
||||
expect(
|
||||
buildPermissionDeniedUrl({
|
||||
permissionName: "app.settings.members.view",
|
||||
tenantId: "tenant_1",
|
||||
requestUrl: "https://app.example.test/acme/settings/members?tab=active",
|
||||
})
|
||||
).toBe("/unauthorized/app.settings.members.view/tenant_1?redirect=%2Facme%2Fsettings%2Fmembers");
|
||||
});
|
||||
|
||||
it("keeps global denial URLs on the global unauthorized route", () => {
|
||||
expect(
|
||||
buildPermissionDeniedUrl({
|
||||
permissionName: "admin.accounts.view",
|
||||
tenantId: null,
|
||||
requestUrl: "https://admin.example.test/accounts/users",
|
||||
})
|
||||
).toBe("/unauthorized/admin.accounts.view?redirect=%2Faccounts%2Fusers");
|
||||
});
|
||||
});
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("~/utils/db/permissions/userRoles.db.server", () => ({
|
||||
findPermissionByUser: vi.fn(),
|
||||
findUserRolesByIds: vi.fn(),
|
||||
getUserRoleInAdmin: vi.fn(),
|
||||
getUserRoles: vi.fn(),
|
||||
userHasPermission: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/permissions/permissions.db.server", () => ({
|
||||
getPermissionName: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/permissions/groups.db.server", () => ({
|
||||
getMyGroups: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/tenants.db.server", () => ({
|
||||
getTenantMember: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
getUserInfo: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/loaders.middleware", () => ({
|
||||
getIsSuperAdminByUserId: vi.fn(),
|
||||
}));
|
||||
|
||||
import { TenantUserStatus } from "~/application/enums/tenants/TenantUserStatus";
|
||||
import { getPermissionName } from "~/utils/db/permissions/permissions.db.server";
|
||||
import { userHasPermission } from "~/utils/db/permissions/userRoles.db.server";
|
||||
import { getTenantMember } from "~/utils/db/tenants.db.server";
|
||||
import { getIsSuperAdminByUserId } from "~/utils/loaders.middleware";
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import {
|
||||
buildPermissionDeniedUrl,
|
||||
shapePermissionCheckResult,
|
||||
verifyUserHasPermission,
|
||||
type PermissionCheckResult,
|
||||
} from "./PermissionsService";
|
||||
|
||||
const mocks = {
|
||||
getPermissionName: vi.mocked(getPermissionName),
|
||||
getTenantMember: vi.mocked(getTenantMember),
|
||||
getIsSuperAdminByUserId: vi.mocked(getIsSuperAdminByUserId),
|
||||
getUserInfo: vi.mocked(getUserInfo),
|
||||
userHasPermission: vi.mocked(userHasPermission),
|
||||
};
|
||||
|
||||
const permissionName = "app.dashboard.view";
|
||||
|
||||
async function expectRedirect(error: unknown) {
|
||||
expect(error).toBeInstanceOf(Response);
|
||||
const response = error as Response;
|
||||
expect(response.status).toBe(302);
|
||||
return response.headers.get("Location");
|
||||
}
|
||||
|
||||
describe("verifyUserHasPermission", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.getUserInfo.mockResolvedValue({ userId: "user_123" });
|
||||
mocks.getPermissionName.mockResolvedValue({ id: "perm_123", name: permissionName, description: "Dashboard" });
|
||||
mocks.getTenantMember.mockResolvedValue({ id: "tenant_user_123", status: TenantUserStatus.ACTIVE } as Awaited<ReturnType<typeof getTenantMember>>);
|
||||
mocks.getIsSuperAdminByUserId.mockResolvedValue(false);
|
||||
mocks.userHasPermission.mockResolvedValue(true);
|
||||
});
|
||||
|
||||
it("allows users with a known granted permission", async () => {
|
||||
await expect(verifyUserHasPermission(new Request("https://app.example.com/dashboard"), permissionName, "tenant_123")).resolves.toBe(true);
|
||||
|
||||
expect(mocks.getUserInfo).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).toHaveBeenCalledWith(permissionName);
|
||||
expect(mocks.getTenantMember).toHaveBeenCalledWith("user_123", "tenant_123");
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledWith("user_123", "tenant_123", permissionName);
|
||||
});
|
||||
|
||||
it("fails closed for unknown permissions without counting user permissions", async () => {
|
||||
mocks.getPermissionName.mockResolvedValue(null);
|
||||
|
||||
await expect(verifyUserHasPermission(new Request("https://app.example.com/legacy"), permissionName, "tenant_123")).rejects.toThrow(
|
||||
`Unknown permission: ${permissionName}`
|
||||
);
|
||||
|
||||
expect(mocks.getUserInfo).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getTenantMember).not.toHaveBeenCalled();
|
||||
expect(mocks.userHasPermission).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("denies tenant permissions when the user no longer has active tenant membership", async () => {
|
||||
mocks.getTenantMember.mockResolvedValue(null);
|
||||
|
||||
let location: string | null = null;
|
||||
try {
|
||||
await verifyUserHasPermission(new Request("https://app.example.com/acme/settings?token=secret"), permissionName, "tenant_123");
|
||||
} catch (error) {
|
||||
location = await expectRedirect(error);
|
||||
}
|
||||
|
||||
expect(location).toBe(`/unauthorized/${permissionName}/tenant_123?redirect=%2Facme%2Fsettings`);
|
||||
expect(mocks.getTenantMember).toHaveBeenCalledWith("user_123", "tenant_123");
|
||||
expect(mocks.userHasPermission).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("throws Unauthorized for unauthenticated users before permission lookup", async () => {
|
||||
mocks.getUserInfo.mockResolvedValue({ userId: null, cookies: [{ category: "analytics", allowed: true }] });
|
||||
|
||||
await expect(verifyUserHasPermission(new Request("https://app.example.com/dashboard?token=secret"), permissionName, "tenant_123")).rejects.toThrow(
|
||||
"Unauthorized"
|
||||
);
|
||||
|
||||
expect(mocks.getUserInfo).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).not.toHaveBeenCalled();
|
||||
expect(mocks.userHasPermission).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redirects denied tenant permissions with a redacted pathname-only redirect", async () => {
|
||||
mocks.userHasPermission.mockResolvedValue(false);
|
||||
|
||||
let location: string | null = null;
|
||||
try {
|
||||
await verifyUserHasPermission(new Request("https://app.example.com/settings?token=secret&apiKey=key_123"), permissionName, "tenant_123");
|
||||
} catch (error) {
|
||||
location = await expectRedirect(error);
|
||||
}
|
||||
|
||||
expect(location).toBe(`/unauthorized/${permissionName}/tenant_123?redirect=%2Fsettings`);
|
||||
expect(location).not.toContain("token=secret");
|
||||
expect(location).not.toContain("apiKey=key_123");
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("redirects denied admin permissions without tenant scope", async () => {
|
||||
mocks.userHasPermission.mockResolvedValue(false);
|
||||
|
||||
let location: string | null = null;
|
||||
try {
|
||||
await verifyUserHasPermission(new Request("https://admin.example.com/users?jwt=secret"), "admin.users.view", null);
|
||||
} catch (error) {
|
||||
location = await expectRedirect(error);
|
||||
}
|
||||
|
||||
expect(location).toBe("/unauthorized/admin.users.view?redirect=%2Fusers");
|
||||
expect(location).not.toContain("jwt=secret");
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledWith("user_123", null, "admin.users.view");
|
||||
});
|
||||
});
|
||||
|
||||
describe("permission decision helpers", () => {
|
||||
it("builds denied URLs from pathname only", () => {
|
||||
expect(
|
||||
buildPermissionDeniedUrl({ permissionName, tenantId: "tenant_123", requestUrl: "https://app.example.com/accounts?bearer=secret" })
|
||||
).toBe(`/unauthorized/${permissionName}/tenant_123?redirect=%2Faccounts`);
|
||||
});
|
||||
|
||||
it("shapes redacted permission check results", () => {
|
||||
const denied: PermissionCheckResult = shapePermissionCheckResult({
|
||||
allowed: false,
|
||||
permissionName,
|
||||
tenantId: "tenant_123",
|
||||
reason: "permission-denied",
|
||||
});
|
||||
|
||||
expect(denied).toEqual({ allowed: false, permissionName, tenantId: "tenant_123", reason: "permission-denied" });
|
||||
expect(JSON.stringify(denied)).not.toContain("cookie");
|
||||
expect(JSON.stringify(denied)).not.toContain("secret");
|
||||
});
|
||||
});
|
||||
+253
@@ -0,0 +1,253 @@
|
||||
import { Prisma } from "@toolctrl/db/prisma-browser";
|
||||
import { redirect } from "react-router";
|
||||
import { RowPermissionsDto } from "~/application/dtos/entities/RowPermissionsDto";
|
||||
import { DefaultAdminRoles } from "~/application/dtos/shared/DefaultAdminRoles";
|
||||
import { DefaultPermission } from "~/application/dtos/shared/DefaultPermissions";
|
||||
import { TenantUserStatus } from "~/application/enums/tenants/TenantUserStatus";
|
||||
import { RowAccess, RowAccessTypes } from "~/application/enums/entities/RowAccess";
|
||||
import { RowWithDetails } from "~/utils/db/entities/rows.db.server";
|
||||
import { getMyGroups } from "~/utils/db/permissions/groups.db.server";
|
||||
import { getPermissionName } from "~/utils/db/permissions/permissions.db.server";
|
||||
import { findPermissionByUser, findUserRolesByIds, getUserRoles, getUserRoleInAdmin, userHasPermission } from "~/utils/db/permissions/userRoles.db.server";
|
||||
import { getTenantMember } from "~/utils/db/tenants.db.server";
|
||||
import { getEntityPermission } from "~/utils/helpers/PermissionsHelper";
|
||||
import { getIsSuperAdminByUserId } from "~/utils/loaders.middleware";
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import { getAuthUserWithoutPasswordById as getUser } from "@toolctrl/db/auth-boundary.server";
|
||||
|
||||
const OPERATOR_ADMIN_BYPASS_DESCRIPTION = "Operator admin tenant permission bypass";
|
||||
|
||||
export async function getUserPermission({ userId, permissionName, tenantId }: { userId: string; permissionName: string; tenantId?: string | null }) {
|
||||
if (tenantId) {
|
||||
const user = await getUser(userId);
|
||||
const isOperatorAdmin = !!user?.admin || (await getIsSuperAdminByUserId(userId)) || !!(await getUserRoleInAdmin(userId, DefaultAdminRoles.SuperAdmin));
|
||||
if (isOperatorAdmin) {
|
||||
return {
|
||||
permission: { id: "operator-admin-bypass", name: permissionName, description: OPERATOR_ADMIN_BYPASS_DESCRIPTION },
|
||||
userPermission: permissionName,
|
||||
};
|
||||
}
|
||||
}
|
||||
const permission = await getPermissionName(permissionName);
|
||||
if (!permission) {
|
||||
return { permission, userPermission: undefined };
|
||||
}
|
||||
if (tenantId && (await getIsSuperAdminByUserId(userId))) {
|
||||
return { permission, userPermission: permission.name };
|
||||
}
|
||||
// const userRoles = await getUserRoles(userId ?? undefined, tenantId ?? null);
|
||||
// let userPermission: Permission | undefined = undefined;
|
||||
// userRoles.forEach((userRole) => {
|
||||
// userRole.role.permissions.forEach((rolePermission) => {
|
||||
// if (rolePermission.permission.name === permissionName) {
|
||||
// userPermission = rolePermission.permission;
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
const userPermission = await findPermissionByUser(permissionName, userId, tenantId);
|
||||
return { permission, userPermission };
|
||||
}
|
||||
|
||||
export type PermissionCheckDeniedReason = "unauthenticated" | "permission-denied";
|
||||
|
||||
export type PermissionCheckResult =
|
||||
| { allowed: true; permissionName: DefaultPermission; tenantId: string | null; reason?: undefined }
|
||||
| { allowed: false; permissionName: DefaultPermission; tenantId: string | null; reason: PermissionCheckDeniedReason };
|
||||
|
||||
export function buildPermissionDeniedUrl({
|
||||
permissionName,
|
||||
tenantId,
|
||||
requestUrl,
|
||||
}: {
|
||||
permissionName: DefaultPermission;
|
||||
tenantId: string | null;
|
||||
requestUrl: string;
|
||||
}) {
|
||||
const requestPathname = new URL(requestUrl).pathname;
|
||||
const searchParams = new URLSearchParams([["redirect", requestPathname]]);
|
||||
const tenantSegment = tenantId ? `/${encodeURIComponent(tenantId)}` : "";
|
||||
return `/unauthorized/${permissionName}${tenantSegment}?${searchParams}`;
|
||||
}
|
||||
|
||||
export function shapePermissionCheckResult({
|
||||
allowed,
|
||||
permissionName,
|
||||
tenantId,
|
||||
reason,
|
||||
}: {
|
||||
allowed: boolean;
|
||||
permissionName: DefaultPermission;
|
||||
tenantId: string | null;
|
||||
reason?: PermissionCheckDeniedReason;
|
||||
}): PermissionCheckResult {
|
||||
if (allowed) {
|
||||
return { allowed: true, permissionName, tenantId };
|
||||
}
|
||||
return { allowed: false, permissionName, tenantId, reason: reason ?? "permission-denied" };
|
||||
}
|
||||
|
||||
export type EntityPermissionOperation = "view" | "read" | "create" | "update" | "delete";
|
||||
|
||||
export async function verifyUserHasEntityPermission(
|
||||
request: Request,
|
||||
entity: { name: string },
|
||||
operation: EntityPermissionOperation,
|
||||
tenantId: string | null = null
|
||||
) {
|
||||
return verifyUserHasPermission(request, getEntityPermission(entity, operation), tenantId);
|
||||
}
|
||||
|
||||
export async function verifyUserHasPermission(request: Request, permissionName: DefaultPermission, tenantId: string | null = null) {
|
||||
// if (permissionName.startsWith("entity.")) {
|
||||
// return true;
|
||||
// }
|
||||
// console.log("verifyUserHasPermission", permissionName);
|
||||
const userInfo = await getUserInfo(request);
|
||||
if (!userInfo.userId) {
|
||||
shapePermissionCheckResult({ allowed: false, permissionName, tenantId, reason: "unauthenticated" });
|
||||
throw Error("Unauthorized");
|
||||
}
|
||||
if (tenantId) {
|
||||
const user = await getUser(userInfo.userId);
|
||||
const isOperatorAdmin =
|
||||
!!user?.admin || (await getIsSuperAdminByUserId(userInfo.userId)) || !!(await getUserRoleInAdmin(userInfo.userId, DefaultAdminRoles.SuperAdmin));
|
||||
if (isOperatorAdmin) {
|
||||
shapePermissionCheckResult({ allowed: true, permissionName, tenantId });
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const permission = await getPermissionName(permissionName);
|
||||
if (!permission) {
|
||||
shapePermissionCheckResult({ allowed: false, permissionName, tenantId, reason: "permission-denied" });
|
||||
throw Error(`Unknown permission: ${permissionName}`);
|
||||
}
|
||||
|
||||
if (tenantId) {
|
||||
const tenantMember = await getTenantMember(userInfo.userId, tenantId);
|
||||
if (!tenantMember || tenantMember.status !== TenantUserStatus.ACTIVE) {
|
||||
const decision = shapePermissionCheckResult({ allowed: false, permissionName, tenantId, reason: "permission-denied" });
|
||||
throw redirect(buildPermissionDeniedUrl({ permissionName: decision.permissionName, tenantId: decision.tenantId, requestUrl: request.url }));
|
||||
}
|
||||
}
|
||||
|
||||
const userPermission = await userHasPermission(userInfo.userId, tenantId, permissionName);
|
||||
if (!userPermission) {
|
||||
const decision = shapePermissionCheckResult({ allowed: false, permissionName, tenantId, reason: "permission-denied" });
|
||||
throw redirect(buildPermissionDeniedUrl({ permissionName: decision.permissionName, tenantId: decision.tenantId, requestUrl: request.url }));
|
||||
}
|
||||
shapePermissionCheckResult({ allowed: true, permissionName, tenantId });
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function getUserRowPermission(row: RowWithDetails, tenantId?: string | null, userId?: string): Promise<RowPermissionsDto> {
|
||||
const accessLevels: RowAccess[] = ["none"];
|
||||
|
||||
let isOwner = false;
|
||||
if (tenantId && userId && (await getIsSuperAdminByUserId(userId))) {
|
||||
isOwner = true;
|
||||
} else if (tenantId === null && userId) {
|
||||
const isSuperAdmin = await getUserRoleInAdmin(userId, DefaultAdminRoles.SuperAdmin);
|
||||
if (isSuperAdmin) {
|
||||
isOwner = true;
|
||||
}
|
||||
} else if (tenantId === undefined && userId === undefined) {
|
||||
accessLevels.push("view");
|
||||
} else {
|
||||
isOwner = !!userId && userId === row.createdByUserId;
|
||||
if (tenantId) {
|
||||
const existing = row.permissions.find((f) => f.tenantId === tenantId);
|
||||
if (existing) {
|
||||
accessLevels.push(existing.access as RowAccess);
|
||||
}
|
||||
} else if (userId) {
|
||||
if (row.createdByUserId === userId || row.createdByUserId === null) {
|
||||
accessLevels.push("delete");
|
||||
}
|
||||
const existing = row.permissions.find((f) => f.userId === userId);
|
||||
if (existing) {
|
||||
accessLevels.push(existing.access as RowAccess);
|
||||
}
|
||||
}
|
||||
if (tenantId !== undefined && userId) {
|
||||
const inRoles = row.permissions
|
||||
.filter((f) => f.roleId)
|
||||
.map((f) => f.roleId)
|
||||
.map((f) => f as string);
|
||||
if (inRoles.length > 0) {
|
||||
const userRoles = await findUserRolesByIds(userId, tenantId, inRoles);
|
||||
userRoles.forEach((userRole) => {
|
||||
const existing = row.permissions.find((f) => f.roleId === userRole.roleId);
|
||||
if (existing) {
|
||||
accessLevels.push(existing.access as RowAccess);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const inGroups = row.permissions
|
||||
.filter((f) => f.groupId)
|
||||
.map((f) => f.groupId)
|
||||
.map((f) => f as string);
|
||||
if (inGroups.length > 0) {
|
||||
const userGroups = await getMyGroups(userId, tenantId);
|
||||
userGroups.forEach((userGroup) => {
|
||||
const existing = row.permissions.find((f) => f.groupId === userGroup.id);
|
||||
if (existing) {
|
||||
accessLevels.push(existing.access as RowAccess);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let access: RowAccess | undefined = undefined;
|
||||
for (let idx = RowAccessTypes.length - 1; idx >= 0; idx--) {
|
||||
const accessType = RowAccessTypes[idx];
|
||||
if (accessLevels.includes(accessType)) {
|
||||
access = accessType;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const rowPermissions: RowPermissionsDto = {
|
||||
canRead: isOwner || (access && access !== "none"),
|
||||
canComment: isOwner || access === "comment" || access === "edit" || access === "delete",
|
||||
canUpdate: isOwner || access === "edit" || access === "delete",
|
||||
canDelete: isOwner || access === "delete",
|
||||
isOwner,
|
||||
};
|
||||
return rowPermissions;
|
||||
}
|
||||
|
||||
export type RowPermissionsFilter = Prisma.RowWhereInput | {};
|
||||
export async function getRowPermissionsCondition({ tenantId, userId }: { tenantId?: string | null; userId?: string }) {
|
||||
if (tenantId && userId && (await getIsSuperAdminByUserId(userId))) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const OR_CONDITIONS: Prisma.RowWhereInput[] = [];
|
||||
const readableAccess = { not: "none" };
|
||||
if (tenantId) {
|
||||
OR_CONDITIONS.push(...[{ permissions: { some: { tenantId, access: readableAccess } } }]);
|
||||
if (userId) {
|
||||
OR_CONDITIONS.push(...[{ createdByUserId: userId }, { permissions: { some: { userId, access: readableAccess } } }]);
|
||||
}
|
||||
} else {
|
||||
if (userId) {
|
||||
OR_CONDITIONS.push(...[{ createdByUserId: null }, { createdByUserId: userId }, { permissions: { some: { userId, access: readableAccess } } }]);
|
||||
}
|
||||
}
|
||||
if (tenantId !== undefined && userId) {
|
||||
const userRoles = await getUserRoles(userId, tenantId);
|
||||
userRoles.forEach((userRole) => {
|
||||
OR_CONDITIONS.push(...[{ permissions: { some: { roleId: userRole.roleId, access: readableAccess } } }]);
|
||||
});
|
||||
|
||||
const userGroups = await getMyGroups(userId, tenantId);
|
||||
userGroups.forEach((userGroup) => {
|
||||
OR_CONDITIONS.push(...[{ permissions: { some: { groupId: userGroup.id, access: readableAccess } } }]);
|
||||
});
|
||||
}
|
||||
return {
|
||||
OR: OR_CONDITIONS,
|
||||
};
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
import { Prisma } from "@toolctrl/db/prisma-browser";
|
||||
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
||||
import { RowFiltersDto } from "~/application/dtos/data/RowFiltersDto";
|
||||
import { SortedByDto } from "~/application/dtos/data/SortedByDto";
|
||||
import { RowWithDetails, getRows, countRows } from "~/utils/db/entities/rows.db.server";
|
||||
|
||||
export async function getRowsWithPagination({
|
||||
pageSize,
|
||||
page,
|
||||
entityId,
|
||||
entityName,
|
||||
tenantId,
|
||||
portalId,
|
||||
userId,
|
||||
sortedBy,
|
||||
filters,
|
||||
rowWhere,
|
||||
includePublic,
|
||||
orderBy,
|
||||
}: {
|
||||
pageSize: number;
|
||||
page: number;
|
||||
entityId?: string;
|
||||
entityName?: string;
|
||||
tenantId?: string | null;
|
||||
portalId?: string | null;
|
||||
userId?: string | undefined;
|
||||
sortedBy?: SortedByDto[];
|
||||
filters?: RowFiltersDto;
|
||||
rowWhere?: Prisma.RowWhereInput;
|
||||
includePublic?: boolean;
|
||||
orderBy?: Prisma.RowOrderByWithRelationInput[];
|
||||
}): Promise<{
|
||||
items: RowWithDetails[];
|
||||
pagination: PaginationDto;
|
||||
}> {
|
||||
if (!orderBy) {
|
||||
orderBy = [{ order: "desc" }, { createdAt: "desc" }];
|
||||
}
|
||||
|
||||
let skip: number | undefined = pageSize * (page - 1);
|
||||
let take: number | undefined = pageSize;
|
||||
if (pageSize === -1) {
|
||||
skip = undefined;
|
||||
take = undefined;
|
||||
}
|
||||
const items = await getRows({ entityId, entityName, tenantId, userId, take, skip, orderBy, filters, rowWhere, includePublic });
|
||||
const totalItems = await countRows({ entityId, entityName, tenantId, userId, filters, rowWhere, includePublic });
|
||||
let totalPages = Math.ceil(totalItems / pageSize);
|
||||
if (pageSize === -1) {
|
||||
totalPages = 1;
|
||||
}
|
||||
|
||||
return {
|
||||
items,
|
||||
pagination: {
|
||||
totalItems,
|
||||
totalPages,
|
||||
page,
|
||||
pageSize,
|
||||
sortedBy,
|
||||
query: undefined,
|
||||
},
|
||||
};
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
import { RowMedia, Entity, Prisma, RowValue, RowValueMultiple, RowValueRange } from "@toolctrl/db/prisma-browser";
|
||||
import { RowsApi } from "../../api/.server/RowsApi";
|
||||
import { db } from "../../db.server";
|
||||
import { PropertyWithDetails } from "../../db/entities/entities.db.server";
|
||||
import { RowValueWithDetails } from "../../db/entities/rows.db.server";
|
||||
import { RowValueDto } from "~/application/dtos/entities/RowValueDto";
|
||||
import { RowValueMultipleDto } from "~/application/dtos/entities/RowValueMultipleDto";
|
||||
import { RowValueRangeDto } from "~/application/dtos/entities/RowValueRangeDto";
|
||||
|
||||
export type RowValueUpdateDto = {
|
||||
name: string;
|
||||
textValue?: string | undefined;
|
||||
numberValue?: number | undefined;
|
||||
dateValue?: Date | undefined;
|
||||
booleanValue?: boolean | undefined;
|
||||
media?: RowMedia[];
|
||||
multiple?: RowValueMultipleDto[];
|
||||
range?: RowValueRangeDto | undefined;
|
||||
};
|
||||
async function update({
|
||||
entity,
|
||||
row,
|
||||
values,
|
||||
rowUpdateInput,
|
||||
session,
|
||||
checkPermissions = true,
|
||||
options,
|
||||
}: {
|
||||
entity: Entity & { properties: PropertyWithDetails[] };
|
||||
row: { id: string; entityId: string; tenantId: string | null; values: RowValueWithDetails[] };
|
||||
values?: RowValueUpdateDto[];
|
||||
rowUpdateInput?: Prisma.RowUpdateInput;
|
||||
session: { tenantId: string | null; userId?: string } | undefined;
|
||||
checkPermissions?: boolean;
|
||||
options?: {
|
||||
createLog?: boolean;
|
||||
createEvent?: boolean;
|
||||
};
|
||||
}) {
|
||||
const dynamicProperties: RowValueDto[] = [];
|
||||
await Promise.all(
|
||||
(values ?? []).map(async (value) => {
|
||||
const property = entity.properties.find((i) => i.name === value.name);
|
||||
if (property) {
|
||||
let existingProperty: RowValue | null = row.values.find((f) => f.propertyId === property?.id) ?? null;
|
||||
if (!existingProperty) {
|
||||
existingProperty = await db.rowValue.findFirstOrThrow({ where: { propertyId: property.id, rowId: row.id } }).catch(() => {
|
||||
return null;
|
||||
});
|
||||
}
|
||||
if (!existingProperty) {
|
||||
existingProperty = await db.rowValue.create({
|
||||
data: {
|
||||
propertyId: property.id,
|
||||
rowId: row.id,
|
||||
textValue: value.textValue,
|
||||
numberValue: value.numberValue,
|
||||
dateValue: value.dateValue,
|
||||
},
|
||||
});
|
||||
}
|
||||
dynamicProperties.push({
|
||||
id: existingProperty.id,
|
||||
property,
|
||||
propertyId: property.id,
|
||||
textValue: value.textValue,
|
||||
numberValue: value.numberValue,
|
||||
dateValue: value.dateValue,
|
||||
booleanValue: value.booleanValue,
|
||||
media: value.media,
|
||||
multiple: value.multiple as RowValueMultiple[],
|
||||
range: value.range as RowValueRange,
|
||||
});
|
||||
if (value.textValue !== undefined) {
|
||||
existingProperty.textValue = value.textValue;
|
||||
}
|
||||
if (value.numberValue !== undefined) {
|
||||
existingProperty.numberValue = value.numberValue ? new Prisma.Decimal(value.numberValue) : null;
|
||||
}
|
||||
if (value.dateValue !== undefined) {
|
||||
existingProperty.dateValue = value.dateValue;
|
||||
}
|
||||
if (value.booleanValue !== undefined) {
|
||||
existingProperty.booleanValue = value.booleanValue;
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
return await RowsApi.update(row.id, {
|
||||
entity,
|
||||
tenantId: row.tenantId,
|
||||
rowValues: {
|
||||
dynamicProperties,
|
||||
},
|
||||
rowUpdateInput,
|
||||
userId: session?.userId,
|
||||
checkPermissions,
|
||||
options,
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
update,
|
||||
};
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
import { TFunction } from "i18next";
|
||||
import { EntityWithDetails } from "../../db/entities/entities.db.server";
|
||||
import { RowWithDetails, getRowById } from "../../db/entities/rows.db.server";
|
||||
import ApiHelper, { ApiFormatOptions } from "../ApiHelper";
|
||||
import RowHelper from "../RowHelper";
|
||||
import { getTenant } from "../../db/tenants.db.server";
|
||||
import { RowAsJson } from "../TemplateApiHelper";
|
||||
|
||||
async function fetchRowWithRelationships({
|
||||
rowId,
|
||||
scope,
|
||||
entities,
|
||||
processedRows = new Set<string>(),
|
||||
backlog = [], // This will be used to store the deferred relationships to be fetched later.
|
||||
}: {
|
||||
rowId: string;
|
||||
scope: { entityId?: string; tenantId: string | null };
|
||||
entities: EntityWithDetails[];
|
||||
processedRows?: Set<string>;
|
||||
backlog?: { rowId: string; relation: "parent" | "child"; attachTo: RowWithDetails; scope: { entityId?: string; tenantId: string | null } }[];
|
||||
}): Promise<RowWithDetails | null> {
|
||||
if (processedRows.has(rowId)) {
|
||||
return null;
|
||||
}
|
||||
processedRows.add(rowId);
|
||||
|
||||
const row = await getRowById(rowId, scope);
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Fetch parent relationships.
|
||||
for (const parentRow of row.parentRows) {
|
||||
backlog.push({ rowId: parentRow.parent.id, relation: "parent", attachTo: row, scope: { entityId: parentRow.parent.entityId, tenantId: parentRow.parent.tenantId } });
|
||||
}
|
||||
|
||||
// Fetch child relationships.
|
||||
for (const childRow of row.childRows) {
|
||||
backlog.push({ rowId: childRow.child.id, relation: "child", attachTo: row, scope: { entityId: childRow.child.entityId, tenantId: childRow.child.tenantId } });
|
||||
}
|
||||
|
||||
// Process the backlog.
|
||||
while (backlog.length > 0) {
|
||||
const { rowId: backlogRowId, relation, attachTo, scope: backlogScope } = backlog.shift()!;
|
||||
if (relation === "parent") {
|
||||
const parent = await fetchRowWithRelationships({ rowId: backlogRowId, scope: backlogScope, entities, processedRows, backlog });
|
||||
if (parent) {
|
||||
const parentRow = attachTo.parentRows.find((p) => p.parent.id === backlogRowId);
|
||||
if (parentRow) {
|
||||
parentRow.parent = parent;
|
||||
}
|
||||
}
|
||||
} else if (relation === "child") {
|
||||
const child = await fetchRowWithRelationships({ rowId: backlogRowId, scope: backlogScope, entities, processedRows, backlog });
|
||||
if (child) {
|
||||
const childRow = attachTo.childRows.find((c) => c.child.id === backlogRowId);
|
||||
if (childRow) {
|
||||
childRow.child = child;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
async function getRowInApiFormatWithRecursiveRelationships({
|
||||
entities,
|
||||
rowId,
|
||||
scope,
|
||||
t,
|
||||
options,
|
||||
}: {
|
||||
entities: EntityWithDetails[];
|
||||
rowId: string;
|
||||
scope: { entityId?: string; tenantId: string | null };
|
||||
t: TFunction;
|
||||
options: ApiFormatOptions;
|
||||
}): Promise<RowAsJson | null> {
|
||||
const row = await fetchRowWithRelationships({ rowId, scope, entities });
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
pruneDuplicateRows(row);
|
||||
const entity = entities.find((entity) => entity.id === row.entityId);
|
||||
if (!entity) {
|
||||
throw new Error(`Entity ${row.entityId} not found.`);
|
||||
}
|
||||
|
||||
const apiFormat = ApiHelper.getApiFormatWithRelationships({ entities, item: row, options });
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
name: RowHelper.getTextDescription({ entity, item: row, t }),
|
||||
entityId: entity.id,
|
||||
tenant: row.tenantId ? await getTenant(row.tenantId) : null,
|
||||
data: {
|
||||
[entity.name]: apiFormat,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function pruneDuplicateRows(mainRow: RowWithDetails): void {
|
||||
const seenRowIds: Set<string> = new Set();
|
||||
|
||||
let currentLevelRows: RowWithDetails[] = [mainRow];
|
||||
while (currentLevelRows.length > 0) {
|
||||
const nextLevelRows: RowWithDetails[] = [];
|
||||
|
||||
for (const row of currentLevelRows) {
|
||||
if (!row) continue;
|
||||
|
||||
// Process parent rows
|
||||
if (row.parentRows) {
|
||||
for (let i = row.parentRows.length - 1; i >= 0; i--) {
|
||||
const parent = row.parentRows[i].parent;
|
||||
if (seenRowIds.has(parent.id)) {
|
||||
row.parentRows.splice(i, 1); // Remove this parent
|
||||
} else {
|
||||
nextLevelRows.push(parent as RowWithDetails);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process child rows
|
||||
if (row.childRows) {
|
||||
for (let i = row.childRows.length - 1; i >= 0; i--) {
|
||||
const child = row.childRows[i].child;
|
||||
if (seenRowIds.has(child.id)) {
|
||||
row.childRows.splice(i, 1); // Remove this child
|
||||
} else {
|
||||
nextLevelRows.push(child as RowWithDetails);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark the row as seen
|
||||
seenRowIds.add(row.id);
|
||||
}
|
||||
|
||||
// Move to the next level
|
||||
currentLevelRows = nextLevelRows;
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
getRowInApiFormatWithRecursiveRelationships,
|
||||
};
|
||||
Reference in new issue
Block a user