Skip to content

Commit fd83c4c

Browse files
authored
fix: fixed and migrated analytics to mongodb (#20)
* 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
1 parent afee7e3 commit fd83c4c

File tree

4 files changed

+141
-129
lines changed

4 files changed

+141
-129
lines changed

netlify/functions/save_locators_interaction.ts

+17-28
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
import { Handler } from "@netlify/functions";
2-
import { Response } from "@netlify/functions/dist/function/response";
3-
import Airtable from "airtable";
1+
import { Handler, HandlerResponse } from "@netlify/functions";
2+
import { MongoClient } from "mongodb";
43
import invariant from "tiny-invariant";
54

5+
invariant(process.env.MONGODB_URI, "MongoDB URI is not defined.");
6+
const mongoClient = new MongoClient(process.env.MONGODB_URI);
7+
const clientPromise = mongoClient.connect();
8+
69
const saveLocatorsInteraction = async (
710
type: "generate" | "copy",
811
amount: number,
912
stateTime: number
1013
) => {
11-
const { AIRTABLE_API_KEY, AIRTABLE_BASE, AIRTABLE_LOCATORS_TABLE } =
14+
const { MONGODB_DATABASE, MONGODB_LOCATORS_COLLECTION } =
1215
process.env;
1316

14-
invariant(AIRTABLE_API_KEY, "Airtable API key is not defined.");
15-
invariant(AIRTABLE_BASE, "Airtable base is not defined.");
16-
invariant(AIRTABLE_LOCATORS_TABLE, "Airtable table id is not defined.");
17+
invariant(MONGODB_DATABASE, "Mongo DB name is not defined.");
18+
invariant(MONGODB_LOCATORS_COLLECTION, "Mongo DB collection is not defined.");
1719

18-
Airtable.configure({
19-
apiKey: AIRTABLE_API_KEY,
20-
endpointUrl: "https://api.airtable.com",
21-
});
22-
const base = Airtable.base(AIRTABLE_BASE);
20+
const database = (await clientPromise).db(MONGODB_DATABASE);
21+
const collection = database.collection(MONGODB_LOCATORS_COLLECTION);
2322

24-
return new Promise((resolve, reject) => {
25-
base(AIRTABLE_LOCATORS_TABLE).create(
26-
{
27-
action_type: type,
28-
locators_number: amount,
29-
state_time: stateTime,
30-
created_at: new Date().toISOString(),
31-
},
32-
(err) => {
33-
if (err) {
34-
reject(err);
35-
}
36-
resolve(true);
37-
}
38-
);
23+
await collection.insertOne({
24+
action_type: type,
25+
locators_number: amount,
26+
state_time: stateTime,
27+
created_at: new Date(),
3928
});
4029
};
4130

@@ -46,7 +35,7 @@ const headers = {
4635
};
4736

4837
const handler: Handler = async (event, _context) => {
49-
const response: Response = {
38+
const response: HandlerResponse = {
5039
statusCode: 200,
5140
headers,
5241
};

netlify/functions/save_traits_interaction.ts

+23-33
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { Handler } from "@netlify/functions";
2-
import { Response } from "@netlify/functions/dist/function/response";
3-
import Airtable from "airtable";
1+
import { Handler, HandlerResponse } from "@netlify/functions";
2+
import { MongoClient } from "mongodb";
43
import invariant from "tiny-invariant";
54
import { State } from "../../src/components/TraitsBuilderTab/index.d";
65

6+
invariant(process.env.MONGODB_URI, "MongoDB URI is not defined.");
7+
const mongoClient = new MongoClient(process.env.MONGODB_URI);
8+
const clientPromise = mongoClient.connect();
9+
710
const saveLocatorsInteraction = async (type: "PNG" | "DDS", state: State) => {
8-
const { AIRTABLE_API_KEY, AIRTABLE_BASE, AIRTABLE_TRAITS_TABLE } =
11+
const { MONGODB_DATABASE, MONGODB_TRAITS_COLLECTION } =
912
process.env;
1013
const {
1114
name,
@@ -17,37 +20,24 @@ const saveLocatorsInteraction = async (type: "PNG" | "DDS", state: State) => {
1720
iconYOffset,
1821
} = state;
1922

20-
invariant(AIRTABLE_API_KEY, "Airtable API key is not defined.");
21-
invariant(AIRTABLE_BASE, "Airtable base is not defined.");
22-
invariant(AIRTABLE_TRAITS_TABLE, "Airtable table id is not defined.");
23+
invariant(MONGODB_DATABASE, "Mongo DB name is not defined.");
24+
invariant(MONGODB_TRAITS_COLLECTION, "Mongo DB collection is not defined.");
2325

24-
Airtable.configure({
25-
apiKey: AIRTABLE_API_KEY,
26-
endpointUrl: "https://api.airtable.com",
27-
});
28-
const base = Airtable.base(AIRTABLE_BASE);
26+
const database = (await clientPromise).db(MONGODB_DATABASE);
27+
const collection = database.collection(MONGODB_TRAITS_COLLECTION);
2928

30-
return new Promise((resolve, reject) => {
31-
base(AIRTABLE_TRAITS_TABLE).create(
32-
{
33-
name,
34-
format: type,
35-
bg_color: bgColor,
36-
recolor_icon: recolorIcon,
37-
icon_color: iconColor,
38-
icon_scale: iconScale,
39-
icon_x_offset: iconXOffset,
40-
icon_y_offset: iconYOffset,
41-
created_at: new Date().toISOString(),
42-
},
43-
(err) => {
44-
if (err) {
45-
reject(err);
46-
}
47-
resolve(true);
48-
}
49-
);
29+
await collection.insertOne({
30+
name,
31+
format: type,
32+
bg_color: bgColor,
33+
recolor_icon: recolorIcon,
34+
icon_color: iconColor,
35+
icon_scale: iconScale,
36+
icon_x_offset: iconXOffset,
37+
icon_y_offset: iconYOffset,
38+
created_at: new Date(),
5039
});
40+
5141
};
5242

5343
const headers = {
@@ -57,7 +47,7 @@ const headers = {
5747
};
5848

5949
const handler: Handler = async (event, _context) => {
60-
const response: Response = {
50+
const response: HandlerResponse = {
6151
statusCode: 200,
6252
headers,
6353
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
"@mantine/dropzone": "^7.0.0",
2020
"@mantine/hooks": "^7.0.0",
2121
"@mantine/notifications": "^7.0.0",
22-
"@netlify/functions": "^1.0.0",
22+
"@netlify/functions": "^2.8.1",
2323
"@sentry/browser": "^7.12.1",
2424
"@tabler/icons-react": "^3.5.0",
25-
"airtable": "^0.11.4",
2625
"filesize": "^10.0.12",
2726
"konva": "^8.3.14",
27+
"mongodb": "^6.8.0",
2828
"random": "^3.0.6",
2929
"react": "^18.3.1",
3030
"react-dom": "^18.3.1",

0 commit comments

Comments
 (0)