Skip to content

Commit

Permalink
Merge pull request Open-Cap-Stack#65 from anushac14/feature/document
Browse files Browse the repository at this point in the history
Document Green Tests (Updated Data Model)
  • Loading branch information
urbantech authored Aug 15, 2024
2 parents 1db8008 + 74a6869 commit f8a8fcc
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 148 deletions.
5 changes: 3 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// app.js
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
Expand All @@ -8,7 +9,7 @@ const stakeholderRoutes = require('./routes/stakeholderRoutes');
const documentRoutes = require('./routes/documentRoutes');
const fundraisingRoundRoutes = require('./routes/fundraisingRoundRoutes');
const equityPlanRoutes = require('./routes/equityPlanRoutes');
const documentEmbeddingRoutes = require('./routes/documentEmbeddingRoutes');
const documentEmbeddingRoutes = require('./routes/documentEmbeddingRoutes');
const employeeRoutes = require('./routes/employeeRoutes');
const activityRoutes = require('./routes/activityRoutes');
const investmentRoutes = require('./routes/investmentTrackerRoutes');
Expand All @@ -29,6 +30,6 @@ app.use('/api/employees', employeeRoutes);
app.use('/api/activities', activityRoutes);
app.use('/api/investments', investmentRoutes);
app.use('/api/admins', adminRoutes);
app.use('/api/documentAccesses', documentAccessRoutes);
app.use('/api/documentAccesses', documentAccessRoutes);

module.exports = app;
61 changes: 30 additions & 31 deletions controllers/documentController.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
const DocumentEmbedding = require('../models/DocumentEmbeddingModel');
const Document = require('../models/documentModel');

exports.createDocumentEmbedding = async (req, res) => {
// Create a new document
exports.createDocument = async (req, res) => {
try {
const newEmbedding = new DocumentEmbedding(req.body);
const savedEmbedding = await newEmbedding.save();
res.status(201).json(savedEmbedding);
const document = new Document(req.body);
const savedDocument = await document.save();
res.status(201).json(savedDocument);
} catch (error) {
res.status(400).json({ error: error.message });
res.status(400).json({ message: error.message });
}
};

exports.getDocumentEmbeddings = async (req, res) => {
// Get all documents
exports.getDocuments = async (req, res) => {
try {
const embeddings = await DocumentEmbedding.find();
res.status(200).json(embeddings);
const documents = await Document.find();
res.status(200).json(documents);
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ message: error.message });
}
};

exports.getDocumentEmbeddingById = async (req, res) => {
// Get a document by ID
exports.getDocumentById = async (req, res) => {
try {
const embedding = await DocumentEmbedding.findById(req.params.id);
if (!embedding) {
return res.status(404).json({ message: 'Document embedding not found' });
}
res.status(200).json(embedding);
const document = await Document.findById(req.params.id);
if (!document) return res.status(404).json({ message: 'Document not found' });
res.status(200).json(document);
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ message: error.message });
}
};

exports.updateDocumentEmbedding = async (req, res) => {
// Update a document by ID
exports.updateDocumentById = async (req, res) => {
try {
const embedding = await DocumentEmbedding.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!embedding) {
return res.status(404).json({ message: 'Document embedding not found' });
}
res.status(200).json(embedding);
const document = await Document.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!document) return res.status(404).json({ message: 'Document not found' });
res.status(200).json(document);
} catch (error) {
res.status(400).json({ error: error.message });
res.status(400).json({ message: error.message });
}
};

exports.deleteDocumentEmbedding = async (req, res) => {
// Delete a document by ID
exports.deleteDocumentById = async (req, res) => {
try {
const embedding = await DocumentEmbedding.findByIdAndDelete(req.params.id);
if (!embedding) {
return res.status(404).json({ message: 'Document embedding not found' });
}
res.status(200).json({ message: 'Document embedding deleted' });
const document = await Document.findByIdAndDelete(req.params.id);
if (!document) return res.status(404).json({ message: 'Document not found' });
res.status(200).json({ message: 'Document deleted' });
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ message: error.message });
}
};
13 changes: 0 additions & 13 deletions models/Document.js

This file was deleted.

19 changes: 19 additions & 0 deletions models/documentModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// models/documentModel.js
const mongoose = require('mongoose');

const documentSchema = new mongoose.Schema({
documentId: { type: String, unique: true, required: true },
name: { type: String, required: true },
metadata: { type: Object },
uploadedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
path: { type: String, required: true },
title: { type: String, required: true },
content: { type: String, required: true },
DocumentType: { type: String, enum: ['Legal', 'Financial', 'Other'], required: true },
FileType: { type: String, enum: ['PDF', 'DOCX', 'TXT'], required: true },
Versioning: { type: String },
AccessControl: { type: Object },
LegalSignificance: { type: String }
}, { timestamps: true });

module.exports = mongoose.model('Document', documentSchema);
15 changes: 6 additions & 9 deletions routes/documentRoutes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const express = require('express');
const router = express.Router();
const { createDocument, getAllDocuments, getDocumentById, updateDocumentById, deleteDocumentById } = require('../controllers/documentController');
const documentController = require('../controllers/documentController');

// Existing routes
router.get('/', getAllDocuments);
router.post('/', createDocument);

