kf save almost good

This commit is contained in:
STEINNI
2025-10-26 20:03:39 +00:00
parent 105aeca3fb
commit 793efb4d72
3 changed files with 62 additions and 19 deletions
+3
View File
@@ -1,14 +1,17 @@
import { methods as userMethods, mappings as userMappings } from './users.js' import { methods as userMethods, mappings as userMappings } from './users.js'
import { methods as agentMethods, mappings as agentMappings } from './agents.js' import { methods as agentMethods, mappings as agentMappings } from './agents.js'
import { methods as keyframesMethods, mappings as keyframesMappings } from './keyframes.js'
export const apiMappings = [ export const apiMappings = [
...userMappings, ...userMappings,
...agentMappings, ...agentMappings,
...keyframesMappings,
] ]
export const apiMethods = { export const apiMethods = {
...userMethods, ...userMethods,
...agentMethods, ...agentMethods,
...keyframesMethods,
} }
+54 -14
View File
@@ -1,39 +1,70 @@
import { authGuard } from '../authGuard.js' import { authGuard } from '../authGuard.js'
import { uuidv7 } from "uuidv7" import { uuidv7obj, UUID } from "uuidv7"
export const mappings = [ export const mappings = [
{ method: 'put', url:'/keyframes/:kfid', handler: 'saveKeyframe', middlewares: [authGuard]},
{ method: 'put', url:'/keyframes', handler: 'createKeyframe', middlewares: [authGuard]}, { method: 'put', url:'/keyframes', handler: 'createKeyframe', middlewares: [authGuard]},
{ method: 'put', url:'/keyframes/:kfid', handler: 'renameKeyframe', middlewares: [authGuard]},
{ method: 'put', url:'/keyframes/:kfid/agents', handler: 'saveKeyframeAgents', middlewares: [authGuard]},
] ]
export const methods = { export const methods = {
async createKeyframe(req, res) { async createKeyframe(req, res) {
const[isOk, payload, errors] = this.utils.validateMapObject(req.body, const[isOk, payload, errors] = this.utils.validateMapObject(req.body,
{ {
'kfName': ((val, obj) => ( (typeof(val)=='string') && (new RegExp(/^\w{5,50}$/).test(val))) ), 'kfName': ((val, obj) => ( (typeof(val)=='string') && (new RegExp(/^[\w\s]{5,50}$/).test(val))) ),
'prevKFId': ((val, obj) => ( (typeof(val)=='undefined') || (this.utils.isValidUUIDV7(val)) ) ), 'prevKfId': ((val, obj) => ( (typeof(val)=='undefined') || (this.utils.isValidUUIDV7(val)) ) ),
}, },
{ {
'kfName': 'kfName', 'kfName': 'kfName',
'prevKFId': 'prevKFId', 'prevKfId': 'prevKfId',
}) })
if(!isOk){ if(!isOk){
this.err(req, res,`Cannot create this keyframe ! `, `Validations errors for creating KF : ${errors.join(', ')}`, 400) this.err(req, res,`Cannot create this keyframe ! `, `Validations errors for creating KF : ${errors.join(', ')}`, 400)
return return
} }
const kfid = uuidv7() const newKfId = uuidv7obj()
await this.db.execute(` await this.db.execute(`
INSERT INTO edited_keyframes INSERT INTO p42SIM.edited_keyframes
(ekf_uuid, ekf_name, ekf_prev_uuid) (ekf_uuid, ekf_name, ekf_prev_uuid)
VALUES (?, ?, ?)`, VALUES (?, ?, ?)`,
[kfid, payload.kfName, prevKFId || null]) [ Buffer.from(newKfId.bytes),
payload.kfName,
payload.prevKfId ? UUID.parse(payload.prevKfId).bytes : null
])
this.ok(req, res, { kfid: kfid }) this.ok(req, res, { kfId: newKfId.toString() })
}, },
async saveKeyframe(req, res) { async renameKeyframe(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.validateMapObject(req.body,
{
'kfName': ((val, obj) => ( (typeof(val)=='string') && (new RegExp(/^\w{5,50}$/).test(val))) ),
},
{
'kfName': 'kfName',
})
if(!isOk){
this.err(req, res,`Cannot rename this keyframe ! `, `Validations errors for renaming KF : ${errors.join(', ')}`, 400)
return
}
await this.db.execute(`
UPDATE p42SIM.edited_keyframes
SET ekf_name = ?
WHERE (ekf_uuid = ?)`,
[payload.kfName, UUID.parse(kfid).bytes])
this.ok(req, res, { kfId: kfid })
},
async saveKeyframeAgents(req, res) {
const kfid = req.params.kfid const kfid = req.params.kfid
if(!this.utils.isValidUUIDV7(kfid)) { if(!this.utils.isValidUUIDV7(kfid)) {
this.err(req, res,`Cannot create Keyframe ! `, `Invalid Keyframe ID !`, 400) this.err(req, res,`Cannot create Keyframe ! `, `Invalid Keyframe ID !`, 400)
@@ -56,17 +87,26 @@ export const methods = {
return return
} }
await this.db.execute(`DELETE FROM edited_kf_store WHERE ekfs_ekf_uuid = ?`, [kfid]) await this.db.execute(`DELETE FROM p42SIM.edited_kf_store WHERE ekfs_ekf_uuid = ?`, [UUID.parse(kfid).bytes])
for(const entry of payload){ for(const entry of payload){
await this.db.execute(` await this.db.execute(`
INSERT INTO edited_kf_store INSERT INTO p42SIM.edited_kf_store
(ekfs_ekf_uuid, ekfs_agent_id, ekfs_store_value, ekfs_gps_values) (ekfs_ekf_uuid, ekfs_agent_id, ekfs_store_value, ekfs_gps_values)
VALUES (?, ?, ?, ?)`, VALUES (?, ?, ?, ?)`,
[kfid, entry.aid, entry.storeValues, entry.gpsValues]) [ UUID.parse(kfid).bytes,
UUID.parse(entry.aid).bytes,
entry.storeValues,
entry.gpsValues
])
} }
this.ok(req, res, { kfid: kfid }) this.ok(req, res, { kfId: kfid })
}, },
/*
REMEMBER READ BACK UUIDS from BINARY :
const uuid = uuidStringify(rows[0].ekf_uuid)
*/
} }
+4 -4
View File
@@ -13,13 +13,13 @@ export class Utils {
} }
isValidUUIDV4(val) { isValidUUIDV4(val) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i const uuidV4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
return(uuidRegex.test(val)) return(uuidV4Regex.test(val))
} }
isValidUUIDV7(val) { isValidUUIDV7(val) {
const reUUIDv7 = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i const uuidV7Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
return(uuidRegex.test(val)) return(uuidV7Regex.test(val))
} }
isIterable(obj) { isIterable(obj) {