Skip to content

Commit

Permalink
Merge pull request #84 from sinamics/dev
Browse files Browse the repository at this point in the history
Merge dev branch
  • Loading branch information
sinamics authored Aug 12, 2023
2 parents 2bcdf02 + 5b2ed0e commit 4a4cbc7
Show file tree
Hide file tree
Showing 35 changed files with 881 additions and 395 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"recommendations": [
"prisma.prisma",
"rome.rome"
"rome.rome",
"bradlc.vscode-tailwindcss"
]
}
5 changes: 5 additions & 0 deletions init-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ echo "Applying migrations to the database..."
npx prisma migrate deploy
echo "Migrations applied successfully!"

# seed the database
echo "Seeding the database..."
npx prisma db seed
echo "Database seeded successfully!"

>&2 echo "Executing command"
exec $cmd
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@types/ejs": "^3.1.2",
"@types/jest": "^29.5.0",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^18.14.0",
"@types/node": "^18.17.5",
"@types/nodemailer": "^6.4.8",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
Expand All @@ -87,5 +87,8 @@
},
"ct3aMetadata": {
"initVersion": "7.8.0"
},
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}
}
34 changes: 34 additions & 0 deletions prisma/migrations/20230811063619_user_options/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Warnings:
- You are about to drop the column `showNotationMarkerInTableRow` on the `GlobalOptions` table. All the data in the column will be lost.
- You are about to drop the column `useNotationColorAsBg` on the `GlobalOptions` table. All the data in the column will be lost.
- You are about to drop the column `ztCentralApiKey` on the `GlobalOptions` table. All the data in the column will be lost.
- You are about to drop the column `ztCentralApiUrl` on the `GlobalOptions` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "GlobalOptions" DROP COLUMN "showNotationMarkerInTableRow",
DROP COLUMN "useNotationColorAsBg",
DROP COLUMN "ztCentralApiKey",
DROP COLUMN "ztCentralApiUrl";