// Add these routes for update and delete
router.get('/:id', getDocumentById); // Retrieve a document by ID
router.put('/:id', updateDocumentById); // Update a document by ID
router.delete('/:id', deleteDocumentById); // Delete a document by ID
router.post('/documents', documentController.createDocument);
router.get('/documents', documentController.getDocuments);
router.get('/documents/:id', documentController.getDocumentById);
router.put('/documents/:id', documentController.updateDocumentById);
router.delete('/documents/:id', documentController.deleteDocumentById);

module.exports = router;
34 changes: 0 additions & 34 deletions test/document.model.test.js

This file was deleted.

101 changes: 101 additions & 0 deletions test/documentController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const request = require('supertest');
const express = require('express');
const mongoose = require('mongoose');
const Document = require('../models/documentModel');
const { connectDB, disconnectDB } = require('../db');

const app = express();
app.use(express.json());
app.use('/api', require('../routes/documentRoutes'));

beforeAll(async () => {
await connectDB();
});

afterAll(async () => {
await mongoose.connection.db.dropDatabase();
await mongoose.connection.close();
});

describe('Document Controller', () => {
it('should create a new document', async () => {
const response = await request(app)
.post('/api/documents')
.send({
documentId: 'doc123',
name: 'Sample Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/path/to/document',
title: 'Sample Title',
content: 'Sample Content',
DocumentType: 'Legal',
FileType: 'PDF',
Versioning: '1.0'
});
expect(response.statusCode).toBe(201);
expect(response.body.name).toBe('Sample Document');
});

it('should get all documents', async () => {
const response = await request(app).get('/api/documents');
expect(response.statusCode).toBe(200);
expect(response.body).toBeInstanceOf(Array);
});

it('should get a document by ID', async () => {
const newDocument = new Document({
documentId: 'doc124',
name: 'Another Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/path/to/another/document',
title: 'Another Title',
content: 'Another Content',
DocumentType: 'Financial',
FileType: 'DOCX'
});
const savedDocument = await newDocument.save();

const response = await request(app).get(`/api/documents/${savedDocument._id}`);
expect(response.statusCode).toBe(200);
expect(response.body.name).toBe('Another Document');
});

it('should update a document by ID', async () => {
const newDocument = new Document({
documentId: 'doc125',
name: 'Update Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/path/to/update/document',
title: 'Update Title',
content: 'Update Content',
DocumentType: 'Legal',
FileType: 'PDF'
});
const savedDocument = await newDocument.save();

const response = await request(app)
.put(`/api/documents/${savedDocument._id}`)
.send({ name: 'Updated Document' });

expect(response.statusCode).toBe(200);
expect(response.body.name).toBe('Updated Document');
});

it('should delete a document by ID', async () => {
const newDocument = new Document({
documentId: 'doc126',
name: 'Delete Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/path/to/delete/document',
title: 'Delete Title',
content: 'Delete Content',
DocumentType: 'Other',
FileType: 'TXT'
});
const savedDocument = await newDocument.save();

const response = await request(app).delete(`/api/documents/${savedDocument._id}`);
expect(response.statusCode).toBe(200);
expect(response.body.message).toBe('Document deleted');
});
});
89 changes: 89 additions & 0 deletions test/documentModel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const mongoose = require('mongoose');
const Document = require('../models/documentModel');
const { connectDB, disconnectDB } = require('../db');

beforeAll(async () => {
await connectDB();
});

afterAll(async () => {
await mongoose.connection.db.dropDatabase();
await mongoose.connection.close();
});

describe('Document Model', () => {
it('should create a new document', async () => {
const document = new Document({
documentId: 'doc001',
name: 'Test Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/test/path',
title: 'Test Title',
content: 'Test Content',
DocumentType: 'Legal',
FileType: 'PDF',
Versioning: '1.0'
});

const savedDocument = await document.save();
expect(savedDocument._id).toBeDefined();
expect(savedDocument.name).toBe('Test Document');
});

it('should retrieve a document by ID', async () => {
const document = new Document({
documentId: 'doc002',
name: 'Retrieve Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/test/retrieve',
title: 'Retrieve Title',
content: 'Retrieve Content',
DocumentType: 'Financial',
FileType: 'DOCX'
});

const savedDocument = await document.save();
const foundDocument = await Document.findById(savedDocument._id);

expect(foundDocument).toBeDefined();
expect(foundDocument.name).toBe('Retrieve Document');
});

it('should update a document by ID', async () => {
const document = new Document({
documentId: 'doc003',
name: 'Update Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/test/update',
title: 'Update Title',
content: 'Update Content',
DocumentType: 'Legal',
FileType: 'PDF'
});

const savedDocument = await document.save();
savedDocument.name = 'Updated Document';
const updatedDocument = await savedDocument.save();

expect(updatedDocument.name).toBe('Updated Document');
});

it('should delete a document by ID', async () => {
const document = new Document({
documentId: 'doc004',
name: 'Delete Document',
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId
path: '/test/delete',
title: 'Delete Title',
content: 'Delete Content',
DocumentType: 'Other',
FileType: 'TXT'
});

const savedDocument = await document.save();
await Document.findByIdAndDelete(savedDocument._id);

const deletedDocument = await Document.findById(savedDocument._id);
expect(deletedDocument).toBeNull();
});
});
Loading

0 comments on commit f8a8fcc

Please sign in to comment.