Skip to content

Commit

Permalink
clean up click events further
Browse files Browse the repository at this point in the history
  • Loading branch information
SamratSahoo committed Mar 5, 2024
1 parent 6080205 commit 90a54ed
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 27 deletions.
16 changes: 15 additions & 1 deletion api/__tests__/click-event.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { afterAll, beforeAll, describe, expect, test } from '@jest/globals';
import { afterAll, beforeAll, describe, expect, test, afterEach } from '@jest/globals';
import request, { Test } from "supertest";
import { api } from "@/netlify/functions/api";
import { Project } from '@/src/utils/types';
import { deleteProjectById } from '@/src/actions/project';
import { Server, IncomingMessage, ServerResponse } from 'http';
import TestAgent from 'supertest/lib/agent';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { deleteClickEvents, getClickEvents } from '@/src/actions/click-event';

let testProject: Project | null = null;
let server: Server<typeof IncomingMessage, typeof ServerResponse>;
Expand Down Expand Up @@ -39,12 +40,21 @@ describe("/api/events/click-event", () => {
userId: "exampleUserId"
};

afterEach(async () => {
// Clean up click events
await deleteClickEvents();
})

test("Create new click event with valid client token", async () => {
const response = await agent
.post("/api/events/click-event")
.set("clienttoken", testProject?.clientApiKey as string)
.send(clickEventProperties);
expect(response.status).toBe(200);

const events = await getClickEvents();
expect(events.length).toEqual(1);

});

test("Create new click event without valid client token", async () => {
Expand All @@ -53,6 +63,10 @@ describe("/api/events/click-event", () => {
.set("clienttoken", "invalid client token")
.send(clickEventProperties);
expect(response.status).toBe(403);


const events = await getClickEvents();
expect(events.length).toEqual(0);
});
});
});
4 changes: 2 additions & 2 deletions api/src/actions/click-event.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ClickEvent } from "@/src/utils/types";
import { ClickEvent, EventCategories, EventSubcategories } from "@/src/utils/types";
import { dbConnect } from "@/src/utils/db-connect";
import { ClickEventModel } from "@/src/models/click-event";
import ProjectModel from "@/src/models/project";

export const createClickEvent = async (event: Partial<ClickEvent>) => {
await dbConnect();
const createdEvent = await ClickEventModel.create(event);
const createdEvent = await ClickEventModel.create({ ...event, category: EventCategories.INTERACTION, subcategory: EventSubcategories.CLICK });
return createdEvent;
}

Expand Down
10 changes: 8 additions & 2 deletions api/src/actions/custom-event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomEventType } from "@/src/utils/types";
import { CustomEventType, EventCategories } from "@/src/utils/types";
import { dbConnect } from "@/src/utils/db-connect";
import CustomEventTypeModel from "@/src/models/custom-event-type";
import CustomEvent from "@/src/models/custom-event";
Expand All @@ -17,7 +17,13 @@ export const createCustomEvent = async (projectId: string, eventTypeId: string,
&& Object.keys(typeProperties).every(k => properties.hasOwnProperty(k))) {
return null;
}
const createdEvent = await CustomEvent.create({ projectId, eventTypeId, properties });
const createdEvent = await CustomEvent.create({
projectId,
eventTypeId,
properties,
category: eventType.category,
subcategory: eventType.subcategory
});
return createdEvent;
}
//one function to get eventTypeId, then this paginated method
Expand Down
4 changes: 2 additions & 2 deletions api/src/actions/input-event.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { InputEvent } from "@/src/utils/types";
import { EventCategories, EventSubcategories, InputEvent } from "@/src/utils/types";
import { dbConnect } from "@/src/utils/db-connect";
import { InputEventModel } from "@/src/models/input-event";
import ProjectModel from "@/src/models/project";

export const createInputEvent = async (event: Partial<InputEvent>) => {
await dbConnect();
const createdEvent = await InputEventModel.create(event);
const createdEvent = await InputEventModel.create({ ...event, category: EventCategories.INTERACTION, subcategory: EventSubcategories.INPUT });
return createdEvent;
}

Expand Down
5 changes: 3 additions & 2 deletions api/src/actions/visit-event.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { VisitEvent } from "@/src/utils/types";
import { EventCategories, EventSubcategories, VisitEvent } from "@/src/utils/types";
import { dbConnect } from "@/src/utils/db-connect";
import { VisitEventModel } from "@/src/models/visit-event";
import Project from "@/src/models/project";

export const createVisitEvent = async (event: Partial<VisitEvent>) => {
await dbConnect();
const createdEvent = await VisitEventModel.create(event);
const createdEvent = await VisitEventModel.create({ ...event, category: EventCategories.ACTIVITY, subcategory: EventSubcategories.VISIT });
return createdEvent;
}

Expand All @@ -15,6 +15,7 @@ export const getVisitEvents = async (date?: Date) => {
const events = await VisitEventModel.find({ createdAt: { $gte: fromDate } })
return events
}

export const paginatedGetVisitEvents = async (afterDate: Date, afterID: string, limit: number, projectName: String) => {
await dbConnect();
const project = await Project.findOne({ projectName: projectName })
Expand Down
2 changes: 0 additions & 2 deletions api/src/controllers/events/click-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const clickEventRoute = APIWrapper({
}
const event: Partial<ClickEvent> = {
projectId: project._id,
category: EventCategories.INTERACTION,
subcategory: EventSubcategories.CLICK,
eventProperties: {
objectId,
userId
Expand Down
24 changes: 11 additions & 13 deletions api/src/models/custom-event.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import mongoose, { Schema } from "mongoose";
import { CustomEvent } from "@/src/utils/types";
import ProjectModel from "@/src/models/project";
import mongoose, { CallbackError, Schema } from "mongoose";
import { BaseEvent, CustomEvent, EventCategories } from "@/src/utils/types";
import CustomEventTypeModel from "@/src/models/custom-event-type";

export const CustomEventSchema = new mongoose.Schema<CustomEvent>({
subcategory: {
type: String,
required: true,
},

const CustomEventSchema = new Schema<BaseEvent & CustomEvent>({
properties: {
type: Schema.Types.Mixed,
required: true
},
projectId: {
type: Schema.Types.ObjectId,
required: true,
ref: ProjectModel.modelName
},
eventTypeId: {
type: Schema.Types.ObjectId,
required: true,
ref: CustomEventTypeModel.modelName
}
}, { timestamps: true });

CustomEventSchema.pre("save", async function (this: Document & CustomEvent, next: (err?: CallbackError) => void) {
if (!this.category) {
this.category = EventCategories.CUSTOM;
}
next();
});

const CustomEventModel =
(mongoose.models.CustomEvent as mongoose.Model<CustomEvent>) ||
mongoose.model<CustomEvent>("CustomEvent", CustomEventSchema);
Expand Down
5 changes: 2 additions & 3 deletions api/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ export interface CustomEventType {
projectId: string | Types.ObjectId;
}

export interface CustomEvent {
export interface CustomEvent extends BaseEvent {
_id: string | Types.ObjectId;
projectId: string | Types.ObjectId;
eventTypeId: string | Types.ObjectId;
subcategory: string;
properties: string[] | Types.Array<string>;
}

Expand Down Expand Up @@ -98,6 +96,7 @@ export interface InternalResponseData<T> {
export enum EventCategories {
INTERACTION = "Interaction",
ACTIVITY = "Activity",
CUSTOM = "Custom"
}

export enum EventSubcategories {
Expand Down

0 comments on commit 90a54ed

Please sign in to comment.