import { authGuard } from '../authGuard.js' import { uuidv7 } from "uuidv7" export const mappings = [ { method: 'put', url:'/keyframes/:kfid', handler: 'saveKeyframe', middlewares: [authGuard]}, { method: 'put', url:'/keyframes', handler: 'createKeyframe', middlewares: [authGuard]}, ] export const methods = { async createKeyframe(req, res) { const[isOk, payload, errors] = this.utils.validateMapObject(req.body, { 'kfName': ((val, obj) => ( (typeof(val)=='string') && (new RegExp(/^\w{5,50}$/).test(val))) ), 'prevKFId': ((val, obj) => ( (typeof(val)=='undefined') || (this.utils.isValidUUIDV7(val)) ) ), }, { 'kfName': 'kfName', 'prevKFId': 'prevKFId', }) if(!isOk){ this.err(req, res,`Cannot create this keyframe ! `, `Validations errors for creating KF : ${errors.join(', ')}`, 400) return } const kfid = uuidv7() await this.db.execute(` INSERT INTO edited_keyframes (ekf_uuid, ekf_name, ekf_prev_uuid) VALUES (?, ?, ?)`, [kfid, payload.kfName, prevKFId || null]) this.ok(req, res, { kfid: kfid }) }, async saveKeyframe(req, res) { const kfid = req.params.kfid if(!this.utils.isValidUUIDV7(kfid)) { this.err(req, res,`Cannot create Keyframe ! `, `Invalid Keyframe ID !`, 400) return } const[isOk, payload, errors] = this.utils.validateMapArray(req.body, { 'aid': ((val, obj) => (this.utils.isValidUUIDV7(val)) ), 'storeValues': ((val, obj) => (typeof(val)==='object') ), 'gpsValues': ((val, obj) => (typeof(val)==='object') ), }, { 'aid': 'aid', 'storeValues': 'storeValues', 'gpsValues': 'gpsValues', } ) if(!isOk){ this.err(req, res,`Cannot save this keyframe ! `, `Validations errors for saving KF : ${errors.join(', ')}`, 400) return } await this.db.execute(`DELETE FROM edited_kf_store WHERE ekfs_ekf_uuid = ?`, [kfid]) for(const entry of payload){ await this.db.execute(` INSERT INTO edited_kf_store (ekfs_ekf_uuid, ekfs_agent_id, ekfs_store_value, ekfs_gps_values) VALUES (?, ?, ?, ?)`, [kfid, entry.aid, entry.storeValues, entry.gpsValues]) } this.ok(req, res, { kfid: kfid }) }, }