|
| 1 | +const { createSchemaApiKeySchema } = require('../utils/input.validation'); |
| 2 | +const Project = require('../models/Project'); |
| 3 | +const { v4: uuidv4 } = require('uuid'); |
| 4 | +const { deleteProjectById, setProjectById, deleteProjectByApiKeyCache } = require('../services/redisCaching'); |
| 5 | +const { z } = require('zod'); |
| 6 | + |
| 7 | +module.exports.checkSchema = async (req, res) => { |
| 8 | + try { |
| 9 | + const { collectionName } = req.params; |
| 10 | + const project = req.project; |
| 11 | + |
| 12 | + if (!project) return res.status(401).json({ error: "Project missing from request." }); |
| 13 | + |
| 14 | + const collectionConfig = project.collections.find(c => c.name === collectionName); |
| 15 | + |
| 16 | + if (!collectionConfig) { |
| 17 | + return res.status(404).json({ error: "Schema/Collection not found" }); |
| 18 | + } |
| 19 | + |
| 20 | + res.status(200).json({ message: "Schema exists", collection: collectionConfig }); |
| 21 | + } catch (err) { |
| 22 | + res.status(500).json({ error: err.message }); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +module.exports.createSchema = async (req, res) => { |
| 27 | + try { |
| 28 | + const { name, fields } = createSchemaApiKeySchema.parse(req.body); |
| 29 | + let project = req.project; // From verifyApiKey |
| 30 | + |
| 31 | + // Need the full document to call .save(), req.project from cache might be a lean object. |
| 32 | + // It's safer to fetch the mongoose model instance to modify it. |
| 33 | + const projectId = project._id; |
| 34 | + const fullProject = await Project.findById(projectId); |
| 35 | + |
| 36 | + if (!fullProject) return res.status(404).json({ error: 'Project not found' }); |
| 37 | + |
| 38 | + const exists = fullProject.collections.find(c => c.name === name); |
| 39 | + if (exists) return res.status(400).json({ error: 'Collection/Schema already exists' }); |
| 40 | + |
| 41 | + if (!fullProject.jwtSecret) fullProject.jwtSecret = uuidv4(); |
| 42 | + |
| 43 | + // Convert the incoming API payload (name, type, required) into the UrBackend schema format (key, type, required) |
| 44 | + const transformedFields = (fields || []).map(f => { |
| 45 | + // Capitalize first letter of type mapping "string" -> "String" requirement |
| 46 | + const mappedType = f.type.charAt(0).toUpperCase() + f.type.slice(1).toLowerCase(); |
| 47 | + return { |
| 48 | + key: f.name, |
| 49 | + type: mappedType, |
| 50 | + required: f.required === true |
| 51 | + }; |
| 52 | + }); |
| 53 | + |
| 54 | + fullProject.collections.push({ name: name, model: transformedFields }); |
| 55 | + await fullProject.save(); |
| 56 | + |
| 57 | + // Clear redis cache |
| 58 | + await deleteProjectById(projectId.toString()); |
| 59 | + await setProjectById(projectId.toString(), fullProject); |
| 60 | + await deleteProjectByApiKeyCache(fullProject.apiKey); // In case it was cached by hashed key |
| 61 | + // NOTE: req.hashedApiKey is available from middleware if we strictly want that. |
| 62 | + if (req.hashedApiKey) { |
| 63 | + await deleteProjectByApiKeyCache(req.hashedApiKey); |
| 64 | + } |
| 65 | + |
| 66 | + const projectObj = fullProject.toObject(); |
| 67 | + delete projectObj.apiKey; |
| 68 | + delete projectObj.jwtSecret; |
| 69 | + |
| 70 | + res.status(201).json({ message: "Schema created successfully", project: projectObj }); |
| 71 | + } catch (err) { |
| 72 | + if (err instanceof z.ZodError) return res.status(400).json({ error: err.errors }); |
| 73 | + res.status(500).json({ error: err.message }); |
| 74 | + } |
| 75 | +}; |
0 commit comments