Skip to content

Commit

Permalink
Undo second namespace, post file on filefeed space creation (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmosesman authored Apr 10, 2024
1 parent 7d87904 commit 2c5980b
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 21 deletions.
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ FLATFILE_API_KEY='sk_...'
FLATFILE_ENVIRONMENT_ID='us_env_...'
FLATFILE_NAMESPACE='plmproject'
NEXT_PUBLIC_FLATFILE_NAMESPACE='plmproject'
FLATFILE_SERVICES_NAMESPACE='servicesproject'
NEXT_PUBLIC_FLATFILE_PUBLISHABLE_KEY='pk_...'
NEXT_PUBLIC_FLATFILE_ENVIRONMENT_ID='us_env_...'
NEXT_PUBLIC_APP_ID='products-show'
LISTENER_AUTH_TOKEN='token'
LISTENER_AUTH_TOKEN='token'
NEXT_PUBLIC_GOOGLE_DRIVE_FILE_ID='1mUeqmEUvYzqHPxOo0OUS_My4oJTzo-RX'
GOOGLE_DRIVE_FILE_ID='1mUeqmEUvYzqHPxOo0OUS_My4oJTzo-RX'
GOOGLE_DRIVE_API_KEY='...'
2 changes: 1 addition & 1 deletion app/(authenticated)/file-feed/[spaceId]/workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function Workspace({
<a
className="underline text-gray-400"
target="_blank"
href="https://drive.google.com/file/d/1Y9-rnoDuqxrvV9JIpDYoXq-nt3cdAJQ3/view?usp=sharing"
href={`https://drive.google.com/file/d/${process.env.NEXT_PUBLIC_GOOGLE_DRIVE_FILE_ID}/view?usp=sharing`}
>
here.
</a>
Expand Down
18 changes: 16 additions & 2 deletions app/api/create-space/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { authenticatedRoute } from "@/lib/api-helpers";
import { fetchFileFromDrive } from "@/lib/google-drive";
import { FlatfileService } from "@/lib/services/flatfile";
import { SpaceService } from "@/lib/services/space";
import { WorkflowType } from "@/lib/workflow-type";
import { NextRequest, NextResponse } from "next/server";
import invariant from "ts-invariant";

Expand All @@ -11,16 +14,27 @@ export const POST = async (request: NextRequest, context: { params: any }) => {

const userId = context.user.id;

let space;

try {
const space = await SpaceService.createSpace({
space = await SpaceService.createSpace({
workflowType: json.workflowType,
userId,
spaceName: json.spaceName,
});
return NextResponse.json({ spaceId: space.id }, { status: 201 });
} catch (e) {
console.error(`Error creating space for ${userId}`, e);
return new NextResponse("Error creating space", { status: 500 });
}

if (space.workflowType === WorkflowType.FileFeed) {
const file = await fetchFileFromDrive();
await FlatfileService.postFileToSpace({
flatfileSpaceId: space.flatfileSpaceId,
file,
});
}

return NextResponse.json({ spaceId: space.id }, { status: 201 });
});
};
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace NodeJS {
interface ProcessEnv {
NEXT_PUBLIC_FLATFILE_PUBLISHABLE_KEY: string;
NEXT_PUBLIC_FLATFILE_ENVIRONMENT_ID: string;
FLATFILE_ENVIRONMENT_ID: string;
FLATFILE_NAMESPACE: string;
FLATFILE_SERVICES_NAMESPACE: string;
}
}
40 changes: 40 additions & 0 deletions lib/google-drive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from "fs";
import tmp from "tmp-promise";

const { google } = require("googleapis");

export const fetchFileFromDrive = async (): Promise<fs.ReadStream> => {
const service = google.drive({
version: "v3",
auth: process.env.GOOGLE_DRIVE_API_KEY,
});

let file;

try {
const response = await service.files.get({
fileId: process.env.GOOGLE_DRIVE_FILE_ID,
alt: "media",
});

// Create a temporary file and write the data into it
const tmpFile = await tmp.file({
prefix: "products-sample-data-",
postfix: ".csv",
});
fs.writeFileSync(tmpFile.path, response.data);

// Create a fs.ReadStream from the temporary file
file = fs.createReadStream(tmpFile.path);

// When you're done with the file, close and unlink it
file.on("close", () => {
tmpFile.cleanup();
});

return file;
} catch (err) {
console.log("fetchFileFromDrive error: ", err);
throw err;
}
};
18 changes: 15 additions & 3 deletions lib/services/flatfile.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import api from "@flatfile/api";
import { RecordDataWithLinks } from "@flatfile/api/api";
import { ReadStream } from "fs";

export class FlatfileService {
static createSpace = async ({
userId,
flatfileNamespace,
spaceName,
}: {
userId: string;
flatfileNamespace: string;
spaceName: string;
}) => {
const { data } = await api.spaces.create({
name: spaceName,
environmentId: process.env.FLATFILE_ENVIRONMENT_ID,
autoConfigure: true,
namespace: flatfileNamespace,
namespace: process.env.FLATFILE_NAMESPACE,
metadata: {
userId,
},
Expand Down Expand Up @@ -99,4 +98,17 @@ export class FlatfileService {

return records;
}

static async postFileToSpace({
flatfileSpaceId,
file,
}: {
flatfileSpaceId: string;
file: ReadStream;
}) {
return await api.files.upload(file, {
spaceId: flatfileSpaceId,
environmentId: process.env.FLATFILE_ENVIRONMENT_ID,
});
}
}
11 changes: 0 additions & 11 deletions lib/services/space.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { prismaClient } from "@/lib/prisma-client";
import { FlatfileService } from "./flatfile";
import { WorkflowType } from "@/lib/workflow-type";

export class SpaceService {
static async createSpace({
Expand All @@ -12,19 +11,9 @@ export class SpaceService {
userId: string;
spaceName: string;
}) {
let flatfileNamespace;

// TODO: Adjust for other workflows
if (workflowType === WorkflowType.FileFeed) {
flatfileNamespace = process.env.FLATFILE_SERVICES_NAMESPACE;
} else {
flatfileNamespace = process.env.FLATFILE_NAMESPACE;
}

const flatfileSpace = await FlatfileService.createSpace({
userId,
spaceName,
flatfileNamespace,
});

return await prismaClient.space.create({
Expand Down
Loading

0 comments on commit 2c5980b

Please sign in to comment.