Fix for adding transaction

This commit is contained in:
Nathan Anderson 2023-07-30 22:47:12 -06:00
parent 563b9023dc
commit 37250a15fb
6 changed files with 34 additions and 16 deletions

View File

@ -6,6 +6,8 @@ echo 'Creating backup of database file...'
echo 'Rebuilding server...'
sudo systemctl stop zerver
git pull
zig build

View File

@ -59,6 +59,7 @@ pub const Budget = struct {
created_at: u64,
updated_at: u64,
hide: u8,
expected_income: ?f64,
};
pub const User = struct {

View File

@ -117,9 +117,10 @@ pub fn returnError(message: ?[]const u8, comptime statusCode: u16, res: *httpz.R
}
}
res.status = statusCode;
res.json(.{ .success = false, .message = message }, .{}) catch |err| {
std.log.info("Returning error", .{});
return res.json(.{ .success = false, .message = message }, .{}) catch |err| {
std.log.warn("Couldnt create error body: {}", .{err});
res.body = "{ \"success\": false";
res.body = "{ \"success\": false }";
};
}
@ -128,3 +129,23 @@ pub fn returnData(data: anytype, res: *httpz.Response) !void {
res.status = 200;
try res.json(body, .{});
}
const ReqDataError = error{
MalformedBody,
NoData,
};
pub fn getReqJson(req: *httpz.Request, res: *httpz.Response, comptime ReqType: type) ReqDataError!ReqType {
const body_data = req.json(ReqType) catch |err| {
std.debug.print("Malformed body: {any}\nExpected {any} for req {s} on {s}", .{ err, ReqType, @tagName(req.method), req.url.query });
returnError("Bad request: Malformed Body", 400, res);
return ReqDataError.MalformedBody;
};
if (body_data == null) {
std.debug.print("Bad request, no data\nExpected {any} for req {s} on {s}", .{ ReqType, @tagName(req.method), req.url.query });
returnError("Bad request: No Data", 400, res);
return ReqDataError.NoData;
}
var body = body_data.?;
return body;
}

View File

@ -31,29 +31,29 @@ pub fn verifyRequest(req: *httpz.Request, res: *httpz.Response, user_id: ?u32, f
if (coded_token == null) {
handler.returnError("Unauthorized/NoToken", 401, res);
std.log.info("{s} {s} Unauthorized/NotAuthenticated - @ {s}\n", .{ method, req.url.query, formatted_now });
std.log.info("{s} {s} Unauthorized/NotAuthenticated - @ {s}", .{ method, req.url.query, formatted_now });
return VerifyAuthError.NotAuthenticated;
}
const token = jwt.validate(models.Token, allocator, .HS256, coded_token.?, .{ .key = key }) catch {
handler.returnError("Unauthorized", 401, res);
std.log.info("{s} {s} Unauthorized/BadToken - Token: {s} @ {s}\n", .{ method, req.url.raw, coded_token.?, formatted_now });
handler.returnError("Unauthorized", 400, res);
std.log.info("{s} {s} Unauthorized/BadToken - Token: {s} @ {s}", .{ method, req.url.raw, coded_token.?, formatted_now });
return VerifyAuthError.BadToken;
};
if (user_id != null and user_id.? != token.user_id or family_id != null and family_id.? != token.family_id) {
handler.returnError("Unauthorized", 401, res);
std.log.info("{s} {s} Unauthorized - User: {} Family: {any} @ {s}\n", .{ method, req.url.raw, token.user_id, token.family_id, formatted_now });
std.log.info("{s} {s} Unauthorized/BadCredentials - User: {} Family: {any} @ {s}, Tried User: {any} Family: {any}", .{ method, req.url.raw, token.user_id, token.family_id, formatted_now, user_id, family_id });
return VerifyAuthError.Unauthorized;
}
if (token.expires_at < now) {
std.log.info("{s} {s} Unauthorized/Expired - User: {} Family: {any} @ {s}\n", .{ method, req.url.raw, token.user_id, token.family_id, formatted_now });
std.log.info("{s} {s} Unauthorized/Expired - User: {} Family: {any} @ {s}", .{ method, req.url.raw, token.user_id, token.family_id, formatted_now });
handler.returnError("Credentials Expired", 403, res);
return VerifyAuthError.Expired;
}
std.log.info("{s} {s} Authorized - User: {} Family: {any} @ {s}\n", .{ method, req.url.raw, token.user_id, token.family_id, formatted_now });
std.log.info("{s} {s} Authorized - User: {} Family: {any} @ {s}", .{ method, req.url.raw, token.user_id, token.family_id, formatted_now });
return token;
}

View File

@ -138,16 +138,9 @@ pub fn postTransaction(req: *httpz.Request, res: *httpz.Response) !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const body_data = req.json(TransPostReq) catch |err| {
std.debug.print("Malformed body: {any}\n", .{err});
handler.returnError("Bad request: Malformed Body", 400, res);
var body = handler.getReqJson(req, res, TransPostReq) catch {
return;
};
if (body_data == null) {
handler.returnError("Bad request: No Data", 400, res);
return;
}
var body = body_data.?;
const budget = try db.selectOneById(models.Budget, allocator, body.budget_id);
if (budget == null) {

View File

@ -161,6 +161,7 @@ pub fn signup(req: *httpz.Request, res: *httpz.Response) !void {
.created_at = now,
.updated_at = now,
.hide = 0,
.expected_income = null,
};
try db.insert(models.Budget, new_budget);
const budget_query = try models.createSelectOnFieldQuery(models.Budget, null, "created_at", "=");