mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 12:47:13 +01:00
Updated roles and permissions logic.
This commit is contained in:
parent
4eb3140e43
commit
b57ecf33e6
63 changed files with 432 additions and 546 deletions
|
|
@ -1,14 +1,8 @@
|
|||
import { Prisma, Team } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createTeam(
|
||||
data: Prisma.TeamCreateInput,
|
||||
searchDeleted = false,
|
||||
): Promise<Team> {
|
||||
return prisma.client.team.create({
|
||||
data: { ...data, isDeleted: searchDeleted ? null : false },
|
||||
});
|
||||
}
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { Website } from 'lib/types';
|
||||
|
||||
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
|
||||
return prisma.client.team.findFirst({
|
||||
|
|
@ -22,19 +16,35 @@ export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getTeamsByUserId(userId: string): Promise<Team[]> {
|
||||
return prisma.client.teamUser
|
||||
.findMany({
|
||||
where: {
|
||||
export async function getTeamWebsites(teamId: string): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function createTeam(data: Prisma.TeamCreateInput): Promise<Team> {
|
||||
const { id, userId } = data;
|
||||
|
||||
return prisma.transaction([
|
||||
prisma.client.team.create({
|
||||
data,
|
||||
}),
|
||||
prisma.client.teamUser.create({
|
||||
data: {
|
||||
id: uuid(),
|
||||
teamId: id,
|
||||
userId,
|
||||
role: ROLES.teamOwner,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
},
|
||||
})
|
||||
.then(data => {
|
||||
return data.map(a => a.team);
|
||||
});
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
export async function updateTeam(
|
||||
|
|
@ -42,7 +52,10 @@ export async function updateTeam(
|
|||
where: Prisma.TeamWhereUniqueInput,
|
||||
): Promise<Team> {
|
||||
return prisma.client.team.update({
|
||||
data,
|
||||
data: {
|
||||
...data,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
@ -50,7 +63,7 @@ export async function updateTeam(
|
|||
export async function deleteTeam(teamId: string): Promise<Team> {
|
||||
return prisma.client.team.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: teamId,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,23 @@ import { Prisma, TeamUser } from '@prisma/client';
|
|||
import { uuid } from 'lib/crypto';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getTeamUser(teamId: string, userId: string): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where: {
|
||||
teamId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamUsers(teamId: string): Promise<TeamUser[]> {
|
||||
return prisma.client.teamUser.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createTeamUser(
|
||||
userId: string,
|
||||
teamId: string,
|
||||
|
|
@ -17,18 +34,6 @@ export async function createTeamUser(
|
|||
});
|
||||
}
|
||||
|
||||
export async function getTeamUser(where: Prisma.TeamUserWhereInput): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamUsers(where: Prisma.TeamUserWhereInput): Promise<TeamUser[]> {
|
||||
return prisma.client.teamUser.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateTeamUser(
|
||||
data: Prisma.TeamUserUpdateInput,
|
||||
where: Prisma.TeamUserWhereUniqueInput,
|
||||
|
|
@ -42,7 +47,7 @@ export async function updateTeamUser(
|
|||
export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: teamUserId,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { Prisma } from '@prisma/client';
|
||||
import { Prisma, Team } from '@prisma/client';
|
||||
import cache from 'lib/cache';
|
||||
import prisma from 'lib/prisma';
|
||||
import { Website } from 'lib/types';
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
|
|
@ -9,36 +10,19 @@ export interface User {
|
|||
createdAt?: Date;
|
||||
}
|
||||
|
||||
export async function createUser(data: {
|
||||
id: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<{
|
||||
id: string;
|
||||
username: string;
|
||||
}> {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUser(
|
||||
where: Prisma.UserWhereUniqueInput,
|
||||
includePassword = false,
|
||||
options: { includePassword?: boolean } = {},
|
||||
): Promise<User> {
|
||||
const { includePassword = false } = options;
|
||||
|
||||
return prisma.client.user.findUnique({
|
||||
where,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
userRole: {
|
||||
select: { role: true },
|
||||
},
|
||||
password: includePassword,
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -53,24 +37,56 @@ export async function getUsers(): Promise<User[]> {
|
|||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
role: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUsersByTeamId(teamId): Promise<User[]> {
|
||||
return prisma.client.user.findMany({
|
||||
where: {
|
||||
teamUser: {
|
||||
every: {
|
||||
teamId,
|
||||
},
|
||||
export async function getUserTeams(userId: string): Promise<Team[]> {
|
||||
return prisma.client.teamUser
|
||||
.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
},
|
||||
})
|
||||
.then(data => {
|
||||
return data.map(a => a.team);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserWebsites(userId: string): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function createUser(data: {
|
||||
id: string;
|
||||
username: string;
|
||||
password: string;
|
||||
role: string;
|
||||
}): Promise<{
|
||||
id: string;
|
||||
username: string;
|
||||
role: string;
|
||||
}> {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
createdAt: true,
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -85,8 +101,8 @@ export async function updateUser(
|
|||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
role: true,
|
||||
createdAt: true,
|
||||
userRole: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -106,8 +122,8 @@ export async function deleteUser(
|
|||
websiteIds = websites.map(a => a.id);
|
||||
}
|
||||
|
||||
return client
|
||||
.$transaction([
|
||||
return prisma
|
||||
.transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId: { in: websiteIds } },
|
||||
}),
|
||||
|
|
@ -116,13 +132,13 @@ export async function deleteUser(
|
|||
}),
|
||||
client.website.updateMany({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: { id: { in: websiteIds } },
|
||||
}),
|
||||
client.user.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: userId,
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
import { Prisma, UserRole } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUserRole(
|
||||
data: Prisma.UserRoleCreateInput | Prisma.UserRoleUncheckedCreateInput,
|
||||
): Promise<UserRole> {
|
||||
return prisma.client.userRole.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserRole(where: Prisma.UserRoleWhereInput): Promise<UserRole> {
|
||||
return prisma.client.userRole.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserRoles(where: Prisma.UserRoleWhereInput): Promise<UserRole[]> {
|
||||
return prisma.client.userRole.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserRolesByUserId(userId: string): Promise<UserRole[]> {
|
||||
return prisma.client.userRole.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateUserRole(
|
||||
data: Prisma.UserRoleUpdateInput,
|
||||
where: Prisma.UserRoleWhereUniqueInput,
|
||||
): Promise<UserRole> {
|
||||
return prisma.client.userRole.update({
|
||||
data,
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteUserRole(userRoleId: string): Promise<UserRole> {
|
||||
return prisma.client.userRole.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
},
|
||||
where: {
|
||||
id: userRoleId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -3,6 +3,20 @@ import cache from 'lib/cache';
|
|||
import prisma from 'lib/prisma';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsites(): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createWebsite(
|
||||
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
|
||||
): Promise<Website> {
|
||||
|
|
@ -55,44 +69,6 @@ export async function resetWebsite(
|
|||
});
|
||||
}
|
||||
|
||||
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsitesByUserId(userId): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsitesByTeamId(teamId): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllWebsites(): Promise<Website[]> {
|
||||
return await prisma.client.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteWebsite(websiteId: string) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),
|
||||
|
|
@ -127,7 +103,7 @@ async function deleteWebsiteRelationalQuery(
|
|||
async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> {
|
||||
return prisma.client.website.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: { id: websiteId },
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ import clickhouse from 'lib/clickhouse';
|
|||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { WebsiteMetric } from 'interface/api/models';
|
||||
import { EventType } from 'lib/types';
|
||||
import { WebsiteMetric } from 'lib/types';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
|
||||
export async function getEventData(
|
||||
...args: [
|
||||
|
|
@ -47,7 +47,7 @@ async function relationalQuery(
|
|||
from website_event
|
||||
where website_id ='${websiteId}'
|
||||
and created_at between $1 and $2
|
||||
and event_type = ${EventType.Event}
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
${eventName ? `and eventName = ${eventName}` : ''}
|
||||
${
|
||||
Object.keys(filters).length > 0
|
||||
|
|
@ -80,7 +80,7 @@ async function clickhouseQuery(
|
|||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${EventType.Event}
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
${eventName ? `and eventName = ${eventName}` : ''}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ import prisma from 'lib/prisma';
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
import { WebsiteEventMetric } from 'interface/api/models';
|
||||
import { EventType } from 'lib/types';
|
||||
import { WebsiteEventMetric } from 'lib/types';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
|
||||
export async function getEventMetrics(
|
||||
...args: [
|
||||
|
|
@ -56,7 +56,7 @@ async function relationalQuery(
|
|||
from website_event
|
||||
where website_id='${websiteId}'
|
||||
and created_at between $1 and $2
|
||||
and event_type = ${EventType.Event}
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
${getFilterQuery(filters, params)}
|
||||
group by 1, 2
|
||||
order by 2`,
|
||||
|
|
@ -95,7 +95,7 @@ async function clickhouseQuery(
|
|||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${EventType.Event}
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${getFilterQuery(filters, params)}
|
||||
group by x, t
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
import { EVENT_NAME_LENGTH, URL_LENGTH } from 'lib/constants';
|
||||
import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import cache from 'lib/cache';
|
||||
import { EventType } from 'lib/types';
|
||||
|
||||
export async function saveEvent(args: {
|
||||
id: string;
|
||||
|
|
@ -43,7 +42,7 @@ async function relationalQuery(data: {
|
|||
sessionId,
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
referrer: referrer?.substring(0, URL_LENGTH),
|
||||
eventType: EventType.Event,
|
||||
eventType: EVENT_TYPE.customEvent,
|
||||
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
|
||||
eventData,
|
||||
};
|
||||
|
|
@ -77,7 +76,7 @@ async function clickhouseQuery(data: {
|
|||
session_id: sessionId,
|
||||
event_id: uuid(),
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
event_type: EventType.Event,
|
||||
event_type: EVENT_TYPE.customEvent,
|
||||
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
|
||||
event_data: eventData ? JSON.stringify(eventData) : null,
|
||||
rev_id: website?.revId || 0,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import clickhouse from 'lib/clickhouse';
|
|||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { EventType } from 'lib/types';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
|
||||
export async function getPageviewMetrics(
|
||||
...args: [
|
||||
|
|
@ -43,7 +43,7 @@ async function relationalQuery(
|
|||
${joinSession}
|
||||
where website_id='${websiteId}'
|
||||
and website_event.created_at between $1 and $2
|
||||
and event_type = ${EventType.Pageview}
|
||||
and event_type = ${EVENT_TYPE.pageView}
|
||||
${filterQuery}
|
||||
group by 1
|
||||
order by 2 desc`,
|
||||
|
|
@ -71,9 +71,9 @@ async function clickhouseQuery(
|
|||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${EventType.Pageview}
|
||||
and event_type = ${EVENT_TYPE.pageView}
|
||||
${column !== 'event_name' ? `and event_name = ''` : `and event_name != ''`}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}n
|
||||
${filterQuery}
|
||||
group by x
|
||||
order by y desc`,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import cache from 'lib/cache';
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import { EventType } from 'lib/types';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
|
||||
export async function getPageviewStats(
|
||||
...args: [
|
||||
|
|
@ -56,7 +56,7 @@ async function relationalQuery(
|
|||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
and event_type = ${EventType.Pageview}
|
||||
and event_type = ${EVENT_TYPE.pageView}
|
||||
${filterQuery}
|
||||
group by 1`,
|
||||
params,
|
||||
|
|
@ -92,7 +92,7 @@ async function clickhouseQuery(
|
|||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${EventType.Pageview}
|
||||
and event_type = ${EVENT_TYPE.pageView}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${filterQuery}
|
||||
group by t) g
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
import { URL_LENGTH } from 'lib/constants';
|
||||
import { URL_LENGTH, EVENT_TYPE } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { EventType } from 'lib/types';
|
||||
|
||||
export async function savePageView(args: {
|
||||
id: string;
|
||||
|
|
@ -40,7 +39,7 @@ async function relationalQuery(data: {
|
|||
sessionId,
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
referrer: referrer?.substring(0, URL_LENGTH),
|
||||
eventType: EventType.Pageview,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -58,7 +57,7 @@ async function clickhouseQuery(data) {
|
|||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
country: country ? country : null,
|
||||
event_type: EventType.Pageview,
|
||||
event_type: EVENT_TYPE.pageView,
|
||||
...args,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
export * from './admin/team';
|
||||
export * from './admin/teamUser';
|
||||
export * from './admin/user';
|
||||
export * from './admin/userRole';
|
||||
export * from './admin/website';
|
||||
export * from './analytics/event/getEventMetrics';
|
||||
export * from './analytics/event/getEvents';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue