Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo second namespace, post file on filefeed space creation #22

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
}
Comment on lines +30 to +36
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling around the file fetching and posting process to enhance the robustness of the space creation functionality.

try {
  const file = await fetchFileFromDrive();
  await FlatfileService.postFileToSpace({
    flatfileSpaceId: space.flatfileSpaceId,
    file,
  });
} catch (fileError) {
  console.error("Error handling file feed workflow: ", fileError);
  return new NextResponse("Error handling file feed workflow", { status: 500 });
}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (space.workflowType === WorkflowType.FileFeed) {
const file = await fetchFileFromDrive();
await FlatfileService.postFileToSpace({
flatfileSpaceId: space.flatfileSpaceId,
file,
});
}
if (space.workflowType === WorkflowType.FileFeed) {
try {
const file = await fetchFileFromDrive();
await FlatfileService.postFileToSpace({
flatfileSpaceId: space.flatfileSpaceId,
file,
});
} catch (fileError) {
console.error("Error handling file feed workflow: ", fileError);
return new NextResponse("Error handling file feed workflow", { status: 500 });
}
}


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;
}
Comment on lines +6 to +39
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling around the temporary file operations to ensure any issues during file creation or writing are appropriately managed.

try {
  const tmpFile = await tmp.file({
    prefix: "products-sample-data-",
    postfix: ".csv",
  });
  fs.writeFileSync(tmpFile.path, response.data);
  file = fs.createReadStream(tmpFile.path);
  file.on("close", () => {
    tmpFile.cleanup();
  });
} catch (tmpError) {
  console.error("Error handling temporary file: ", tmpError);
  throw tmpError;
}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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;
}
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
try {
const tmpFile = await tmp.file({
prefix: "products-sample-data-",
postfix: ".csv",
});
fs.writeFileSync(tmpFile.path, response.data);
file = fs.createReadStream(tmpFile.path);
file.on("close", () => {
tmpFile.cleanup();
});
} catch (tmpError) {
console.error("Error handling temporary file: ", tmpError);
throw tmpError;
}
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
Loading