Skip to content

Commit

Permalink
Merge pull request #3 from tazo90/feature/api-app-consumers
Browse files Browse the repository at this point in the history
Feature/api app consumers
  • Loading branch information
tazo90 authored May 7, 2022
2 parents 59e2b55 + 675b5db commit 6020614
Show file tree
Hide file tree
Showing 19 changed files with 560 additions and 304 deletions.
18 changes: 16 additions & 2 deletions src/components/apps/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import StoresApp from "./stores";
import JobsApp from "./jobs";
import { ApplicationType } from "@prisma/client";

export default {
stores: StoresApp,
jobs: JobsApp,
stores: {
app: StoresApp,
type: ApplicationType.STORES,
description: 'Stores locator'
},
jobs: {
app: JobsApp,
type: ApplicationType.JOBS,
description: 'Jobs locator'
},
ecommerce: {
app: null,
type: ApplicationType.ECOMMERCE,
description: 'Ecommerce locator'
}
};
1 change: 0 additions & 1 deletion src/components/common/page-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default function PageMenu(props: PageMenuProps) {
)}
<nav className="space-y-1">
{props.menu.map((item) => {
console.log("ITEM", item.href, query);
const page = item.href.split("/").slice(-1)[0].replace(":", "");
const isActive = currentPage === page;
const link =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const hashAPIKey = (apiKey: string): string =>

// Generate a random API key. Prisma already makes sure it's unique. So no need to add salts like with passwords.
export const generateUniqueAPIKey = (
apiKey = randomBytes(10).toString("hex")
apiKey = randomBytes(16).toString("hex")
) => [hashAPIKey(apiKey), apiKey];
16 changes: 16 additions & 0 deletions src/lib/lodash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// TODO: get rid of any types

export function pick(obj: any, ...props: any) {
return props.reduce((result: any, prop: string) => {
result[prop] = obj[prop];
return result;
}, {});
}

export function omit(obj: any, ...props: any) {
const result = { ...obj };
props.forEach((prop: string) => {
delete result[prop];
});
return result;
}
97 changes: 58 additions & 39 deletions src/lib/prisma/fixtures/amrest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApplicationStatus, ApplicationType, UserPlan } from "@prisma/client";
import { ApplicationConsumerStatus, ApplicationType, UserPlan } from "@prisma/client";

const organization = {
name: "AmRest",
Expand Down Expand Up @@ -113,18 +113,18 @@ const countries = [

const users = [
{
email: "tazo90@gmail.com",
email: "tazo@gmail.com",
password: "pass",
username: "tazo90",
name: "ala",
plan: UserPlan.FREE,
plan: UserPlan.PRO,
},
{
email: "dieselo@o2.pl",
password: "diesel",
username: "teampro",
email: "dieselo@gmail.pl",
password: "pass",
username: "dieselo",
name: "Team Pro Example",
plan: UserPlan.PRO,
plan: UserPlan.FREE,
},
];

Expand All @@ -145,46 +145,65 @@ const projects = [

const apps = [
{
uid: "1",
title: "KFC PL",
token: "ctskfcpl",
status: ApplicationStatus.DRAFT,
type: ApplicationType.JOBS,
type: ApplicationType.STORES,
paid: false,
domain: "https://cts.pl",
project: "cts",
consumers: [
{
uid: "1",
title: "AmRest Stores",
token: "amspacestores",
status: ApplicationConsumerStatus.DRAFT,
domain: "https://amspace.amrest.eu",
project: "amspace",
brands: ["kfc", "ph", "bk", "sbx"],
countries: ["pl", "cz", "hu"]
},
]
},
{
uid: "2",
title: "KFC PL",
token: "ecommercekfcpl",
status: ApplicationStatus.DRAFT,
type: ApplicationType.ECOMMERCE,
type: ApplicationType.JOBS,
paid: false,
domain: "https://kfc.pl",
project: "ecommerce",
consumers: [
{
uid: "2",
title: "KFC PL",
token: "ctskfcpl",
status: ApplicationConsumerStatus.DRAFT,
domain: "https://cts.pl",
project: "cts",
brands: ["kfc"],
countries: ["pl"],
},

]
},
{
uid: "3",
title: "PH PL",
token: "ecommercephpl",
status: ApplicationStatus.DRAFT,
type: ApplicationType.ECOMMERCE,
paid: false,
domain: "http://pizzahut.pl",
project: "ecommerce",
},
{
uid: "4",
title: "AmRest Stores",
token: "amspacestores",
status: ApplicationStatus.DRAFT,
type: ApplicationType.STORES,
paid: false,
domain: "https://amspace.amrest.eu",
project: "amspace",
},
];
consumers: [
{
uid: "3",
title: "KFC PL",
token: "ecommercekfcpl",
status: ApplicationConsumerStatus.DRAFT,
domain: "https://kfc.pl",
project: "ecommerce",
brands: ["kfc"],
countries: ["pl"]
},
{
uid: "4",
title: "PH PL",
token: "ecommercephpl",
status: ApplicationConsumerStatus.DRAFT,
domain: "http://pizzahut.pl",
project: "ecommerce",
brands: ["ph"],
countries: ["pl"]
},
]
},
]

export default {
organization,
Expand Down
153 changes: 153 additions & 0 deletions src/lib/prisma/migrations/20220506180039_consumers_table/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
Warnings:
- You are about to drop the column `description` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `domain` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `expires` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `projectId` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `status` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `title` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `token` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `uid` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `updatedAt` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `userId` on the `application` table. All the data in the column will be lost.
- You are about to drop the column `applicationId` on the `component` table. All the data in the column will be lost.
- You are about to drop the `_ApplicationToBrand` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `_ApplicationToCountry` table. If the table is not empty, all the data it contains will be lost.
- Added the required column `name` to the `application` table without a default value. This is not possible if the table is not empty.
- Added the required column `ownerId` to the `application` table without a default value. This is not possible if the table is not empty.
- Added the required column `consumerId` to the `component` table without a default value. This is not possible if the table is not empty.
*/
-- CreateEnum
CREATE TYPE "ApplicationConsumerStatus" AS ENUM ('active', 'draft', 'closed');

-- DropForeignKey
ALTER TABLE "_ApplicationToBrand" DROP CONSTRAINT "_ApplicationToBrand_A_fkey";

-- DropForeignKey
ALTER TABLE "_ApplicationToBrand" DROP CONSTRAINT "_ApplicationToBrand_B_fkey";

-- DropForeignKey
ALTER TABLE "_ApplicationToCountry" DROP CONSTRAINT "_ApplicationToCountry_A_fkey";

-- DropForeignKey
ALTER TABLE "_ApplicationToCountry" DROP CONSTRAINT "_ApplicationToCountry_B_fkey";

-- DropForeignKey
ALTER TABLE "application" DROP CONSTRAINT "application_projectId_fkey";

-- DropForeignKey
ALTER TABLE "application" DROP CONSTRAINT "application_userId_fkey";

-- DropForeignKey
ALTER TABLE "component" DROP CONSTRAINT "component_applicationId_fkey";

-- DropIndex
DROP INDEX "application_token_key";

-- DropIndex
DROP INDEX "application_uid_key";

-- AlterTable
ALTER TABLE "application" DROP COLUMN "description",
DROP COLUMN "domain",
DROP COLUMN "expires",
DROP COLUMN "projectId",
DROP COLUMN "status",
DROP COLUMN "title",
DROP COLUMN "token",
DROP COLUMN "uid",
DROP COLUMN "updatedAt",
DROP COLUMN "userId",
ADD COLUMN "name" TEXT NOT NULL,
ADD COLUMN "ownerId" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "component" DROP COLUMN "applicationId",
ADD COLUMN "consumerId" INTEGER NOT NULL;

-- DropTable
DROP TABLE "_ApplicationToBrand";

-- DropTable
DROP TABLE "_ApplicationToCountry";

-- DropEnum
DROP TYPE "ApplicationStatus";

-- CreateTable
CREATE TABLE "application_consumer" (
"id" SERIAL NOT NULL,
"uid" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"applicationId" INTEGER NOT NULL,
"projectId" INTEGER NOT NULL,
"title" TEXT NOT NULL,
"description" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),
"status" "ApplicationConsumerStatus" NOT NULL DEFAULT E'draft',
"token" TEXT NOT NULL,
"expires" TIMESTAMP(3) NOT NULL,
"domain" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "_ApplicationConsumerToBrand" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "_ApplicationConsumerToCountry" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "application_consumer_uid_key" ON "application_consumer"("uid");

-- CreateIndex
CREATE UNIQUE INDEX "application_consumer_token_key" ON "application_consumer"("token");

-- CreateIndex
CREATE UNIQUE INDEX "_ApplicationConsumerToBrand_AB_unique" ON "_ApplicationConsumerToBrand"("A", "B");

-- CreateIndex
CREATE INDEX "_ApplicationConsumerToBrand_B_index" ON "_ApplicationConsumerToBrand"("B");

-- CreateIndex
CREATE UNIQUE INDEX "_ApplicationConsumerToCountry_AB_unique" ON "_ApplicationConsumerToCountry"("A", "B");

-- CreateIndex
CREATE INDEX "_ApplicationConsumerToCountry_B_index" ON "_ApplicationConsumerToCountry"("B");

-- AddForeignKey
ALTER TABLE "application" ADD CONSTRAINT "application_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "application_consumer" ADD CONSTRAINT "application_consumer_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "application_consumer" ADD CONSTRAINT "application_consumer_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "project"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "application_consumer" ADD CONSTRAINT "application_consumer_applicationId_fkey" FOREIGN KEY ("applicationId") REFERENCES "application"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "component" ADD CONSTRAINT "component_consumerId_fkey" FOREIGN KEY ("consumerId") REFERENCES "application_consumer"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ApplicationConsumerToBrand" ADD FOREIGN KEY ("A") REFERENCES "application_consumer"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ApplicationConsumerToBrand" ADD FOREIGN KEY ("B") REFERENCES "brand"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ApplicationConsumerToCountry" ADD FOREIGN KEY ("A") REFERENCES "application_consumer"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ApplicationConsumerToCountry" ADD FOREIGN KEY ("B") REFERENCES "country"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- You are about to drop the column `name` on the `application` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "application" DROP COLUMN "name";
Loading

0 comments on commit 6020614

Please sign in to comment.