Skip to content
180 changes: 180 additions & 0 deletions backend/controllers/rec.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { Response, NextFunction } from "express";
import { RequestExtended } from "../middleware/verifyAuth";
import prisma from "../prisma/client";
import cosineSimilarity from "../utils/cosineSimilarity";
import normalizeTags from "../utils/normalizeTags";
import { getDistanceFromLatLonInKm } from "../utils/googleMapsApi";

export const recommendEvents = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
// modify during integration
const loggedInUserId = req.body.userId;

const latitude = req.body.latitude;
const longitude = req.body.longitude;

const user = await prisma.user.findUnique({
where: {
id: loggedInUserId,
},
include: {
tags: true,
},
});
const userTags = user?.tags;

const allTags = await prisma.tag.findMany({});

if (userTags) {
const userTagsNormalized = normalizeTags(userTags, allTags);

const allEvents = await prisma.event.findMany({
include: {
tags: true,
location: true,
},
});

const eventTagMap = new Map();

allEvents.forEach((event) => {
const eventTagsNormalized = normalizeTags(event.tags, allTags);
eventTagMap.set(event.id, eventTagsNormalized);
});

const recommendationMap = new Map();

for (let [key, value] of eventTagMap) {
let similarity = cosineSimilarity(userTagsNormalized, value);
recommendationMap.set(key, similarity);
}

const similarityArray = Array.from(recommendationMap.entries());
similarityArray.sort((a, b) => b[1] - a[1]);

// sorting similarityArray according to distance
similarityArray.sort((a, b) => {
const eventA = allEvents.find((event) => event.id === a[0]);
const eventB = allEvents.find((event) => event.id === b[0]);
if (eventA && eventB) {
const distanceA = getDistanceFromLatLonInKm(
latitude,
longitude,
eventA.location.latitude,
eventA.location.longitude,
);
const distanceB = getDistanceFromLatLonInKm(
latitude,
longitude,
eventB.location.latitude,
eventB.location.longitude,
);
return distanceA - distanceB;
}
return 0;
});

res.status(200).json({ similarityArray });
}
};

export const recommendPosts = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
// modify during integration
const loggedInUserId = req.body.userId;
const user = await prisma.user.findUnique({
where: {
id: loggedInUserId,
},
include: {
tags: true,
},
});
const userTags = user?.tags;

const allTags = await prisma.tag.findMany({});

if (userTags) {
const userTagsNormalized = normalizeTags(userTags, allTags);

const allPosts = await prisma.post.findMany({
include: {
tags: true,
},
});

const postTagMap = new Map();

allPosts.forEach((post) => {
const eventTagsNormalized = normalizeTags(post.tags, allTags);
postTagMap.set(post.id, eventTagsNormalized);
});

const recommendationMap = new Map();

for (let [key, value] of postTagMap) {
let similarity = cosineSimilarity(userTagsNormalized, value);
recommendationMap.set(key, similarity);
}

const similarityArray = Array.from(recommendationMap.entries());
similarityArray.sort((a, b) => b[1] - a[1]);

res.status(200).json({ similarityArray });
}
};

export const recommendUsers = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
// modify during integration
const loggedInUserId = req.body.userId;
const user = await prisma.user.findUnique({
where: {
id: loggedInUserId,
},
include: {
tags: true,
},
});
const userTags = user?.tags;

const allTags = await prisma.tag.findMany({});

if (userTags) {
const userTagsNormalized = normalizeTags(userTags, allTags);

const allUsers = await prisma.user.findMany({
include: {
tags: true,
},
});

const userTagMap = new Map();

allUsers.forEach((user) => {
const eventTagsNormalized = normalizeTags(user.tags, allTags);
userTagMap.set(user.id, eventTagsNormalized);
});

const recommendationMap = new Map();

for (let [key, value] of userTagMap) {
let similarity = cosineSimilarity(userTagsNormalized, value);
recommendationMap.set(key, similarity);
}

const similarityArray = Array.from(recommendationMap.entries());
similarityArray.sort((a, b) => b[1] - a[1]);

res.status(200).json({ similarityArray });
}
};
190 changes: 190 additions & 0 deletions backend/controllers/tag.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import { RequestExtended } from "../middleware/verifyAuth";
import { Response, NextFunction } from "express";
import prisma from "../prisma/client";

export const readUserTags = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
// modify during integration
const loggedInUserId = req.body.userId;
const user = await prisma.user.findUnique({
where: {
id: loggedInUserId,
},
include: {
tags: true,
},
});

res.status(200).json({ tags: user?.tags });
};

export const modifyUserTags = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
// modify during integration
const loggedInUserId = req.body.userId;
const user = await prisma.user.findUnique({
where: {
id: loggedInUserId,
},
include: {
tags: true,
},
});

console.log(user);

for (const tag of req.body.tags) {
const foundTag = await prisma.tag.findUnique({
where: {
name: tag,
},
});

if (foundTag) {
await prisma.user.update({
where: {
id: loggedInUserId,
},
data: {
tags: {
connect: {
id: foundTag.id,
},
},
},
});
}
}

res.status(200).json({ message: "Tags updated successfully" });
};

export const readEventTags = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
// modify during integration
const relevantEventId = req.body.eventId;
const event = await prisma.event.findUnique({
where: {
id: relevantEventId,
},
include: {
tags: true,
},
});

res.status(200).json({ tags: event?.tags });
};

export const modifyEventTags = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
const relevantEventId = req.body.eventId;
const event = await prisma.event.findUnique({
where: {
id: relevantEventId,
},
include: {
tags: true,
},
});

console.log(relevantEventId);

for (const tag of req.body.tags) {
const foundTag = await prisma.tag.findUnique({
where: {
name: tag,
},
});

if (foundTag) {
await prisma.event.update({
where: {
id: relevantEventId,
},
data: {
tags: {
connect: {
id: foundTag.id,
},
},
},
});
}
}

res.status(200).json({ message: "Tags updated successfully" });
};

export const readPostTags = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
// modify during integration
const relevantPostId = req.body.postId;
const post = await prisma.post.findUnique({
where: {
id: relevantPostId,
},
include: {
tags: true,
},
});

res.status(200).json({ tags: post?.tags });
};

export const modifyPostTags = async (
req: RequestExtended,
res: Response,
next: NextFunction,
) => {
const relevantPostId = req.body.postId;
const post = await prisma.event.findUnique({
where: {
id: relevantPostId,
},
include: {
tags: true,
},
});

console.log(relevantPostId);

for (const tag of req.body.tags) {
const foundTag = await prisma.tag.findUnique({
where: {
name: tag,
},
});

if (foundTag) {
await prisma.user.update({
where: {
id: relevantPostId,
},
data: {
tags: {
connect: {
id: foundTag.id,
},
},
},
});
}
}

res.status(200).json({ message: "Tags updated successfully" });
};
Loading