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,
|
allowNull: false,
|
||||||
defaultValue: DataTypes.NOW,
|
defaultValue: DataTypes.NOW,
|
||||||
},
|
},
|
||||||
metadata: {
|
|
||||||
type: DataTypes.JSONB,
|
|
||||||
defaultValue: {},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = ConditionCheck;
|
module.exports = ConditionCheck;
|
||||||
@@ -37,20 +37,12 @@ router.post(
|
|||||||
// Get uploaded file paths
|
// Get uploaded file paths
|
||||||
const photos = req.files ? req.files.map((file) => file.path) : [];
|
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(
|
const conditionCheck = await ConditionCheckService.submitConditionCheck(
|
||||||
rentalId,
|
rentalId,
|
||||||
checkType,
|
checkType,
|
||||||
userId,
|
userId,
|
||||||
photos,
|
photos,
|
||||||
notes,
|
notes
|
||||||
metadata
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const reqLogger = logger.withRequestId(req.id);
|
const reqLogger = logger.withRequestId(req.id);
|
||||||
|
|||||||
@@ -118,7 +118,6 @@ class ConditionCheckService {
|
|||||||
* @param {string} userId - User submitting the check
|
* @param {string} userId - User submitting the check
|
||||||
* @param {Array} photos - Array of photo URLs
|
* @param {Array} photos - Array of photo URLs
|
||||||
* @param {string} notes - Optional notes
|
* @param {string} notes - Optional notes
|
||||||
* @param {Object} metadata - Additional metadata (device info, location, etc.)
|
|
||||||
* @returns {Object} - Created condition check
|
* @returns {Object} - Created condition check
|
||||||
*/
|
*/
|
||||||
static async submitConditionCheck(
|
static async submitConditionCheck(
|
||||||
@@ -126,8 +125,7 @@ class ConditionCheckService {
|
|||||||
checkType,
|
checkType,
|
||||||
userId,
|
userId,
|
||||||
photos = [],
|
photos = [],
|
||||||
notes = null,
|
notes = null
|
||||||
metadata = {}
|
|
||||||
) {
|
) {
|
||||||
// Validate the check
|
// Validate the check
|
||||||
const validation = await this.validateConditionCheck(
|
const validation = await this.validateConditionCheck(
|
||||||
@@ -145,22 +143,12 @@ class ConditionCheckService {
|
|||||||
throw new Error("Maximum 20 photos allowed per condition check");
|
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({
|
const conditionCheck = await ConditionCheck.create({
|
||||||
rentalId,
|
rentalId,
|
||||||
checkType,
|
checkType,
|
||||||
submittedBy: userId,
|
submittedBy: userId,
|
||||||
photos,
|
photos,
|
||||||
notes,
|
notes,
|
||||||
metadata: enrichedMetadata,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return conditionCheck;
|
return conditionCheck;
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ describe('ConditionCheckService', () => {
|
|||||||
submittedBy: 'renter-789',
|
submittedBy: 'renter-789',
|
||||||
photos: mockPhotos,
|
photos: mockPhotos,
|
||||||
notes: 'Item received in good condition',
|
notes: 'Item received in good condition',
|
||||||
metadata: expect.any(Object)
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -194,7 +194,6 @@ export interface ConditionCheck {
|
|||||||
notes?: string;
|
notes?: string;
|
||||||
submittedBy: string;
|
submittedBy: string;
|
||||||
submittedAt: string;
|
submittedAt: string;
|
||||||
metadata: any;
|
|
||||||
submittedByUser?: User;
|
submittedByUser?: User;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user