-- CreateTable
CREATE TABLE "UserOptions" (
"id" SERIAL NOT NULL,
"userId" INTEGER NOT NULL,
"useNotationColorAsBg" BOOLEAN DEFAULT false,
"showNotationMarkerInTableRow" BOOLEAN DEFAULT true,
"ztCentralApiKey" TEXT DEFAULT '',
"ztCentralApiUrl" TEXT DEFAULT 'https://api.zerotier.com/api/v1',
"localControllerUrl" TEXT DEFAULT 'http://zerotier:9993',
"localControllerSecret" TEXT DEFAULT '',

CONSTRAINT "UserOptions_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "UserOptions_userId_key" ON "UserOptions"("userId");

-- AddForeignKey
ALTER TABLE "UserOptions" ADD CONSTRAINT "UserOptions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
64 changes: 38 additions & 26 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ model GlobalOptions {
// Notifications
userRegistrationNotification Boolean @default(false)
// networks
useNotationColorAsBg Boolean? @default(false)
showNotationMarkerInTableRow Boolean? @default(true)
//zt central
ztCentralApiKey String? @default("")
ztCentralApiUrl String? @default("https://api.zerotier.com/api")
}

enum Role {
Expand Down Expand Up @@ -133,25 +125,45 @@ model Session {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model UserOptions {
id Int @id @default(autoincrement())
userId Int @unique
user User @relation(fields: [userId], references: [id])
//networks
useNotationColorAsBg Boolean? @default(false)
showNotationMarkerInTableRow Boolean? @default(true)
//zt central
ztCentralApiKey String? @default("")
ztCentralApiUrl String? @default("https://api.zerotier.com/api/v1")
// local controller
localControllerUrl String? @default("http://zerotier:9993")
localControllerSecret String? @default("")
}

model User {
id Int @id @default(autoincrement())
name String
email String @unique
emailVerified DateTime?
lastLogin DateTime
lastseen DateTime?
expirationDate String @default("")
online Boolean? @default(false)
role Role @default(USER)
image String?
hash String
licenseStatus String?
orderStatus String?
orderId Int @default(0)
product_id Int? @default(0)
licenseKey String? @default("")
tempPassword String?
firstTime Boolean @default(true)
id Int @id @default(autoincrement())
name String
email String @unique
emailVerified DateTime?
lastLogin DateTime
lastseen DateTime?
expirationDate String @default("")
online Boolean? @default(false)
role Role @default(USER)
image String?
hash String
licenseStatus String?
orderStatus String?
orderId Int @default(0)
product_id Int? @default(0)
licenseKey String? @default("")
tempPassword String?
firstTime Boolean @default(true)
options UserOptions?
accounts Account[]
sessions Session[]
network network[]
Expand Down
19 changes: 19 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { seedUserOptions } from "./seeds/user-option.seed";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
await seedUserOptions();
// rome-ignore lint/nursery/noConsoleLog: <explanation>
console.log("Seeding User Options complete!");
}

main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
32 changes: 32 additions & 0 deletions prisma/seeds/user-option.seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

export async function seedUserOptions() {
// Fetch all users from the database
const users = await prisma.user.findMany();

for (const user of users) {
// Check if UserOptions exist for each user
const userOptionExists = await prisma.userOptions.findUnique({
where: { userId: user.id },
});
// If UserOptions do not exist for a user, create them
if (!userOptionExists) {
await prisma.userOptions.create({
data: {
userId: user.id,
useNotationColorAsBg: false,
showNotationMarkerInTableRow: true,
ztCentralApiKey: "",
ztCentralApiUrl: "https://api.zerotier.com/api/v1",
localControllerUrl: "http://zerotier:9993",
localControllerSecret: "",
},
});
}
}

// rome-ignore lint/nursery/noConsoleLog: <explanation>
console.log("Seeding User Options complete!");
}
27 changes: 23 additions & 4 deletions src/__tests__/__mocks__/networkById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ import "@testing-library/jest-dom";

jest.mock("../../utils/api", () => ({
api: {
auth: {
me: {
useQuery: () => ({
data: {
user: {
id: "1234567890",
email: "",
role: "ADMIN",
verified: true,
avatar: null,
createdAt: "2021-05-04T12:00:00.000Z",
updatedAt: "2021-05-04T12:00:00.000Z",
},
},
isLoading: false,
refetch: jest.fn(),
}),
},
},
admin: {
getAllOptions: {
useQuery: () => ({
Expand Down Expand Up @@ -53,7 +72,7 @@ jest.mock("../../utils/api", () => ({
useMutation: () => ({
mutate: jest.fn(),
}),
}
},
},
network: {
getNetworkById: {
Expand Down Expand Up @@ -88,8 +107,8 @@ jest.mock("../../utils/api", () => ({
},
],
v4AssignMode: {
zt: false, // Changed state to checked
},
zt: false, // Changed state to checked
},
},
members: [],
zombieMembers: [],
Expand Down Expand Up @@ -170,4 +189,4 @@ jest.mock("../../utils/api", () => ({
},
},
},
}));
}));
24 changes: 14 additions & 10 deletions src/components/layouts/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import { api } from "~/utils/api";
const Sidebar = (): JSX.Element => {
const { open, toggle } = useSidebarStore();
const { data: session } = useSession();
const { data: me } = api.auth.me.useQuery();
const t = useTranslations("sidebar");

const sidebarRef = useRef<HTMLDivElement>();
const router = useRouter();

const { data: globalOption } = api.admin.getAllOptions.useQuery();

useEffect(() => {
const handleClickOutside = (_event: MouseEvent) => {
if (open) {
Expand Down Expand Up @@ -65,6 +64,7 @@ const Sidebar = (): JSX.Element => {
}`}
>
<span className="flex items-center justify-center text-lg text-gray-400">
{/* https://heroicons.com/ */}
<svg
fill="none"
strokeLinecap="round"
Expand Down Expand Up @@ -92,6 +92,7 @@ const Sidebar = (): JSX.Element => {
}`}
>
<span className="flex items-center justify-center text-lg text-gray-400">
{/* https://heroicons.com/ */}
<svg
fill="none"
strokeLinecap="round"
Expand All @@ -107,7 +108,7 @@ const Sidebar = (): JSX.Element => {
<span className="ml-3">{t("networks")}</span>
</Link>
</li>
{globalOption?.ztCentralApiKey ? (
{me?.options?.ztCentralApiKey ? (
<li className="my-px">
<Link
href="/central"
Expand All @@ -118,17 +119,21 @@ const Sidebar = (): JSX.Element => {
: "hover:bg-slate-700"
}`}
>
{/* https://heroicons.com/ */}
<span className="flex items-center justify-center text-lg text-gray-400">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
viewBox="0 0 24 24"
strokeWidth="1.5"
stroke="currentColor"
className="h-6 w-6"
className="w-6 h-6"
>
<path d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4" />
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 21a9.004 9.004 0 008.716-6.747M12 21a9.004 9.004 0 01-8.716-6.747M12 21c2.485 0 4.5-4.03 4.5-9S14.485 3 12 3m0 18c-2.485 0-4.5-4.03-4.5-9S9.515 3 12 3m0 0a8.997 8.997 0 017.843 4.582M12 3a8.997 8.997 0 00-7.843 4.582m15.686 0A11.953 11.953 0 0112 10.5c-2.998 0-5.74-1.1-7.843-2.918m15.686 0A8.959 8.959 0 0121 12c0 .778-.099 1.533-.284 2.253m0 0A17.919 17.919 0 0112 16.5c-3.162 0-6.133-.815-8.716-2.247m0 0A9.015 9.015 0 013 12c0-1.605.42-3.113 1.157-4.418"
/>
</svg>
</span>
<span className="ml-3">
Expand Down Expand Up @@ -291,8 +296,7 @@ const Sidebar = (): JSX.Element => {
href="/user-settings?tab=account"
className={`flex h-10 flex-row items-center rounded-lg px-3
${
router.pathname === "/user-settings" &&
router.query.tab === "account"
router.pathname.includes("/user-settings")
? "bg-gray-100 text-gray-700"
: "hover:bg-slate-700"
}`}
Expand Down
1 change: 0 additions & 1 deletion src/components/modules/deletedNetworkMembersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const DeletedNetworkMembersTable = ({ nwid }) => {
},
});
const columnHelper = createColumnHelper<MemberEntity>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const columns = useMemo<ColumnDef<MemberEntity>[]>(
() => [
columnHelper.accessor("authorized", {
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/table/memberEditCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const MemberEditCell = ({ nwid, central = false }: IProp) => {
{ enabled: !!nwid },
);

const { data: options } = api.admin.getAllOptions.useQuery();
const { data: me } = api.auth.me.useQuery();
const { mutate: updateMemberDatabaseOnly } =
api.networkMember.UpdateDatabaseOnly.useMutation();
const { mutate: updateMember } = api.networkMember.Update.useMutation({
Expand Down Expand Up @@ -111,7 +111,7 @@ const MemberEditCell = ({ nwid, central = false }: IProp) => {
<form>
<span className="flex items-center space-x-2">
{!central &&
options?.showNotationMarkerInTableRow &&
me?.options?.showNotationMarkerInTableRow &&
notations?.map((notation) => (
<div
key={notation.label?.name}
Expand Down
Loading

0 comments on commit 4a4cbc7

Please sign in to comment.