From 23c7b323720ae520cb34c029f729658adcf16657 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 19:50:15 +0530 Subject: [PATCH 01/17] feat: user and robot relation --- server/src/models/Robot.ts | 7 +++++++ server/src/models/User.ts | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/server/src/models/Robot.ts b/server/src/models/Robot.ts index a48b9af1..41e974f9 100644 --- a/server/src/models/Robot.ts +++ b/server/src/models/Robot.ts @@ -1,6 +1,8 @@ import { Model, DataTypes, Optional } from 'sequelize'; import sequelize from '../storage/db'; import { WorkflowFile, Where, What, WhereWhatPair } from 'maxun-core'; +import User from './User'; // Import User model +import Run from './Run'; interface RobotMeta { name: string; @@ -82,4 +84,9 @@ Robot.init( } ); +Robot.hasMany(Run, { + foreignKey: 'robotId', + as: 'runs', // Alias for the relation +}); + export default Robot; \ No newline at end of file diff --git a/server/src/models/User.ts b/server/src/models/User.ts index 5a3d552c..f7c22b5b 100644 --- a/server/src/models/User.ts +++ b/server/src/models/User.ts @@ -1,5 +1,6 @@ import { DataTypes, Model, Optional } from 'sequelize'; import sequelize from '../storage/db'; +import Robot from './Robot'; interface UserAttributes { id: number; @@ -79,4 +80,9 @@ User.init( } ); +User.hasMany(Robot, { + foreignKey: 'userId', + as: 'robots', // Alias for the relation + }); + export default User; From 9aa1495b932c98716bc398658e65ca4b8f215706 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:05:48 +0530 Subject: [PATCH 02/17] chore: remove todo --- server/src/api/record.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/api/record.ts b/server/src/api/record.ts index b8f1f2bc..3f1dcd50 100644 --- a/server/src/api/record.ts +++ b/server/src/api/record.ts @@ -114,7 +114,6 @@ router.get("/robots/:id", requireAPIKey, async (req: Request, res: Response) => } }); -// TODO: Format runs to send more data formatted router.get("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => { try { const runs = await Run.findAll({ From bbffd90e84e6485dfc8d4d03c04f2ace1d6ce0f3 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:38:37 +0530 Subject: [PATCH 03/17] feat: remove associations --- server/src/models/Robot.ts | 8 ++++---- server/src/models/User.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/server/src/models/Robot.ts b/server/src/models/Robot.ts index 41e974f9..b419fe8f 100644 --- a/server/src/models/Robot.ts +++ b/server/src/models/Robot.ts @@ -84,9 +84,9 @@ Robot.init( } ); -Robot.hasMany(Run, { - foreignKey: 'robotId', - as: 'runs', // Alias for the relation -}); +// Robot.hasMany(Run, { +// foreignKey: 'robotId', +// as: 'runs', // Alias for the relation +// }); export default Robot; \ No newline at end of file diff --git a/server/src/models/User.ts b/server/src/models/User.ts index f7c22b5b..2bb465d2 100644 --- a/server/src/models/User.ts +++ b/server/src/models/User.ts @@ -80,9 +80,9 @@ User.init( } ); -User.hasMany(Robot, { - foreignKey: 'userId', - as: 'robots', // Alias for the relation - }); +// User.hasMany(Robot, { +// foreignKey: 'userId', +// as: 'robots', // Alias for the relation +// }); export default User; From 7cce881653694ccc3e656689df0b0f52662a22ff Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:39:42 +0530 Subject: [PATCH 04/17] feat: include userId --- server/src/models/Robot.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server/src/models/Robot.ts b/server/src/models/Robot.ts index b419fe8f..2f4d1eec 100644 --- a/server/src/models/Robot.ts +++ b/server/src/models/Robot.ts @@ -19,6 +19,7 @@ interface RobotWorkflow { interface RobotAttributes { id: string; + userId: string; recording_meta: RobotMeta; recording: RobotWorkflow; google_sheet_email?: string | null; @@ -32,6 +33,7 @@ interface RobotCreationAttributes extends Optional { } class Robot extends Model implements RobotAttributes { public id!: string; + public userId!: string; public recording_meta!: RobotMeta; public recording!: RobotWorkflow; public google_sheet_email!: string | null; @@ -48,6 +50,10 @@ Robot.init( defaultValue: DataTypes.UUIDV4, primaryKey: true, }, + userId: { + type: DataTypes.UUID, + allowNull: false, + }, recording_meta: { type: DataTypes.JSONB, allowNull: false, From 1214aeb824f67dd5e6d5b232dd422aeba99fe394 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:39:56 +0530 Subject: [PATCH 05/17] chore: lint --- server/src/models/Robot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/models/Robot.ts b/server/src/models/Robot.ts index 2f4d1eec..00294b34 100644 --- a/server/src/models/Robot.ts +++ b/server/src/models/Robot.ts @@ -2,7 +2,7 @@ import { Model, DataTypes, Optional } from 'sequelize'; import sequelize from '../storage/db'; import { WorkflowFile, Where, What, WhereWhatPair } from 'maxun-core'; import User from './User'; // Import User model -import Run from './Run'; +import Run from './Run'; interface RobotMeta { name: string; From 17ea711c32d687b488303c20c446f2bfe4939fd0 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:42:00 +0530 Subject: [PATCH 06/17] feat: get robot by user id --- server/src/routes/storage.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/src/routes/storage.ts b/server/src/routes/storage.ts index c684d6ea..31124b87 100644 --- a/server/src/routes/storage.ts +++ b/server/src/routes/storage.ts @@ -29,7 +29,9 @@ router.all('/', requireSignIn, (req, res, next) => { */ router.get('/recordings', requireSignIn, async (req, res) => { try { - const data = await Robot.findAll(); + const data = await Robot.findAll({ + where: { userId: req.user.id }, + }); return res.send(data); } catch (e) { logger.log('info', 'Error while reading recordings'); From 4d7ff26ef9a607ab7f08fd68deded828cca3bb02 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:43:01 +0530 Subject: [PATCH 07/17] feat: get robots by user id --- server/src/routes/storage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/routes/storage.ts b/server/src/routes/storage.ts index 31124b87..f22ce28b 100644 --- a/server/src/routes/storage.ts +++ b/server/src/routes/storage.ts @@ -45,7 +45,7 @@ router.get('/recordings', requireSignIn, async (req, res) => { router.get('/recordings/:id', requireSignIn, async (req, res) => { try { const data = await Robot.findOne({ - where: { 'recording_meta.id': req.params.id }, + where: { 'recording_meta.id': req.params.id, userId: req.user.id }, raw: true } ); From 1e4ac016337e0f6eaf753316ccdf79f302294fe6 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:43:31 +0530 Subject: [PATCH 08/17] feat: del robot by user id --- server/src/routes/storage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/routes/storage.ts b/server/src/routes/storage.ts index f22ce28b..c08e05e4 100644 --- a/server/src/routes/storage.ts +++ b/server/src/routes/storage.ts @@ -62,7 +62,7 @@ router.get('/recordings/:id', requireSignIn, async (req, res) => { router.delete('/recordings/:id', requireSignIn, async (req, res) => { try { await Robot.destroy({ - where: { 'recording_meta.id': req.params.id } + where: { 'recording_meta.id': req.params.id, userId: req.user.id } }); return res.send(true); } catch (e) { From 00e21290f0ca91a431642195af2b613cc9b5990a Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:43:50 +0530 Subject: [PATCH 09/17] feat: get robot by user id --- server/src/routes/storage.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/routes/storage.ts b/server/src/routes/storage.ts index c08e05e4..c35287aa 100644 --- a/server/src/routes/storage.ts +++ b/server/src/routes/storage.ts @@ -107,7 +107,8 @@ router.put('/runs/:id', requireSignIn, async (req, res) => { try { const recording = await Robot.findOne({ where: { - 'recording_meta.id': req.params.id + 'recording_meta.id': req.params.id, + userId: req.user.id, }, raw: true }); From 4c5018eae0da01db613c6c1380965cc45e342c3e Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:44:19 +0530 Subject: [PATCH 10/17] feat: get robot by user id --- server/src/routes/storage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/routes/storage.ts b/server/src/routes/storage.ts index c35287aa..1ab3cf58 100644 --- a/server/src/routes/storage.ts +++ b/server/src/routes/storage.ts @@ -200,7 +200,7 @@ router.post('/runs/run/:id', requireSignIn, async (req, res) => { const plainRun = run.toJSON(); - const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId }, raw: true }); + const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId, userId: req.user.id }, raw: true }); if (!recording) { return res.status(404).send(false); } From adaf9d0247f16c82b2b7005b2cd6100443aff46b Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 20:59:49 +0530 Subject: [PATCH 11/17] feat: make userId optional --- server/src/models/Robot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/models/Robot.ts b/server/src/models/Robot.ts index 00294b34..e3a5cae4 100644 --- a/server/src/models/Robot.ts +++ b/server/src/models/Robot.ts @@ -19,7 +19,7 @@ interface RobotWorkflow { interface RobotAttributes { id: string; - userId: string; + userId?: string; recording_meta: RobotMeta; recording: RobotWorkflow; google_sheet_email?: string | null; From 4bf496fcd4f99161c75ba47e7cd7f0257b391433 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 21:03:42 +0530 Subject: [PATCH 12/17] fix: revert changes --- server/src/routes/storage.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/server/src/routes/storage.ts b/server/src/routes/storage.ts index 1ab3cf58..c684d6ea 100644 --- a/server/src/routes/storage.ts +++ b/server/src/routes/storage.ts @@ -29,9 +29,7 @@ router.all('/', requireSignIn, (req, res, next) => { */ router.get('/recordings', requireSignIn, async (req, res) => { try { - const data = await Robot.findAll({ - where: { userId: req.user.id }, - }); + const data = await Robot.findAll(); return res.send(data); } catch (e) { logger.log('info', 'Error while reading recordings'); @@ -45,7 +43,7 @@ router.get('/recordings', requireSignIn, async (req, res) => { router.get('/recordings/:id', requireSignIn, async (req, res) => { try { const data = await Robot.findOne({ - where: { 'recording_meta.id': req.params.id, userId: req.user.id }, + where: { 'recording_meta.id': req.params.id }, raw: true } ); @@ -62,7 +60,7 @@ router.get('/recordings/:id', requireSignIn, async (req, res) => { router.delete('/recordings/:id', requireSignIn, async (req, res) => { try { await Robot.destroy({ - where: { 'recording_meta.id': req.params.id, userId: req.user.id } + where: { 'recording_meta.id': req.params.id } }); return res.send(true); } catch (e) { @@ -107,8 +105,7 @@ router.put('/runs/:id', requireSignIn, async (req, res) => { try { const recording = await Robot.findOne({ where: { - 'recording_meta.id': req.params.id, - userId: req.user.id, + 'recording_meta.id': req.params.id }, raw: true }); @@ -200,7 +197,7 @@ router.post('/runs/run/:id', requireSignIn, async (req, res) => { const plainRun = run.toJSON(); - const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId, userId: req.user.id }, raw: true }); + const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId }, raw: true }); if (!recording) { return res.status(404).send(false); } From e885b1f535e9d3108ea36f347b05eb0fe6488374 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 22:31:58 +0530 Subject: [PATCH 13/17] fix: set userId integer --- server/src/models/Robot.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/models/Robot.ts b/server/src/models/Robot.ts index e3a5cae4..d2dbca67 100644 --- a/server/src/models/Robot.ts +++ b/server/src/models/Robot.ts @@ -19,7 +19,7 @@ interface RobotWorkflow { interface RobotAttributes { id: string; - userId?: string; + userId?: number; recording_meta: RobotMeta; recording: RobotWorkflow; google_sheet_email?: string | null; @@ -33,7 +33,7 @@ interface RobotCreationAttributes extends Optional { } class Robot extends Model implements RobotAttributes { public id!: string; - public userId!: string; + public userId!: number; public recording_meta!: RobotMeta; public recording!: RobotWorkflow; public google_sheet_email!: string | null; @@ -51,7 +51,7 @@ Robot.init( primaryKey: true, }, userId: { - type: DataTypes.UUID, + type: DataTypes.INTEGER, allowNull: false, }, recording_meta: { From 9a439f70bc0de86040d3aedafc09c186643e2563 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 23:07:28 +0530 Subject: [PATCH 14/17] feat: recieve userId from socket --- .../src/workflow-management/classes/Generator.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/server/src/workflow-management/classes/Generator.ts b/server/src/workflow-management/classes/Generator.ts index 0516703b..6d29b5fd 100644 --- a/server/src/workflow-management/classes/Generator.ts +++ b/server/src/workflow-management/classes/Generator.ts @@ -126,10 +126,12 @@ export class WorkflowGenerator { * @private */ private registerEventHandlers = (socket: Socket) => { - socket.on('save', async (fileName: string) => { - logger.log('debug', `Saving workflow ${fileName}`); - await this.saveNewWorkflow(fileName) - }); + socket.on('save', (data) => { + console.log('Received data:', data); + const { fileName, userId } = data; + logger.log('debug', `Saving workflow ${fileName} for user ID ${userId}`); + this.saveNewWorkflow(fileName, userId); + }); socket.on('new-recording', () => this.workflowRecord = { workflow: [], }); @@ -477,7 +479,7 @@ export class WorkflowGenerator { * @param fileName The name of the file. * @returns {Promise} */ - public saveNewWorkflow = async (fileName: string) => { + public saveNewWorkflow = async (fileName: string, userId: number) => { const recording = this.optimizeWorkflow(this.workflowRecord); try { this.recordingMeta = { @@ -489,6 +491,7 @@ export class WorkflowGenerator { params: this.getParams() || [], } const robot = await Robot.create({ + userId, recording_meta: this.recordingMeta, recording: recording, }); @@ -497,7 +500,7 @@ export class WorkflowGenerator { } catch (e) { const { message } = e as Error; - logger.log('warn', `Cannot save the file to the local file system`) + logger.log('warn', `Cannot save the file to the local file system ${e}`) } this.socket.emit('fileSaved'); } From 9dae29b7d9cfacf295d1782a2f0aa5d541633c06 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 23:14:22 +0530 Subject: [PATCH 15/17] feat: pass userId from socket --- src/components/molecules/SaveRecording.tsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/components/molecules/SaveRecording.tsx b/src/components/molecules/SaveRecording.tsx index 3e82048e..6751bceb 100644 --- a/src/components/molecules/SaveRecording.tsx +++ b/src/components/molecules/SaveRecording.tsx @@ -1,8 +1,9 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useState, useContext } from 'react'; import { IconButton, Button, Box, LinearProgress, Tooltip } from "@mui/material"; import { GenericModal } from "../atoms/GenericModal"; import { stopRecording } from "../../api/recording"; import { useGlobalInfoStore } from "../../context/globalInfo"; +import { AuthContext } from '../../context/auth'; import { useSocketStore } from "../../context/socket"; import { TextField, Typography } from "@mui/material"; import { WarningText } from "../atoms/texts"; @@ -24,6 +25,8 @@ export const SaveRecording = ({ fileName }: SaveRecordingProps) => { const { browserId, setBrowserId, notify, recordings } = useGlobalInfoStore(); const { socket } = useSocketStore(); + const { state, dispatch } = useContext(AuthContext); + const { user } = state; const navigate = useNavigate(); const handleChangeOfTitle = (event: React.ChangeEvent) => { @@ -56,9 +59,16 @@ export const SaveRecording = ({ fileName }: SaveRecordingProps) => { // notifies backed to save the recording in progress, // releases resources and changes the view for main page by clearing the global browserId const saveRecording = async () => { - socket?.emit('save', recordingName) - setWaitingForSave(true); - } + if (user) { + const payload = { fileName: recordingName, userId: user.id }; + console.log('Emitting save with payload:', payload); + socket?.emit('save', payload); + setWaitingForSave(true); + console.log(`Saving the recording as ${recordingName} for userId ${user.id}`); + } else { + console.error('User not logged in. Cannot save recording.'); + } +}; useEffect(() => { socket?.on('fileSaved', exitRecording); From 04dd1a7f29157ff43fd931bc0cd632cf1c908de0 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 23:14:39 +0530 Subject: [PATCH 16/17] chore: lint --- src/components/molecules/SaveRecording.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/components/molecules/SaveRecording.tsx b/src/components/molecules/SaveRecording.tsx index 6751bceb..2ee973b2 100644 --- a/src/components/molecules/SaveRecording.tsx +++ b/src/components/molecules/SaveRecording.tsx @@ -60,15 +60,15 @@ export const SaveRecording = ({ fileName }: SaveRecordingProps) => { // releases resources and changes the view for main page by clearing the global browserId const saveRecording = async () => { if (user) { - const payload = { fileName: recordingName, userId: user.id }; - console.log('Emitting save with payload:', payload); - socket?.emit('save', payload); - setWaitingForSave(true); - console.log(`Saving the recording as ${recordingName} for userId ${user.id}`); + const payload = { fileName: recordingName, userId: user.id }; + console.log('Emitting save with payload:', payload); + socket?.emit('save', payload); + setWaitingForSave(true); + console.log(`Saving the recording as ${recordingName} for userId ${user.id}`); } else { - console.error('User not logged in. Cannot save recording.'); + console.error('User not logged in. Cannot save recording.'); } -}; + }; useEffect(() => { socket?.on('fileSaved', exitRecording); From 8c7101daf1858cea09ad6e882d4aff7c90588c63 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 21 Oct 2024 23:15:04 +0530 Subject: [PATCH 17/17] chore: remove console.log --- src/components/molecules/SaveRecording.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/molecules/SaveRecording.tsx b/src/components/molecules/SaveRecording.tsx index 2ee973b2..e9efe532 100644 --- a/src/components/molecules/SaveRecording.tsx +++ b/src/components/molecules/SaveRecording.tsx @@ -61,7 +61,6 @@ export const SaveRecording = ({ fileName }: SaveRecordingProps) => { const saveRecording = async () => { if (user) { const payload = { fileName: recordingName, userId: user.id }; - console.log('Emitting save with payload:', payload); socket?.emit('save', payload); setWaitingForSave(true); console.log(`Saving the recording as ${recordingName} for userId ${user.id}`);