removed metadata from condition check model
This commit is contained in:
70
backend/migrations/20241124000006-create-condition-checks.js
Normal file
70
backend/migrations/20241124000006-create-condition-checks.js
Normal file
@@ -0,0 +1,70 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.createTable("ConditionChecks", {
|
||||
id: {
|
||||
type: Sequelize.UUID,
|
||||
defaultValue: Sequelize.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
rentalId: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: "Rentals",
|
||||
key: "id",
|
||||
},
|
||||
onUpdate: "CASCADE",
|
||||
onDelete: "CASCADE",
|
||||
},
|
||||
submittedBy: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: "Users",
|
||||
key: "id",
|
||||
},
|
||||
onUpdate: "CASCADE",
|
||||
onDelete: "CASCADE",
|
||||
},
|
||||
checkType: {
|
||||
type: Sequelize.ENUM(
|
||||
"pre_rental_owner",
|
||||
"rental_start_renter",
|
||||
"rental_end_renter",
|
||||
"post_rental_owner"
|
||||
),
|
||||
allowNull: false,
|
||||
},
|
||||
photos: {
|
||||
type: Sequelize.ARRAY(Sequelize.STRING),
|
||||
defaultValue: [],
|
||||
},
|
||||
notes: {
|
||||
type: Sequelize.TEXT,
|
||||
},
|
||||
submittedAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.NOW,
|
||||
},
|
||||
createdAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
});
|
||||
|
||||
// Add indexes
|
||||
await queryInterface.addIndex("ConditionChecks", ["rentalId"]);
|
||||
await queryInterface.addIndex("ConditionChecks", ["submittedBy"]);
|
||||
},
|
||||
|
||||
down: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.dropTable("ConditionChecks");
|
||||
},
|
||||
};
|
||||
@@ -44,10 +44,6 @@ const ConditionCheck = sequelize.define("ConditionCheck", {
|
||||
allowNull: false,
|
||||
defaultValue: DataTypes.NOW,
|
||||
},
|
||||
metadata: {
|
||||
type: DataTypes.JSONB,
|
||||
defaultValue: {},
|
||||
},
|
||||
});
|
||||
|
||||
module.exports = ConditionCheck;
|
||||
@@ -37,20 +37,12 @@ router.post(
|
||||
// Get uploaded file paths
|
||||
const photos = req.files ? req.files.map((file) => file.path) : [];
|
||||
|
||||
// Extract metadata from request
|
||||
const metadata = {
|
||||
userAgent: req.get("User-Agent"),
|
||||
ipAddress: req.ip,
|
||||
deviceType: req.get("X-Device-Type") || "web",
|
||||
};
|
||||
|
||||
const conditionCheck = await ConditionCheckService.submitConditionCheck(
|
||||
rentalId,
|
||||
checkType,
|
||||
userId,
|
||||
photos,
|
||||
notes,
|
||||
metadata
|
||||
notes
|
||||
);
|
||||
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
|
||||
@@ -118,7 +118,6 @@ class ConditionCheckService {
|
||||
* @param {string} userId - User submitting the check
|
||||
* @param {Array} photos - Array of photo URLs
|
||||
* @param {string} notes - Optional notes
|
||||
* @param {Object} metadata - Additional metadata (device info, location, etc.)
|
||||
* @returns {Object} - Created condition check
|
||||
*/
|
||||
static async submitConditionCheck(
|
||||
@@ -126,8 +125,7 @@ class ConditionCheckService {
|
||||
checkType,
|
||||
userId,
|
||||
photos = [],
|
||||
notes = null,
|
||||
metadata = {}
|
||||
notes = null
|
||||
) {
|
||||
// Validate the check
|
||||
const validation = await this.validateConditionCheck(
|
||||
@@ -145,22 +143,12 @@ class ConditionCheckService {
|
||||
throw new Error("Maximum 20 photos allowed per condition check");
|
||||
}
|
||||
|
||||
// Add timestamp and user agent to metadata
|
||||
const enrichedMetadata = {
|
||||
...metadata,
|
||||
submittedAt: new Date().toISOString(),
|
||||
userAgent: metadata.userAgent || "Unknown",
|
||||
ipAddress: metadata.ipAddress || "Unknown",
|
||||
deviceType: metadata.deviceType || "Unknown",
|
||||
};
|
||||
|
||||
const conditionCheck = await ConditionCheck.create({
|
||||
rentalId,
|
||||
checkType,
|
||||
submittedBy: userId,
|
||||
photos,
|
||||
notes,
|
||||
metadata: enrichedMetadata,
|
||||
});
|
||||
|
||||
return conditionCheck;
|
||||
|
||||
@@ -51,7 +51,6 @@ describe('ConditionCheckService', () => {
|
||||
submittedBy: 'renter-789',
|
||||
photos: mockPhotos,
|
||||
notes: 'Item received in good condition',
|
||||
metadata: expect.any(Object)
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user