Fix for adding transaction
This commit is contained in:
parent
563b9023dc
commit
37250a15fb
|
@ -6,6 +6,8 @@ echo 'Creating backup of database file...'
|
||||||
|
|
||||||
echo 'Rebuilding server...'
|
echo 'Rebuilding server...'
|
||||||
|
|
||||||
|
sudo systemctl stop zerver
|
||||||
|
|
||||||
git pull
|
git pull
|
||||||
|
|
||||||
zig build
|
zig build
|
||||||
|
|
|
@ -59,6 +59,7 @@ pub const Budget = struct {
|
||||||
created_at: u64,
|
created_at: u64,
|
||||||
updated_at: u64,
|
updated_at: u64,
|
||||||
hide: u8,
|
hide: u8,
|
||||||
|
expected_income: ?f64,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const User = struct {
|
pub const User = struct {
|
||||||
|
|
|
@ -117,9 +117,10 @@ pub fn returnError(message: ?[]const u8, comptime statusCode: u16, res: *httpz.R
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.status = statusCode;
|
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});
|
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;
|
res.status = 200;
|
||||||
try res.json(body, .{});
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -31,29 +31,29 @@ pub fn verifyRequest(req: *httpz.Request, res: *httpz.Response, user_id: ?u32, f
|
||||||
|
|
||||||
if (coded_token == null) {
|
if (coded_token == null) {
|
||||||
handler.returnError("Unauthorized/NoToken", 401, res);
|
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;
|
return VerifyAuthError.NotAuthenticated;
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = jwt.validate(models.Token, allocator, .HS256, coded_token.?, .{ .key = key }) catch {
|
const token = jwt.validate(models.Token, allocator, .HS256, coded_token.?, .{ .key = key }) catch {
|
||||||
handler.returnError("Unauthorized", 401, res);
|
handler.returnError("Unauthorized", 400, res);
|
||||||
std.log.info("{s} {s} Unauthorized/BadToken - Token: {s} @ {s}\n", .{ method, req.url.raw, coded_token.?, formatted_now });
|
std.log.info("{s} {s} Unauthorized/BadToken - Token: {s} @ {s}", .{ method, req.url.raw, coded_token.?, formatted_now });
|
||||||
return VerifyAuthError.BadToken;
|
return VerifyAuthError.BadToken;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (user_id != null and user_id.? != token.user_id or family_id != null and family_id.? != token.family_id) {
|
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);
|
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;
|
return VerifyAuthError.Unauthorized;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token.expires_at < now) {
|
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);
|
handler.returnError("Credentials Expired", 403, res);
|
||||||
return VerifyAuthError.Expired;
|
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;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -138,16 +138,9 @@ pub fn postTransaction(req: *httpz.Request, res: *httpz.Response) !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
const body_data = req.json(TransPostReq) catch |err| {
|
var body = handler.getReqJson(req, res, TransPostReq) catch {
|
||||||
std.debug.print("Malformed body: {any}\n", .{err});
|
|
||||||
handler.returnError("Bad request: Malformed Body", 400, res);
|
|
||||||
return;
|
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);
|
const budget = try db.selectOneById(models.Budget, allocator, body.budget_id);
|
||||||
if (budget == null) {
|
if (budget == null) {
|
||||||
|
|
|
@ -161,6 +161,7 @@ pub fn signup(req: *httpz.Request, res: *httpz.Response) !void {
|
||||||
.created_at = now,
|
.created_at = now,
|
||||||
.updated_at = now,
|
.updated_at = now,
|
||||||
.hide = 0,
|
.hide = 0,
|
||||||
|
.expected_income = null,
|
||||||
};
|
};
|
||||||
try db.insert(models.Budget, new_budget);
|
try db.insert(models.Budget, new_budget);
|
||||||
const budget_query = try models.createSelectOnFieldQuery(models.Budget, null, "created_at", "=");
|
const budget_query = try models.createSelectOnFieldQuery(models.Budget, null, "created_at", "=");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user