Skip to content

Commit

Permalink
fix: fixed and migrated analytics to mongodb (#20)
Browse files Browse the repository at this point in the history
* deps: mongodb

* fix: updated the traits action to save data to mongo

* fix: updated the locators action to save data to mongo

* deps: removed airtable dependency
  • Loading branch information
The24thDS authored Jul 28, 2024
1 parent afee7e3 commit fd83c4c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 129 deletions.
45 changes: 17 additions & 28 deletions netlify/functions/save_locators_interaction.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
import { Handler } from "@netlify/functions";
import { Response } from "@netlify/functions/dist/function/response";
import Airtable from "airtable";
import { Handler, HandlerResponse } from "@netlify/functions";
import { MongoClient } from "mongodb";
import invariant from "tiny-invariant";

invariant(process.env.MONGODB_URI, "MongoDB URI is not defined.");
const mongoClient = new MongoClient(process.env.MONGODB_URI);
const clientPromise = mongoClient.connect();

const saveLocatorsInteraction = async (
type: "generate" | "copy",
amount: number,
stateTime: number
) => {
const { AIRTABLE_API_KEY, AIRTABLE_BASE, AIRTABLE_LOCATORS_TABLE } =
const { MONGODB_DATABASE, MONGODB_LOCATORS_COLLECTION } =
process.env;

invariant(AIRTABLE_API_KEY, "Airtable API key is not defined.");
invariant(AIRTABLE_BASE, "Airtable base is not defined.");
invariant(AIRTABLE_LOCATORS_TABLE, "Airtable table id is not defined.");
invariant(MONGODB_DATABASE, "Mongo DB name is not defined.");
invariant(MONGODB_LOCATORS_COLLECTION, "Mongo DB collection is not defined.");

Airtable.configure({
apiKey: AIRTABLE_API_KEY,
endpointUrl: "https://api.airtable.com",
});
const base = Airtable.base(AIRTABLE_BASE);
const database = (await clientPromise).db(MONGODB_DATABASE);
const collection = database.collection(MONGODB_LOCATORS_COLLECTION);

return new Promise((resolve, reject) => {
base(AIRTABLE_LOCATORS_TABLE).create(
{
action_type: type,
locators_number: amount,
state_time: stateTime,
created_at: new Date().toISOString(),
},
(err) => {
if (err) {
reject(err);
}
resolve(true);
}
);
await collection.insertOne({
action_type: type,
locators_number: amount,
state_time: stateTime,
created_at: new Date(),
});
};

Expand All @@ -46,7 +35,7 @@ const headers = {
};

const handler: Handler = async (event, _context) => {
const response: Response = {
const response: HandlerResponse = {
statusCode: 200,
headers,
};
Expand Down
56 changes: 23 additions & 33 deletions netlify/functions/save_traits_interaction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Handler } from "@netlify/functions";
import { Response } from "@netlify/functions/dist/function/response";
import Airtable from "airtable";
import { Handler, HandlerResponse } from "@netlify/functions";
import { MongoClient } from "mongodb";
import invariant from "tiny-invariant";
import { State } from "../../src/components/TraitsBuilderTab/index.d";

invariant(process.env.MONGODB_URI, "MongoDB URI is not defined.");
const mongoClient = new MongoClient(process.env.MONGODB_URI);
const clientPromise = mongoClient.connect();

const saveLocatorsInteraction = async (type: "PNG" | "DDS", state: State) => {
const { AIRTABLE_API_KEY, AIRTABLE_BASE, AIRTABLE_TRAITS_TABLE } =
const { MONGODB_DATABASE, MONGODB_TRAITS_COLLECTION } =
process.env;
const {
name,
Expand All @@ -17,37 +20,24 @@ const saveLocatorsInteraction = async (type: "PNG" | "DDS", state: State) => {
iconYOffset,
} = state;

invariant(AIRTABLE_API_KEY, "Airtable API key is not defined.");
invariant(AIRTABLE_BASE, "Airtable base is not defined.");
invariant(AIRTABLE_TRAITS_TABLE, "Airtable table id is not defined.");
invariant(MONGODB_DATABASE, "Mongo DB name is not defined.");
invariant(MONGODB_TRAITS_COLLECTION, "Mongo DB collection is not defined.");

Airtable.configure({
apiKey: AIRTABLE_API_KEY,
endpointUrl: "https://api.airtable.com",
});
const base = Airtable.base(AIRTABLE_BASE);
const database = (await clientPromise).db(MONGODB_DATABASE);
const collection = database.collection(MONGODB_TRAITS_COLLECTION);

return new Promise((resolve, reject) => {
base(AIRTABLE_TRAITS_TABLE).create(
{
name,
format: type,
bg_color: bgColor,
recolor_icon: recolorIcon,
icon_color: iconColor,
icon_scale: iconScale,
icon_x_offset: iconXOffset,
icon_y_offset: iconYOffset,
created_at: new Date().toISOString(),
},
(err) => {
if (err) {
reject(err);
}
resolve(true);
}
);
await collection.insertOne({
name,
format: type,
bg_color: bgColor,
recolor_icon: recolorIcon,
icon_color: iconColor,
icon_scale: iconScale,
icon_x_offset: iconXOffset,
icon_y_offset: iconYOffset,
created_at: new Date(),
});

};

const headers = {
Expand All @@ -57,7 +47,7 @@ const headers = {
};

const handler: Handler = async (event, _context) => {
const response: Response = {
const response: HandlerResponse = {
statusCode: 200,
headers,
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"@mantine/dropzone": "^7.0.0",
"@mantine/hooks": "^7.0.0",
"@mantine/notifications": "^7.0.0",
"@netlify/functions": "^1.0.0",
"@netlify/functions": "^2.8.1",
"@sentry/browser": "^7.12.1",
"@tabler/icons-react": "^3.5.0",
"airtable": "^0.11.4",
"filesize": "^10.0.12",
"konva": "^8.3.14",
"mongodb": "^6.8.0",
"random": "^3.0.6",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
Loading

0 comments on commit fd83c4c

Please sign in to comment.