From 37250a15fb6ce194dded63a949c595c9a29236b0 Mon Sep 17 00:00:00 2001 From: Nathan Anderson Date: Sun, 30 Jul 2023 22:47:12 -0600 Subject: [PATCH] Fix for adding transaction --- deploy.sh | 2 ++ src/db/models.zig | 1 + src/http_handler.zig | 25 +++++++++++++++++++++++-- src/routes/auth.zig | 12 ++++++------ src/routes/transactions.zig | 9 +-------- src/routes/user.zig | 1 + 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/deploy.sh b/deploy.sh index 3265c30..b010f96 100755 --- a/deploy.sh +++ b/deploy.sh @@ -6,6 +6,8 @@ echo 'Creating backup of database file...' echo 'Rebuilding server...' +sudo systemctl stop zerver + git pull zig build diff --git a/src/db/models.zig b/src/db/models.zig index 4966c2d..6c14579 100644 --- a/src/db/models.zig +++ b/src/db/models.zig @@ -59,6 +59,7 @@ pub const Budget = struct { created_at: u64, updated_at: u64, hide: u8, + expected_income: ?f64, }; pub const User = struct { diff --git a/src/http_handler.zig b/src/http_handler.zig index 0a76921..35655bb 100644 --- a/src/http_handler.zig +++ b/src/http_handler.zig @@ -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; +} diff --git a/src/routes/auth.zig b/src/routes/auth.zig index 84785a0..ae4062b 100644 --- a/src/routes/auth.zig +++ b/src/routes/auth.zig @@ -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; } diff --git a/src/routes/transactions.zig b/src/routes/transactions.zig index f657866..f0e2d6e 100644 --- a/src/routes/transactions.zig +++ b/src/routes/transactions.zig @@ -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) { diff --git a/src/routes/user.zig b/src/routes/user.zig index f845754..7506b7a 100644 --- a/src/routes/user.zig +++ b/src/routes/user.zig @@ -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", "=");