feat: enhance validation and serialization with fiscal code checks and JSON schema support
This commit is contained in:
@@ -12,6 +12,9 @@ namespace visione::services
|
||||
{
|
||||
namespace
|
||||
{
|
||||
using repositories::JsonFieldType;
|
||||
using repositories::JsonRowSchema;
|
||||
|
||||
Json::Value failure(const std::string &code)
|
||||
{
|
||||
Json::Value body;
|
||||
@@ -58,6 +61,27 @@ int boundedPerPage(const std::optional<std::string> &value)
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
||||
const JsonRowSchema &moderationFlagSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"report_id", JsonFieldType::UInt64},
|
||||
{"message_id", JsonFieldType::UInt64},
|
||||
{"created_by_user_id", JsonFieldType::UInt64},
|
||||
{"resolved_by_user_id", JsonFieldType::UInt64}};
|
||||
return schema;
|
||||
}
|
||||
|
||||
const JsonRowSchema &adminUserSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"points", JsonFieldType::Int64},
|
||||
{"banned", JsonFieldType::Bool},
|
||||
{"must_change_password", JsonFieldType::Bool}};
|
||||
return schema;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json::Value AdminService::listCategories()
|
||||
@@ -229,7 +253,8 @@ Json::Value AdminService::listModeration(
|
||||
statusValue,
|
||||
statusValue,
|
||||
perPageValue,
|
||||
offset));
|
||||
offset),
|
||||
moderationFlagSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -317,7 +342,8 @@ Json::Value AdminService::listUsers(const auth::UserContext &user)
|
||||
"LEFT JOIN roles r ON r.id = ur.role_id "
|
||||
"GROUP BY u.id, u.first_name, u.last_name, u.email, u.fiscal_code, "
|
||||
"u.phone, u.points, u.banned, u.must_change_password "
|
||||
"ORDER BY u.created_at DESC LIMIT 200"));
|
||||
"ORDER BY u.created_at DESC LIMIT 200"),
|
||||
adminUserSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
+70
-10
@@ -16,6 +16,9 @@ namespace visione::services
|
||||
{
|
||||
namespace
|
||||
{
|
||||
using repositories::JsonFieldType;
|
||||
using repositories::JsonRowSchema;
|
||||
|
||||
Json::Value failure(const std::string &code)
|
||||
{
|
||||
Json::Value body;
|
||||
@@ -152,6 +155,57 @@ int boundedPerPage(const std::optional<std::string> &value)
|
||||
}
|
||||
}
|
||||
|
||||
const JsonRowSchema &reportSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"user_id", JsonFieldType::UInt64},
|
||||
{"category_id", JsonFieldType::UInt64},
|
||||
{"area_id", JsonFieldType::UInt64},
|
||||
{"assigned_operator_id", JsonFieldType::UInt64},
|
||||
{"latitude", JsonFieldType::Double},
|
||||
{"longitude", JsonFieldType::Double}};
|
||||
return schema;
|
||||
}
|
||||
|
||||
const JsonRowSchema &reportHistorySchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"report_id", JsonFieldType::UInt64},
|
||||
{"changed_by_user_id", JsonFieldType::UInt64}};
|
||||
return schema;
|
||||
}
|
||||
|
||||
const JsonRowSchema &reportMediaSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"report_id", JsonFieldType::UInt64},
|
||||
{"size_bytes", JsonFieldType::UInt64}};
|
||||
return schema;
|
||||
}
|
||||
|
||||
const JsonRowSchema &messageSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"report_id", JsonFieldType::UInt64},
|
||||
{"user_id", JsonFieldType::UInt64}};
|
||||
return schema;
|
||||
}
|
||||
|
||||
const JsonRowSchema &openDataReportSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"category_id", JsonFieldType::UInt64},
|
||||
{"area_id", JsonFieldType::UInt64},
|
||||
{"latitude", JsonFieldType::Double},
|
||||
{"longitude", JsonFieldType::Double}};
|
||||
return schema;
|
||||
}
|
||||
|
||||
Json::Value pagedReportResponse(const drogon::orm::Result &result,
|
||||
int page,
|
||||
int perPage)
|
||||
@@ -159,7 +213,7 @@ Json::Value pagedReportResponse(const drogon::orm::Result &result,
|
||||
auto response = success();
|
||||
response["page"] = page;
|
||||
response["per_page"] = perPage;
|
||||
response["reports"] = repositories::resultToJson(result);
|
||||
response["reports"] = repositories::resultToJson(result, reportSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -335,7 +389,7 @@ Json::Value ReportService::listReports(const auth::UserContext &user,
|
||||
}
|
||||
|
||||
auto response = success();
|
||||
response["reports"] = repositories::resultToJson(result);
|
||||
response["reports"] = repositories::resultToJson(result, reportSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -429,13 +483,15 @@ Json::Value ReportService::reportDetail(const auth::UserContext &user,
|
||||
}
|
||||
|
||||
auto response = success();
|
||||
response["report"] = repositories::rowToJson(report[0]);
|
||||
response["report"] = repositories::rowToJson(report[0], reportSchema());
|
||||
response["history"] = repositories::resultToJson(db->execSqlSync(
|
||||
"SELECT * FROM report_status_history WHERE report_id = ? ORDER BY id",
|
||||
reportId));
|
||||
reportId),
|
||||
reportHistorySchema());
|
||||
response["media"] = repositories::resultToJson(db->execSqlSync(
|
||||
"SELECT * FROM report_photos WHERE report_id = ? ORDER BY id",
|
||||
reportId));
|
||||
reportId),
|
||||
reportMediaSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -464,15 +520,17 @@ Json::Value ReportService::interventionSummary(const auth::UserContext &user,
|
||||
return failure("report_not_found");
|
||||
}
|
||||
auto response = success();
|
||||
response["summary"] = repositories::rowToJson(report[0]);
|
||||
response["summary"] = repositories::rowToJson(report[0], reportSchema());
|
||||
response["media"] = repositories::resultToJson(db->execSqlSync(
|
||||
"SELECT * FROM report_photos WHERE report_id = ? ORDER BY id",
|
||||
reportId));
|
||||
reportId),
|
||||
reportMediaSchema());
|
||||
response["latest_messages"] = repositories::resultToJson(db->execSqlSync(
|
||||
"SELECT m.id, m.user_id, u.first_name, u.last_name, m.body, m.created_at "
|
||||
"FROM messages m JOIN users u ON u.id = m.user_id "
|
||||
"WHERE m.report_id = ? ORDER BY m.created_at DESC LIMIT 5",
|
||||
reportId));
|
||||
reportId),
|
||||
messageSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -628,7 +686,8 @@ Json::Value ReportService::listMessages(const auth::UserContext &user,
|
||||
"SELECT m.*, u.first_name, u.last_name FROM messages m "
|
||||
"JOIN users u ON u.id = m.user_id WHERE m.report_id = ? "
|
||||
"ORDER BY m.created_at",
|
||||
reportId));
|
||||
reportId),
|
||||
messageSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -802,7 +861,8 @@ Json::Value ReportService::openDataReports(const std::optional<std::string> &pag
|
||||
"WHERE r.status IN ('RESOLVED', 'CLOSED') "
|
||||
"ORDER BY r.updated_at DESC LIMIT ? OFFSET ?",
|
||||
perPageValue,
|
||||
offset));
|
||||
offset),
|
||||
openDataReportSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
+24
-2
@@ -10,6 +10,9 @@ namespace visione::services
|
||||
{
|
||||
namespace
|
||||
{
|
||||
using repositories::JsonFieldType;
|
||||
using repositories::JsonRowSchema;
|
||||
|
||||
Json::Value failure(const std::string &code)
|
||||
{
|
||||
Json::Value body;
|
||||
@@ -56,6 +59,23 @@ int boundedPerPage(const std::optional<std::string> &value)
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
||||
const JsonRowSchema ¬ificationSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"report_id", JsonFieldType::UInt64}};
|
||||
return schema;
|
||||
}
|
||||
|
||||
const JsonRowSchema &rewardEventSchema()
|
||||
{
|
||||
static const JsonRowSchema schema{
|
||||
{"id", JsonFieldType::UInt64},
|
||||
{"report_id", JsonFieldType::UInt64},
|
||||
{"points", JsonFieldType::Int64}};
|
||||
return schema;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json::Value UserService::listNotifications(
|
||||
@@ -77,7 +97,8 @@ Json::Value UserService::listNotifications(
|
||||
"ORDER BY created_at DESC LIMIT ? OFFSET ?",
|
||||
user.id,
|
||||
perPageValue,
|
||||
offset));
|
||||
offset),
|
||||
notificationSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -122,7 +143,8 @@ Json::Value UserService::rewards(const auth::UserContext &user,
|
||||
"ORDER BY created_at DESC LIMIT ? OFFSET ?",
|
||||
user.id,
|
||||
perPageValue,
|
||||
offset));
|
||||
offset),
|
||||
rewardEventSchema());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user