Compare commits

..

160 commits

Author SHA1 Message Date
Imbus
12810075f9 Logic error in getAllWeeklyReports fixed 2024-04-03 17:44:50 +02:00
Imbus
12a2691d55 Rename 2024-04-03 17:40:48 +02:00
Imbus
9c5aa10414 Tests for getAllWeeklyReports 2024-04-03 17:32:50 +02:00
Imbus
fcd035fe6e TS Api for getAllWeeklyReport 2024-04-03 17:32:07 +02:00
Imbus
a39cfedad3 Rename, fix and testing for getAllWeeklyReports path 2024-04-03 17:31:39 +02:00
Imbus
a1d2520d88 Updating typescript api 2024-04-03 17:12:29 +02:00
Imbus
903132e56d Polishing tests 2024-04-03 17:06:08 +02:00
Imbus
7c973009ac Adding pycache to gitignore 2024-04-03 15:54:27 +02:00
Imbus
ffe5d53625 Splitting test script 2024-04-03 15:53:52 +02:00
Imbus
8ea6dec346 Fixes for various paths 2024-04-03 15:53:36 +02:00
Imbus
61a2d1ce0c PromoteToPm handler 2024-04-03 15:53:15 +02:00
Imbus
9cca8edd9d Additional tests 2024-04-03 14:50:04 +02:00
Imbus
1f9ccca9bf Fixing integration test script 2024-04-03 14:21:55 +02:00
Davenludd
6b880244a1 Merge branch 'gruppDM' into frontend 2024-04-03 14:00:14 +02:00
Davenludd
4d7b3e0d57 Refactor API call and types in AllTimeReportsInProjectOtherUser component 2024-04-03 13:59:44 +02:00
Peter KW
46eb3c76a8 Small fix to statistics 2024-04-02 19:18:05 +02:00
Peter KW
632676e3d2 Added admin to all projects in sample_data 2024-04-02 19:16:02 +02:00
Mattias
1b4e521508 Update variable names in TimePerActivity component 2024-04-02 18:02:40 +02:00
Mattias
a7cc48d392 Refactor TimePerRole component to use API response for time per activity 2024-04-02 18:02:04 +02:00
Davenludd
ff37236cf6 Minor fixes TimePerActivity component to use readOnly input fields 2024-04-02 17:35:02 +02:00
Davenludd
eb741ba20d Add total time calculation to DisplayUnsignedReports component 2024-04-02 17:33:20 +02:00
Davenludd
00ca5514e5 Added sign functionality to component 2024-04-02 17:27:17 +02:00
Mattias
7c7755085e Add comments for getUnsignedReportsInProject in API 2024-04-02 17:11:30 +02:00
Mattias
1e1677fc57 Refactor getUnsignedReports in DisplayUnsignedReports component 2024-04-02 17:07:47 +02:00
Mattias
0b8b430f38 Refactor DisplayUnsignedReports component to use API and WeeklyReport type 2024-04-02 17:05:55 +02:00
Mattias
93659a72dc Add getUnsignedReportsInProject API method 2024-04-02 17:05:46 +02:00
Mattias
8d6da684bf Add username retrieval from local storage 2024-04-02 16:21:23 +02:00
Davenludd
762a1b7368 Merge branch 'frontend' into gruppDM 2024-04-02 15:51:11 +02:00
Mattias
398305d3ed Fix input validation in NewWeeklyReport component 2024-04-02 15:43:18 +02:00
Mattias
6c2213b488 Update handleUpdateWeeklyReport function and fix input validation 2024-04-02 15:43:12 +02:00
Mattias
b3e363f391 Add updateWeeklyReport function to API.ts 2024-04-02 15:43:05 +02:00
Peter KW
51a4d2a0b7 Fix to comments 2024-04-02 13:45:03 +02:00
Peter KW
524bd6c691 Can now delete project 2024-04-02 13:44:31 +02:00
Peter KW
b50d88f670 DeleteProject component 2024-04-02 13:44:06 +02:00
Peter KW
3ed4393c77 Fixed fetch path in removeProject 2024-04-02 13:43:32 +02:00
Peter KW
75876e43da Minor design fix 2024-04-02 13:22:46 +02:00
Peter KW
ea5bbf5f0a Merge branch 'frontend' into gruppPP 2024-04-02 13:18:25 +02:00
Peter KW
948dcce1ca Some fixes to design and comments 2024-04-02 13:14:08 +02:00
Peter KW
cb68a6323b Now shows project statistics 2024-04-02 13:11:33 +02:00
Peter KW
ca88daf493 GetProjectTimes component 2024-04-02 13:10:04 +02:00
Peter KW
644d0ee12c getProjectTimes API 2024-04-02 13:09:28 +02:00
Peter KW
6efc961774 Added getProjectTimes 2024-04-02 13:08:55 +02:00
Davenludd
7df1654bdc Merge branch 'frontend' into gruppDM 2024-04-02 11:49:46 +02:00
Davenludd
6dfa917cf0 Fix in DisplayUserProjects component 2024-04-02 11:37:53 +02:00
Peter KW
17a571fd7c Uses component to get projects now 2024-04-01 02:25:12 +02:00
Peter KW
f3466854c7 Removed unused pages and paths to them in main 2024-04-01 02:24:26 +02:00
Peter KW
1212b3c5ef Removed some stuff 2024-04-01 02:20:48 +02:00
Peter KW
dc98fb510e Clears username+password fields on successful register 2024-04-01 02:17:57 +02:00
Peter KW
58deef400a Removed unused code 2024-04-01 02:17:02 +02:00
Peter KW
cc039d27ae New modal for member info 2024-04-01 02:16:23 +02:00
Peter KW
3981190c7a Can now change username in this modal + moved some stuff to a separate modal 2024-04-01 02:16:06 +02:00
Peter KW
9b0a231701 Some fixes to ChangeUsername 2024-04-01 02:14:44 +02:00
Peter KW
e06aced6dd ChangeRole component to change role and a view for it 2024-04-01 02:13:24 +02:00
Peter KW
68fbbb4b19 Added some alerts + removed unused code 2024-04-01 02:09:28 +02:00
Peter KW
378dd99592 Changed so that you can only change other users role 2024-04-01 02:08:19 +02:00
Peter KW
6fa8135e32 ChangeUserRole API added + bugfix 2024-04-01 02:02:22 +02:00
Peter KW
60fb333090 Fix to paths 2024-03-31 21:04:58 +02:00
Peter KW
e7911574be Removed unused files 2024-03-31 20:56:40 +02:00
Peter KW
0c8a394f74 Small fix so that it uses component for getting users in a proj 2024-03-31 20:56:08 +02:00
Peter KW
5f42fa7818 Fixed types and imports of types 2024-03-31 20:54:00 +02:00
Peter KW
8b6462abee Changed so that username is required to get projects, so that you can get another user's projects (for admin stuff) 2024-03-29 20:19:22 +01:00
Peter KW
4ab23b3c3c Merge branch 'dev' into gruppPP 2024-03-29 20:05:10 +01:00
Imbus
bc9b01d85a Example component GenApiDemo on how to use the new API 2024-03-29 19:37:21 +01:00
Imbus
d2b4bf2a89 Brand new typescript API interface generated from swagger 2024-03-29 19:36:47 +01:00
Imbus
8d5329146d New tygo generated goTypes 2024-03-29 19:36:06 +01:00
Imbus
77f028fd39 Prettierignore for generated files 2024-03-29 19:23:35 +01:00
Imbus
f1e15137d6 Freshly generated swagger docs 2024-03-29 18:43:26 +01:00
Imbus
87a19bfd4e Swagger annotations for JWT key 2024-03-29 18:42:53 +01:00
Imbus
c2fa9aa0c1 Lots of fiddling with swagger annotations in user related handlers 2024-03-29 18:42:12 +01:00
Imbus
1385011769 Make swagger-typescript-api makefile target wipe the previous version 2024-03-29 18:41:20 +01:00
Davenludd
c1f49915ba Refactor signReport method signature 2024-03-29 18:29:38 +01:00
Davenludd
2aaa327a01 Merge branch 'dev' into gruppDM 2024-03-29 17:55:32 +01:00
Davenludd
05545f6f88 Minor fixes 2024-03-29 17:53:37 +01:00
Imbus
374e357820 Add database.txt to clean target 2024-03-29 16:41:55 +01:00
Imbus
0792c6b8a3 More sane swagger-typescript-api generator parameters 2024-03-29 16:36:47 +01:00
Imbus
45e891ed2c Regenerated swagger docs 2024-03-29 15:59:36 +01:00
Imbus
1db1b84e8f Initial demo of swagger-typescript-api interface generation 2024-03-29 15:58:36 +01:00
Imbus
0cd0c8d832 Handler re-order to satisfy human OCD 2024-03-29 15:36:42 +01:00
Imbus
4538a3b193 SignReport handler changes along with tests and TS interface 2024-03-29 15:33:20 +01:00
Imbus
b927fb80fb Refactor again, splitting project related handlers 2024-03-29 15:00:29 +01:00
Imbus
1d5fcd61b6 Refactor, report related endpoints now reside in individual files 2024-03-29 14:50:56 +01:00
Imbus
13d3035e49 Major refactor, splitting user handlers into separate files and changes to how the database is accessed 2024-03-29 14:37:22 +01:00
al8763be
c466a98b15 test for getUnsigned 2024-03-29 12:43:35 +01:00
Imbus
9edcc74ee2 Fix for IsProjectManagerHandler 2024-03-29 12:41:51 +01:00
al8763be
8d7d815745 Merge remote-tracking branch 'origin/gruppDM' into BumBranch 2024-03-28 16:46:29 +01:00
al8763be
09f2a2202f Merge remote-tracking branch 'origin/frontend' into BumBranch 2024-03-28 16:45:39 +01:00
al8763be
5d714bbacf Merge remote-tracking branch 'origin/AlexTester' into BumBranch 2024-03-28 16:44:29 +01:00
al8763be
9a54175d49 Co-authored-by: Imbus <imbus64@users.noreply.github.com> 2024-03-28 16:43:34 +01:00
al8763be
f6dcdcc376 GetUnsignedReports 2024-03-28 16:40:55 +01:00
Davenludd
4a71078f2a Fix API endpoint for checking project manager 2024-03-28 12:25:24 +01:00
Davenludd
c002f0e530 Merge remote-tracking branch 'origin/AlexTester' into gruppDM 2024-03-28 12:19:45 +01:00
al8763be
6a7bb9ab26 fix ifPM igen 2024-03-28 12:19:11 +01:00
Davenludd
cc186f8ad0 Refactor handleProjectClick function to remove unused username parameter 2024-03-28 12:16:35 +01:00
Davenludd
e589726ab2 Merge remote-tracking branch 'origin/AlexTester' into gruppDM 2024-03-28 12:10:53 +01:00
Davenludd
37d06b10be Fix typo in route parameter name 2024-03-28 12:09:16 +01:00
Davenludd
48e4d1a8df Refactor fetchWeeklyReport in EditWeeklyReport component 2024-03-28 12:09:09 +01:00
al8763be
6a78e67e7e lol 2024-03-28 12:08:14 +01:00
al8763be
7c46797634 ifPM fix 2024-03-28 12:06:59 +01:00
Alexander Ek
d4cc556366 Co-authored-by: al8763be <al8763be@users.noreply.github.com> 2024-03-27 21:18:44 +01:00
Davenludd
7c73a01d4c Add success alert message after submitting weekly report 2024-03-27 20:47:24 +01:00
Davenludd
0fc8957e55 Merge branch 'master' into gruppDM 2024-03-27 20:38:31 +01:00
Imbus
e1b410c850 Merge branch 'dev' 2024-03-26 04:19:27 +01:00
Imbus
94f36d4e06 Cleaning 2024-03-23 20:32:00 +01:00
Imbus
96238ceb2e Fix asset resolution at build time 2024-03-23 20:29:08 +01:00
Alexander Ek
42a2ad02e4 Description comment in main.go file 2024-03-23 15:22:58 +01:00
Davenludd
492cfed08c Merge branch 'dev' into gruppDM 2024-03-21 18:09:17 +01:00
Davenludd
6ed53fe94a retract changes made in error handling in SubmitWeeklyReport handler 2024-03-21 18:07:56 +01:00
Imbus
dbbe4da401 Fix for submitWeeklyReport ts API 2024-03-21 18:05:41 +01:00
Davenludd
8d739396a1 Change input types from number to text in ViewOtherTimeReport component 2024-03-21 14:01:33 +01:00
Davenludd
689daf4e1f Updated Link in ProjectMembers component 2024-03-21 14:01:19 +01:00
Davenludd
1d054e660c Fixes for PMOtherUsersTR 2024-03-21 14:00:47 +01:00
Davenludd
589a135bb5 New Page for PM 2024-03-21 13:59:57 +01:00
Davenludd
4c22ba478d Add OtherUsersTR component for displaying weekly report of a user in a project 2024-03-21 13:59:24 +01:00
Davenludd
deeff6c3c2 Added AllTimeReportsInProjectOtherUser component 2024-03-21 13:59:10 +01:00
Davenludd
e47f12c6d7 Add new route for PM components 2024-03-21 13:58:45 +01:00
Davenludd
b656204457 Added component for PM to view other users time reports 2024-03-21 12:46:05 +01:00
Mattias
9f931a2643 Add heading to Edit Time Report page 2024-03-21 12:37:44 +01:00
Mattias
a31a50965f Update routes and components for PMViewUnsignedReport 2024-03-21 12:36:14 +01:00
Davenludd
ebc59e0c11 Refactor handleNewWeeklyReport function to return a boolean indicating success or failure if week already has a report 2024-03-21 12:31:16 +01:00
Davenludd
d8a73329a1 Refactor error handling in SubmitWeeklyReport handler 2024-03-21 12:31:16 +01:00
Mattias
1b20173ece Remove unused import and add DisplayUnsignedReports component 2024-03-21 12:10:53 +01:00
Mattias
4b6c93a202 Add DisplayUnsignedReports component and update route for otherUsersTimeReports 2024-03-21 12:10:43 +01:00
Mattias
70e6cbf12e Update project members page layout and remove unnecessary links 2024-03-21 11:30:07 +01:00
Davenludd
856ae40900 Refactor input fields to handle empty values 2024-03-21 11:10:37 +01:00
Melker
ec362cfa3a GetProjectTimesHandler 2024-03-21 10:51:47 +01:00
Mattias
e2d2310275 Add ViewOtherTimeReport component to PMOtherUsersTR page 2024-03-21 10:31:24 +01:00
Mattias
85be4c79d6 Refactor getProjectMembers function in ProjectMembers component 2024-03-21 10:05:51 +01:00
Davenludd
ae0208ff23 Add project navigation based on user and pm role 2024-03-21 10:02:59 +01:00
Mattias
14133a9f22 CleanUp, making lint happy 2024-03-21 09:59:12 +01:00
Mattias
c415539904 Refactor ProjectMembers component to use API for fetching project members 2024-03-21 09:36:08 +01:00
Davenludd
01a6f9e61d Add comment NewWeeklyReport 2024-03-21 08:57:56 +01:00
Davenludd
faf998e652 Merge branch 'dev' into gruppDM 2024-03-21 08:56:07 +01:00
dDogge
2d4ff7e087 Fully implemented UpdateWeeklyReport for database, handler and corresponding tests 2024-03-21 02:48:55 +01:00
Imbus
b484346031 Better integration test target 2024-03-21 02:26:30 +01:00
al8763be
dea802bd91 bummed handler 2024-03-21 01:37:39 +01:00
Imbus
ed88220a47 Fix containerfile permission errors 2024-03-21 01:24:17 +01:00
Imbus
d4547e997c Merge branch 'dev' 2024-03-21 00:48:44 +01:00
Imbus
a6d7ee2de6 Merge branch 'dev' 2024-03-21 00:03:51 +01:00
Davenludd
a4bd2c9315 Merge branch 'dev' into gruppDM 2024-03-20 22:50:56 +01:00
Mattias
1a87effc3a Changes to EditWeeklyReport component 2024-03-20 22:49:54 +01:00
Davenludd
d0209094a9 Remove unused variable in EditWeeklyReport component 2024-03-20 22:10:53 +01:00
Davenludd
f91f0ff8f5 Merge branch 'dev' into gruppDM 2024-03-20 22:05:04 +01:00
Davenludd
c995dc52ad Merge branch 'dev' into gruppDM 2024-03-20 22:04:11 +01:00
Davenludd
900797facc Fix initial state values and handle empty input values in NewWeeklyReport component 2024-03-20 21:53:27 +01:00
Davenludd
b9b17bf229 Merge branch 'dev' into gruppDM 2024-03-20 21:21:59 +01:00
Davenludd
bf59503517 Update background images in LoginPage.css 2024-03-20 17:24:54 +01:00
Davenludd
e4f5fbda44 Fix useEffect dependency in AllTimeReportsInProject component 2024-03-20 16:46:44 +01:00
Mattias
b99de71c38 Update links in PMProjectMembers component 2024-03-20 16:41:32 +01:00
Mattias
0befc4c7d1 Minor fix 2024-03-20 16:36:39 +01:00
Mattias
a3fc71f4bf Changed path in main 2024-03-20 16:36:32 +01:00
Mattias
4030031ce9 New comp TimePerActivity 2024-03-20 16:36:24 +01:00
Mattias
dd02c2c5c6 Brand new TimePerRole component deluxe edition 2024-03-20 16:24:38 +01:00
Mattias
287e97fe6f Updated path in main 2024-03-20 16:24:10 +01:00
Davenludd
8084f722b5 Fixies in API and component code 2024-03-20 15:32:30 +01:00
Davenludd
caa793d036 Fix week validation in NewWeeklyReport component for safari and firefox 2024-03-20 15:32:14 +01:00
Davenludd
847073c6f8 Merge branch 'BumBranch' into gruppDM 2024-03-20 15:21:26 +01:00
Davenludd
54e42cd2a8 Add support for week input in Chrome and Edge browsers 2024-03-20 15:19:09 +01:00
Davenludd
eddf678f3a Merge branch 'BumBranch' into gruppDM 2024-03-20 14:48:32 +01:00
Johanna
1974607fc7 Formating 2024-03-19 22:04:20 +01:00
Johanna
63fd2e9b6f //fix parse in NewWeeklyReport 2024-03-19 21:47:16 +01:00
99 changed files with 4402 additions and 1814 deletions

5
.gitignore vendored
View file

@ -14,6 +14,11 @@ diagram.puml
backend/*.png
backend/*.jpg
backend/*.svg
__pycache__
/go.work.sum
/package-lock.json
/backend/docs/swagger.json
# Test binary, built with `go test -c`
*.test

View file

@ -23,10 +23,13 @@ load-release file:
# Tests every part of the project
testall:
cd frontend && npm install
cd frontend && npm test
cd frontend && npm run lint
cd frontend && npm run build
cd backend && make test
cd backend && make lint
cd backend && make itest
# Cleans up everything related to the project
clean: remove-podman-containers

View file

@ -13,10 +13,13 @@ remove-podman-containers:
# Tests every part of the project
testall:
cd frontend && npm install
cd frontend && npm test
cd frontend && npm run lint
cd frontend && npm run build
cd backend && make test
cd backend && make lint
cd backend && make itest
# Cleans up everything related to the project
clean: remove-podman-containers

View file

@ -8,17 +8,19 @@ GOGET = $(GOCMD) get
# SQLite database filename
DB_FILE = db.sqlite3
PROC_NAME = ttime_server
# Directory containing migration SQL scripts
MIGRATIONS_DIR = internal/database/migrations
SAMPLE_DATA_DIR = internal/database/sample_data
# Build target
build:
$(GOBUILD) -o bin/server main.go
$(GOBUILD) -o bin/$(PROC_NAME) main.go
# Run target
run: build
./bin/server
./bin/$(PROC_NAME)
watch: build
watchexec -c -w . -r make run
@ -32,11 +34,22 @@ clean:
rm -f plantuml.jar
rm -f erd.png
rm -f config.toml
rm -f database.txt
# Test target
test: db.sqlite3
$(GOTEST) ./... -count=1
# Integration test target
.PHONY: itest
itest:
pgrep $(PROC_NAME) && echo "Server already running" && exit 1 || true
make build
./bin/$(PROC_NAME) >/dev/null 2>&1 &
sleep 1 # Adjust if needed
python ../testing.py
pkill $(PROC_NAME)
# Get dependencies target
deps:
$(GOGET) -v ./...
@ -92,6 +105,17 @@ default: build
docs:
swag init -outputTypes go
api: ./docs/swagger.json
rm ../frontend/src/API/GenApi.ts
npx swagger-typescript-api \
--api-class-name GenApi \
--path ./docs/swagger.json \
--output ../frontend/src/API \
--name GenApi.ts \
./docs/swagger.json:
swag init -outputTypes json
.PHONY: docfmt
docfmt:
swag fmt

View file

@ -21,21 +21,21 @@ const docTemplate = `{
"paths": {
"/login": {
"post": {
"description": "logs the user in and returns a jwt token",
"description": "Logs in a user and returns a JWT token",
"consumes": [
"application/json"
],
"produces": [
"text/plain"
"application/json"
],
"tags": [
"User"
"Auth"
],
"summary": "login",
"summary": "Login",
"parameters": [
{
"description": "login info",
"name": "NewUser",
"description": "User credentials",
"name": "body",
"in": "body",
"required": true,
"schema": {
@ -45,9 +45,9 @@ const docTemplate = `{
],
"responses": {
"200": {
"description": "Successfully signed token for user",
"description": "JWT token",
"schema": {
"type": "Token"
"$ref": "#/definitions/types.Token"
}
},
"400": {
@ -71,29 +71,26 @@ const docTemplate = `{
}
}
},
"/loginerenew": {
"/loginrenew": {
"post": {
"security": [
{
"bererToken": []
"JWT": []
}
],
"description": "renews the users token",
"consumes": [
"description": "Renews the users token.",
"produces": [
"application/json"
],
"produces": [
"text/plain"
],
"tags": [
"User"
"Auth"
],
"summary": "LoginRenews",
"responses": {
"200": {
"description": "Successfully signed token for user",
"schema": {
"type": "Token"
"$ref": "#/definitions/types.Token"
}
},
"401": {
@ -113,7 +110,12 @@ const docTemplate = `{
},
"/promoteToAdmin": {
"post": {
"description": "promote chosen user to admin",
"security": [
{
"JWT": []
}
],
"description": "Promote chosen user to site admin",
"consumes": [
"application/json"
],
@ -137,13 +139,13 @@ const docTemplate = `{
],
"responses": {
"200": {
"description": "Successfully prometed user",
"description": "Successfully promoted user",
"schema": {
"type": "json"
"$ref": "#/definitions/types.Token"
}
},
"400": {
"description": "bad request",
"description": "Bad request",
"schema": {
"type": "string"
}
@ -173,7 +175,7 @@ const docTemplate = `{
"text/plain"
],
"tags": [
"User"
"Auth"
],
"summary": "Register",
"parameters": [
@ -211,6 +213,11 @@ const docTemplate = `{
},
"/userdelete/{username}": {
"delete": {
"security": [
{
"JWT": []
}
],
"description": "UserDelete deletes a user from the database",
"consumes": [
"application/json"
@ -252,12 +259,14 @@ const docTemplate = `{
},
"/users/all": {
"get": {
"description": "lists all users",
"consumes": [
"application/json"
"security": [
{
"JWT": []
}
],
"description": "lists all users",
"produces": [
"text/plain"
"application/json"
],
"tags": [
"User"
@ -265,9 +274,12 @@ const docTemplate = `{
"summary": "ListsAllUsers",
"responses": {
"200": {
"description": "Successfully signed token for user",
"description": "Successfully returned all users",
"schema": {
"type": "json"
"type": "array",
"items": {
"type": "string"
}
}
},
"401": {
@ -291,16 +303,27 @@ const docTemplate = `{
"type": "object",
"properties": {
"password": {
"type": "string"
"type": "string",
"example": "password123"
},
"username": {
"type": "string",
"example": "username123"
}
}
},
"types.Token": {
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"bererToken": {
"JWT": {
"description": "Use the JWT token provided by the login endpoint to authenticate requests. **Prefix the token with \"Bearer \".**",
"type": "apiKey",
"name": "Authorization",
"in": "header"

View file

@ -35,13 +35,14 @@ type Database interface {
GetProject(projectId int) (types.Project, error)
GetUserRole(username string, projectname string) (string, error)
GetWeeklyReport(username string, projectName string, week int) (types.WeeklyReport, error)
GetWeeklyReportsUser(username string, projectname string) ([]types.WeeklyReportList, error)
GetAllWeeklyReports(username string, projectname string) ([]types.WeeklyReportList, error)
GetUnsignedWeeklyReports(projectName string) ([]types.WeeklyReport, error)
SignWeeklyReport(reportId int, projectManagerId int) error
IsSiteAdmin(username string) (bool, error)
IsProjectManager(username string, projectname string) (bool, error)
GetProjectTimes(projectName string) (map[string]int, error)
UpdateWeeklyReport(projectName string, userName string, week int, developmentTime int, meetingTime int, adminTime int, ownWorkTime int, studyTime int, testingTime int) error
RemoveProject(projectname string) error
}
// This struct is a wrapper type that holds the database connection
@ -355,6 +356,51 @@ func (d *Db) SignWeeklyReport(reportId int, projectManagerId int) error {
return err
}
func (d *Db) GetUnsignedWeeklyReports(projectName string) ([]types.WeeklyReport, error) {
// Define the SQL query to fetch unsigned reports for a given user
query := `
SELECT
report_id,
user_id,
project_id,
week,
development_time,
meeting_time,
admin_time,
own_work_time,
study_time,
testing_time,
signed_by
FROM
weekly_reports
WHERE
signed_by IS NULL
AND project_id = (SELECT id FROM projects WHERE name = ?)
`
// Execute the query
rows, err := d.Queryx(query, projectName)
if err != nil {
return nil, err
}
defer rows.Close()
// Iterate over the rows and populate the result slice
var reports []types.WeeklyReport
for rows.Next() {
var report types.WeeklyReport
if err := rows.StructScan(&report); err != nil {
return nil, err
}
reports = append(reports, report)
}
if err := rows.Err(); err != nil {
return nil, err
}
return reports, nil
}
// IsSiteAdmin checks if a given username is a site admin
func (d *Db) IsSiteAdmin(username string) (bool, error) {
// Define the SQL query to check if the user is a site admin
@ -417,8 +463,8 @@ func (d *Db) Migrate() error {
return nil
}
// GetWeeklyReportsUser retrieves weekly reports for a specific user and project.
func (d *Db) GetWeeklyReportsUser(username string, projectName string) ([]types.WeeklyReportList, error) {
// GetAllWeeklyReports retrieves weekly reports for a specific user and project.
func (d *Db) GetAllWeeklyReports(username string, projectName string) ([]types.WeeklyReportList, error) {
query := `
SELECT
wr.week,
@ -454,6 +500,26 @@ func (d *Db) IsProjectManager(username string, projectname string) (bool, error)
return manager, err
}
func (d *Db) UpdateWeeklyReport(projectName string, userName string, week int, developmentTime int, meetingTime int, adminTime int, ownWorkTime int, studyTime int, testingTime int) error {
query := `
UPDATE weekly_reports
SET
development_time = ?,
meeting_time = ?,
admin_time = ?,
own_work_time = ?,
study_time = ?,
testing_time = ?
WHERE
user_id = (SELECT id FROM users WHERE username = ?)
AND project_id = (SELECT id FROM projects WHERE name = ?)
AND week = ?
`
_, err := d.Exec(query, developmentTime, meetingTime, adminTime, ownWorkTime, studyTime, testingTime, userName, projectName, week)
return err
}
// MigrateSampleData applies sample data to the database.
func (d *Db) MigrateSampleData() error {
// Insert sample data
@ -495,34 +561,34 @@ func (d *Db) MigrateSampleData() error {
// GetProjectTimes retrieves a map with times per "Activity" for a given project
func (d *Db) GetProjectTimes(projectName string) (map[string]int, error) {
query := `
query := `
SELECT development_time, meeting_time, admin_time, own_work_time, study_time, testing_time
FROM weekly_reports
JOIN projects ON weekly_reports.project_id = projects.id
WHERE projects.name = ?
`
rows, err := d.DB.Query(query, projectName)
if err != nil {
return nil, err
}
defer rows.Close()
rows, err := d.DB.Query(query, projectName)
if err != nil {
return nil, err
}
defer rows.Close()
totalTime := make(map[string]int)
for rows.Next() {
var developmentTime, meetingTime, adminTime, ownWorkTime, studyTime, testingTime int
if err := rows.Scan(&developmentTime, &meetingTime, &adminTime, &ownWorkTime, &studyTime, &testingTime); err != nil {
return nil, err
}
for rows.Next() {
var developmentTime, meetingTime, adminTime, ownWorkTime, studyTime, testingTime int
if err := rows.Scan(&developmentTime, &meetingTime, &adminTime, &ownWorkTime, &studyTime, &testingTime); err != nil {
return nil, err
}
totalTime["development"] += developmentTime
totalTime["meeting"] += meetingTime
totalTime["admin"] += adminTime
totalTime["own_work"] += ownWorkTime
totalTime["study"] += studyTime
totalTime["testing"] += testingTime
}
totalTime["development"] += developmentTime
totalTime["meeting"] += meetingTime
totalTime["admin"] += adminTime
totalTime["own_work"] += ownWorkTime
totalTime["study"] += studyTime
totalTime["testing"] += testingTime
}
if err := rows.Err(); err != nil {
return nil, err
@ -530,3 +596,8 @@ func (d *Db) GetProjectTimes(projectName string) (map[string]int, error) {
return totalTime, nil
}
func (d *Db) RemoveProject(projectname string) error {
_, err := d.Exec("DELETE FROM projects WHERE name = ?", projectname)
return err
}

View file

@ -470,6 +470,47 @@ func TestGetWeeklyReport(t *testing.T) {
// Check other fields similarly
}
func TestGetUnsignedWeeklyReports(t *testing.T) {
db, err := setupAdvancedState()
if err != nil {
t.Error("setupState failed:", err)
}
err = db.AddUser("testuser", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
err = db.AddUser("testuser1", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
err = db.AddProject("testproject", "description", "testuser")
if err != nil {
t.Error("AddProject failed:", err)
}
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
if err != nil {
t.Error("AddWeeklyReport failed:", err)
}
err = db.AddWeeklyReport("testproject", "testuser1", 1, 1, 1, 1, 1, 1, 1)
if err != nil {
t.Error("AddWeeklyReport failed:", err)
}
reports, err := db.GetUnsignedWeeklyReports("testproject")
if err != nil {
t.Error("GetUnsignedWeeklyReports failed:", err)
}
if reports == nil {
t.Error("Expected non-nil reports, got nil")
}
}
// TestSignWeeklyReport tests SignWeeklyReport function of the database
func TestSignWeeklyReport(t *testing.T) {
db, err := setupState()
@ -664,7 +705,7 @@ func TestGetWeeklyReportsUser(t *testing.T) {
t.Error("AddWeeklyReport failed:", err)
}
reports, err := db.GetWeeklyReportsUser("testuser", "testproject")
reports, err := db.GetAllWeeklyReports("testuser", "testproject")
if err != nil {
t.Error("GetWeeklyReportsUser failed:", err)
}
@ -729,90 +770,89 @@ func TestIsProjectManager(t *testing.T) {
}
}
func TestGetProjectTimes(t *testing.T) {
// Initialize
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
return
}
// Initialize
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
return
}
// Create a user
user := "TeaUser"
password := "Vanilla"
err = db.AddUser(user, password)
if err != nil {
t.Error("AddUser failed:", err)
return
}
// Create a user
user := "TeaUser"
password := "Vanilla"
err = db.AddUser(user, password)
if err != nil {
t.Error("AddUser failed:", err)
return
}
// Create a project
projectName := "ProjectVanilla"
projectDescription := "When tea tastes its best"
err = db.AddProject(projectName, projectDescription, user) // Fix the variable name here
if err != nil {
t.Error("AddProject failed:", err)
return
}
// Create a project
projectName := "ProjectVanilla"
projectDescription := "When tea tastes its best"
err = db.AddProject(projectName, projectDescription, user) // Fix the variable name here
if err != nil {
t.Error("AddProject failed:", err)
return
}
// Tests the func in db.go
totalTime, err := db.GetProjectTimes(projectName)
if err != nil {
t.Error("GetTotalTimePerActivity failed:", err)
return
}
// Tests the func in db.go
totalTime, err := db.GetProjectTimes(projectName)
if err != nil {
t.Error("GetTotalTimePerActivity failed:", err)
return
}
// Check if the totalTime map is not nil
if totalTime == nil {
t.Error("Expected non-nil totalTime map, got nil")
return
}
// Check if the totalTime map is not nil
if totalTime == nil {
t.Error("Expected non-nil totalTime map, got nil")
return
}
// Define the expected valeus
expectedTotalTime := map[string]int{
"development": 0,
"meeting": 0,
"admin": 0,
"own_work": 0,
"study": 0,
"testing": 0,
}
// Define the expected valeus
expectedTotalTime := map[string]int{
"development": 0,
"meeting": 0,
"admin": 0,
"own_work": 0,
"study": 0,
"testing": 0,
}
// Compare the expectedTotalTime with the totalTime retrieved from the database
for activity, expectedTime := range expectedTotalTime {
if totalTime[activity] != expectedTime {
t.Errorf("Expected %s time to be %d, got %d", activity, expectedTime, totalTime[activity])
}
}
// Compare the expectedTotalTime with the totalTime retrieved from the database
for activity, expectedTime := range expectedTotalTime {
if totalTime[activity] != expectedTime {
t.Errorf("Expected %s time to be %d, got %d", activity, expectedTime, totalTime[activity])
}
}
// Insert some data into the database for different activities
err = db.AddWeeklyReport(projectName, user, 1, 1, 3, 2, 1, 4, 5)
if err != nil {
t.Error("Failed to insert data into the database:", err)
return
}
// Insert some data into the database for different activities
err = db.AddWeeklyReport(projectName, user, 1, 1, 3, 2, 1, 4, 5)
if err != nil {
t.Error("Failed to insert data into the database:", err)
return
}
newTotalTime, err := db.GetProjectTimes(projectName)
if err != nil {
t.Error("GetTotalTimePerActivity failed:", err)
return
}
if err != nil {
t.Error("GetTotalTimePerActivity failed:", err)
return
}
newExpectedTotalTime := map[string]int{
"development": 1,
"meeting": 3,
"admin": 2,
"own_work": 1,
"study": 4,
"testing": 5,
}
"development": 1,
"meeting": 3,
"admin": 2,
"own_work": 1,
"study": 4,
"testing": 5,
}
for activity, newExpectedTime := range newExpectedTotalTime {
if newTotalTime[activity] != newExpectedTime {
t.Errorf("Expected %s time to be %d, got %d", activity, newExpectedTime, newTotalTime[activity])
}
}
if newTotalTime[activity] != newExpectedTime {
t.Errorf("Expected %s time to be %d, got %d", activity, newExpectedTime, newTotalTime[activity])
}
}
}
func TestEnsureManagerOfCreatedProject(t *testing.T) {
db, err := setupState()
@ -847,3 +887,80 @@ func TestEnsureManagerOfCreatedProject(t *testing.T) {
t.Error("Expected testuser to be a project manager, but it's not.")
}
}
// TestUpdateWeeklyReport tests the UpdateWeeklyReport function of the database
func TestUpdateWeeklyReport(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
// Add a user
err = db.AddUser("testuser", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
// Add a project
err = db.AddProject("testproject", "description", "testuser")
if err != nil {
t.Error("AddProject failed:", err)
}
// Add a weekly report
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
if err != nil {
t.Error("AddWeeklyReport failed:", err)
}
// Update the weekly report
err = db.UpdateWeeklyReport("testproject", "testuser", 1, 2, 2, 2, 2, 2, 2)
if err != nil {
t.Error("UpdateWeeklyReport failed:", err)
}
// Retrieve the updated report
updatedReport, err := db.GetWeeklyReport("testuser", "testproject", 1)
if err != nil {
t.Error("GetWeeklyReport failed:", err)
}
// Check if the report was updated correctly
if updatedReport.DevelopmentTime != 2 ||
updatedReport.MeetingTime != 2 ||
updatedReport.AdminTime != 2 ||
updatedReport.OwnWorkTime != 2 ||
updatedReport.StudyTime != 2 ||
updatedReport.TestingTime != 2 {
t.Error("UpdateWeeklyReport failed: report not updated correctly")
}
}
func TestRemoveProject(t *testing.T) {
db, err := setupAdvancedState()
if err != nil {
t.Error("setupState failed:", err)
}
// Promote user to Admin
err = db.PromoteToAdmin("demouser")
if err != nil {
t.Error("PromoteToAdmin failed:", err)
}
// Remove project
err = db.RemoveProject("projecttest")
if err != nil {
t.Error("RemoveProject failed:", err)
}
// Check if the project was removed
projects, err := db.GetAllProjects()
if err != nil {
t.Error("GetAllProjects failed:", err)
}
if len(projects) != 0 {
t.Error("RemoveProject failed: expected 0, got", len(projects))
}
}

View file

@ -0,0 +1,17 @@
package database
import "github.com/gofiber/fiber/v2"
// Simple middleware that provides a shared database pool as a local key "db"
func DbMiddleware(db *Database) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
c.Locals("db", db)
return c.Next()
}
}
// Helper function to get the database from the context, without fiddling with casts
func GetDb(c *fiber.Ctx) Database {
// Dereference a pointer to a local, casted to a pointer to a Database
return *c.Locals("db").(*Database)
}

View file

@ -21,6 +21,12 @@ VALUES ("projecttest3","test project3", 1);
INSERT OR IGNORE INTO user_roles(user_id,project_id,p_role)
VALUES (1,1,"project_manager");
INSERT OR IGNORE INTO user_roles(user_id,project_id,p_role)
VALUES (1,2,"project_manager");
INSERT OR IGNORE INTO user_roles(user_id,project_id,p_role)
VALUES (1,3,"project_manager");
INSERT OR IGNORE INTO user_roles(user_id,project_id,p_role)
VALUES (2,1,"member");

View file

@ -1,41 +0,0 @@
package handlers
import (
"ttime/internal/database"
"github.com/gofiber/fiber/v2"
)
// The actual interface that we will use
type GlobalState interface {
Register(c *fiber.Ctx) error // To register a new user
UserDelete(c *fiber.Ctx) error // To delete a user
Login(c *fiber.Ctx) error // To get the token
LoginRenew(c *fiber.Ctx) error // To renew the token
CreateProject(c *fiber.Ctx) error // To create a new project
GetUserProjects(c *fiber.Ctx) error // To get all projects
SubmitWeeklyReport(c *fiber.Ctx) error
GetWeeklyReport(c *fiber.Ctx) error
SignReport(c *fiber.Ctx) error
GetProject(c *fiber.Ctx) error
AddUserToProjectHandler(c *fiber.Ctx) error
PromoteToAdmin(c *fiber.Ctx) error
GetWeeklyReportsUserHandler(c *fiber.Ctx) error
IsProjectManagerHandler(c *fiber.Ctx) error
DeleteProject(c *fiber.Ctx) error // To delete a project // WIP
ListAllUsers(c *fiber.Ctx) error // To get a list of all users in the application database
ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project
ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project
ChangeUserName(c *fiber.Ctx) error // WIP
GetAllUsersProject(c *fiber.Ctx) error // WIP
}
// "Constructor"
func NewGlobalState(db database.Database) GlobalState {
return &GState{Db: db}
}
// The global state, which implements all the handlers
type GState struct {
Db database.Database
}

View file

@ -1,15 +0,0 @@
package handlers
import (
"testing"
"ttime/internal/database"
)
// The actual interface that we will use
func TestGlobalState(t *testing.T) {
db := database.DbConnect(":memory:")
gs := NewGlobalState(db)
if gs == nil {
t.Error("NewGlobalState returned nil")
}
}

View file

@ -1,236 +0,0 @@
package handlers
import (
"strconv"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// CreateProject is a simple handler that creates a new project
func (gs *GState) CreateProject(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
p := new(types.NewProject)
if err := c.BodyParser(p); err != nil {
return c.Status(400).SendString(err.Error())
}
// Get the username from the token and set it as the owner of the project
// This is ugly but
claims := user.Claims.(jwt.MapClaims)
owner := claims["name"].(string)
if err := gs.Db.AddProject(p.Name, p.Description, owner); err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Status(200).SendString("Project added")
}
func (gs *GState) DeleteProject(c *fiber.Ctx) error {
projectID := c.Params("projectID")
username := c.Params("username")
if err := gs.Db.DeleteProject(projectID, username); err != nil {
return c.Status(500).SendString((err.Error()))
}
return c.Status(200).SendString("Project deleted")
}
// GetUserProjects returns all projects that the user is a member of
func (gs *GState) GetUserProjects(c *fiber.Ctx) error {
username := c.Params("username")
if username == "" {
log.Info("No username provided")
return c.Status(400).SendString("No username provided")
}
// Then dip into the database to get the projects
projects, err := gs.Db.GetProjectsForUser(username)
if err != nil {
return c.Status(500).SendString(err.Error())
}
// Return a json serialized list of projects
return c.JSON(projects)
}
// ProjectRoleChange is a handler that changes a user's role within a project
func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error {
//check token and get username of current user
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract the necessary parameters from the request
data := new(types.RoleChange)
if err := c.BodyParser(data); err != nil {
log.Info("error parsing username, project or role")
return c.Status(400).SendString(err.Error())
}
log.Info("Changing role for user: ", username, " in project: ", data.Projectname, " to: ", data.Role)
// Dubble diping and checcking if current user is
if ismanager, err := gs.Db.IsProjectManager(username, data.Projectname); err != nil {
log.Warn("Error checking if projectmanager:", err)
return c.Status(500).SendString(err.Error())
} else if !ismanager {
log.Warn("User is not projectmanager")
return c.Status(401).SendString("User is not projectmanager")
}
// Change the user's role within the project in the database
if err := gs.Db.ChangeUserRole(username, data.Projectname, data.Role); err != nil {
return c.Status(500).SendString(err.Error())
}
// Return a success message
return c.SendStatus(fiber.StatusOK)
}
// GetProject retrieves a specific project by its ID
func (gs *GState) GetProject(c *fiber.Ctx) error {
// Extract the project ID from the request parameters or body
projectID := c.Params("projectID")
if projectID == "" {
log.Info("No project ID provided")
return c.Status(400).SendString("No project ID provided")
}
log.Info("Getting project with ID: ", projectID)
// Parse the project ID into an integer
projectIDInt, err := strconv.Atoi(projectID)
if err != nil {
log.Info("Invalid project ID")
return c.Status(400).SendString("Invalid project ID")
}
// Get the project from the database by its ID
project, err := gs.Db.GetProject(projectIDInt)
if err != nil {
log.Info("Error getting project:", err)
return c.Status(500).SendString(err.Error())
}
// Return the project as JSON
log.Info("Returning project: ", project.Name)
return c.JSON(project)
}
func (gs *GState) ListAllUsersProject(c *fiber.Ctx) error {
// Extract the project name from the request parameters or body
projectName := c.Params("projectName")
if projectName == "" {
log.Info("No project name provided")
return c.Status(400).SendString("No project name provided")
}
// Get the user token
userToken := c.Locals("user").(*jwt.Token)
claims := userToken.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Check if the user is a project manager for the specified project
isManager, err := gs.Db.IsProjectManager(username, projectName)
if err != nil {
log.Info("Error checking project manager status:", err)
return c.Status(500).SendString(err.Error())
}
// If the user is not a project manager, check if the user is a site admin
if !isManager {
isAdmin, err := gs.Db.IsSiteAdmin(username)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
if !isAdmin {
log.Info("User is neither a project manager nor a site admin:", username)
return c.Status(403).SendString("User is neither a project manager nor a site admin")
}
}
// Get all users associated with the project from the database
users, err := gs.Db.GetAllUsersProject(projectName)
if err != nil {
log.Info("Error getting users for project:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Returning users for project: ", projectName)
// Return the list of users as JSON
return c.JSON(users)
}
// AddUserToProjectHandler is a handler that adds a user to a project with a specified role
func (gs *GState) AddUserToProjectHandler(c *fiber.Ctx) error {
// Extract necessary parameters from the request
var requestData struct {
Username string `json:"username"`
ProjectName string `json:"projectName"`
Role string `json:"role"`
}
if err := c.BodyParser(&requestData); err != nil {
log.Info("Error parsing request body:", err)
return c.Status(400).SendString("Bad request")
}
// Check if the user adding another user to the project is a site admin
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
adminUsername := claims["name"].(string)
log.Info("Admin username from claims:", adminUsername)
isAdmin, err := gs.Db.IsSiteAdmin(adminUsername)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
if !isAdmin {
log.Info("User is not a site admin:", adminUsername)
return c.Status(403).SendString("User is not a site admin")
}
// Add the user to the project with the specified role
err = gs.Db.AddUserToProject(requestData.Username, requestData.ProjectName, requestData.Role)
if err != nil {
log.Info("Error adding user to project:", err)
return c.Status(500).SendString(err.Error())
}
// Return success message
log.Info("User added to project successfully:", requestData.Username)
return c.SendStatus(fiber.StatusOK)
}
// IsProjectManagerHandler is a handler that checks if a user is a project manager for a given project
func (gs *GState) IsProjectManagerHandler(c *fiber.Ctx) error {
// Get the username from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract necessary parameters from the request query string
projectName := c.Params("projectName")
log.Info("Checking if user ", username, " is a project manager for project ", projectName)
// Check if the user is a project manager for the specified project
isManager, err := gs.Db.IsProjectManager(username, projectName)
if err != nil {
log.Info("Error checking project manager status:", err)
return c.Status(500).SendString(err.Error())
}
// Return the result as JSON
return c.JSON(fiber.Map{"isProjectManager": isManager})
}

View file

@ -1,143 +0,0 @@
package handlers
import (
"strconv"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func (gs *GState) SubmitWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
report := new(types.NewWeeklyReport)
if err := c.BodyParser(report); err != nil {
log.Info("Error parsing weekly report")
return c.Status(400).SendString(err.Error())
}
// Make sure all the fields of the report are valid
if report.Week < 1 || report.Week > 52 {
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number")
}
if report.DevelopmentTime < 0 || report.MeetingTime < 0 || report.AdminTime < 0 || report.OwnWorkTime < 0 || report.StudyTime < 0 || report.TestingTime < 0 {
log.Info("Invalid time report")
return c.Status(400).SendString("Invalid time report")
}
if err := gs.Db.AddWeeklyReport(report.ProjectName, username, report.Week, report.DevelopmentTime, report.MeetingTime, report.AdminTime, report.OwnWorkTime, report.StudyTime, report.TestingTime); err != nil {
log.Info("Error adding weekly report to db:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Weekly report added")
return c.Status(200).SendString("Time report added")
}
// Handler for retrieving weekly report
func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the request
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
log.Info("Getting weekly report for: ", username)
// Extract project name and week from query parameters
projectName := c.Query("projectName")
week := c.Query("week")
if projectName == "" || week == "" {
log.Info("Missing project name or week number")
return c.Status(400).SendString("Missing project name or week number")
}
// Convert week to integer
weekInt, err := strconv.Atoi(week)
if err != nil {
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number")
}
// Call the database function to get the weekly report
report, err := gs.Db.GetWeeklyReport(username, projectName, weekInt)
if err != nil {
log.Info("Error getting weekly report from db:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Returning weekly report")
// Return the retrieved weekly report
return c.JSON(report)
}
type ReportId struct {
ReportId int
}
func (gs *GState) SignReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
projectManagerUsername := claims["name"].(string)
log.Info("Signing report for: ", projectManagerUsername)
// Extract report ID from the request query parameters
// reportID := c.Query("reportId")
rid := new(ReportId)
if err := c.BodyParser(rid); err != nil {
return err
}
log.Info("Signing report for: ", rid.ReportId)
// Get the project manager's ID
projectManagerID, err := gs.Db.GetUserId(projectManagerUsername)
if err != nil {
log.Info("Failed to get project manager ID")
return c.Status(500).SendString("Failed to get project manager ID")
}
log.Info("Project manager ID: ", projectManagerID)
// Call the database function to sign the weekly report
err = gs.Db.SignWeeklyReport(rid.ReportId, projectManagerID)
if err != nil {
log.Info("Error signing weekly report:", err)
return c.Status(500).SendString(err.Error())
}
return c.Status(200).SendString("Weekly report signed successfully")
}
// GetWeeklyReportsUserHandler retrieves all weekly reports for a user in a specific project
func (gs *GState) GetWeeklyReportsUserHandler(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract necessary (path) parameters from the request
projectName := c.Params("projectName")
// TODO: Here we need to check whether the user is a member of the project
// If not, we should return an error. On the other hand, if the user not a member,
// the returned list of reports will (should) allways be empty.
// Retrieve weekly reports for the user in the project from the database
reports, err := gs.Db.GetWeeklyReportsUser(username, projectName)
if err != nil {
log.Error("Error getting weekly reports for user:", username, "in project:", projectName, ":", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Returning weekly reports for user:", username, "in project:", projectName)
// Return the list of reports as JSON
return c.JSON(reports)
}

View file

@ -1,269 +0,0 @@
package handlers
import (
"time"
"ttime/internal/types"
"github.com/gofiber/fiber/v2/log"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
)
// Register is a simple handler that registers a new user
//
// @Summary Register
// @Description Register a new user
// @Tags User
// @Accept json
// @Produce plain
// @Param NewUser body types.NewUser true "User to register"
// @Success 200 {string} string "User added"
// @Failure 400 {string} string "Bad request"
// @Failure 500 {string} string "Internal server error"
// @Router /register [post]
func (gs *GState) Register(c *fiber.Ctx) error {
u := new(types.NewUser)
if err := c.BodyParser(u); err != nil {
log.Warn("Error parsing body")
return c.Status(400).SendString(err.Error())
}
log.Info("Adding user:", u.Username)
if err := gs.Db.AddUser(u.Username, u.Password); err != nil {
log.Warn("Error adding user:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("User added:", u.Username)
return c.Status(200).SendString("User added")
}
// This path should obviously be protected in the future
// UserDelete deletes a user from the database
//
// @Summary UserDelete
// @Description UserDelete deletes a user from the database
// @Tags User
// @Accept json
// @Produce plain
// @Success 200 {string} string "User deleted"
// @Failure 403 {string} string "You can only delete yourself"
// @Failure 500 {string} string "Internal server error"
// @Failure 401 {string} string "Unauthorized"
// @Router /userdelete/{username} [delete]
func (gs *GState) UserDelete(c *fiber.Ctx) error {
// Read from path parameters
username := c.Params("username")
// Read username from Locals
auth_username := c.Locals("user").(*jwt.Token).Claims.(jwt.MapClaims)["name"].(string)
if username == auth_username {
log.Info("User tried to delete itself")
return c.Status(403).SendString("You can't delete yourself")
}
if err := gs.Db.RemoveUser(username); err != nil {
log.Warn("Error deleting user:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("User deleted:", username)
return c.Status(200).SendString("User deleted")
}
// Login is a simple login handler that returns a JWT token
//
// @Summary login
// @Description logs the user in and returns a jwt token
// @Tags User
// @Accept json
// @Param NewUser body types.NewUser true "login info"
// @Produce plain
// @Success 200 Token types.Token "Successfully signed token for user"
// @Failure 400 {string} string "Bad request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /login [post]
func (gs *GState) Login(c *fiber.Ctx) error {
// The body type is identical to a NewUser
u := new(types.NewUser)
if err := c.BodyParser(u); err != nil {
log.Warn("Error parsing body")
return c.Status(400).SendString(err.Error())
}
log.Info("Username logging in:", u.Username)
if !gs.Db.CheckUser(u.Username, u.Password) {
log.Info("User not found")
return c.SendStatus(fiber.StatusUnauthorized)
}
isAdmin, err := gs.Db.IsSiteAdmin(u.Username)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
// Create the Claims
claims := jwt.MapClaims{
"name": u.Username,
"admin": isAdmin,
"exp": time.Now().Add(time.Hour * 72).Unix(),
}
// Create token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
log.Info("Token created for user:", u.Username)
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte("secret"))
if err != nil {
log.Warn("Error signing token")
return c.SendStatus(fiber.StatusInternalServerError)
}
println("Successfully signed token for user:", u.Username)
return c.JSON(types.Token{Token: t})
}
// LoginRenew is a simple handler that renews the token
//
// @Summary LoginRenews
// @Description renews the users token
// @Security bererToken
// @Tags User
// @Accept json
// @Produce plain
// @Success 200 Token types.Token "Successfully signed token for user"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /loginerenew [post]
func (gs *GState) LoginRenew(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
log.Info("Renewing token for user:", user.Claims.(jwt.MapClaims)["name"])
claims := user.Claims.(jwt.MapClaims)
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
renewed := jwt.MapClaims{
"name": claims["name"],
"admin": claims["admin"],
"exp": claims["exp"],
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, renewed)
t, err := token.SignedString([]byte("secret"))
if err != nil {
log.Warn("Error signing token")
return c.SendStatus(fiber.StatusInternalServerError)
}
log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"])
return c.JSON(types.Token{Token: t})
}
// ListAllUsers is a handler that returns a list of all users in the application database
//
// @Summary ListsAllUsers
// @Description lists all users
// @Tags User
// @Accept json
// @Produce plain
// @Success 200 {json} json "Successfully signed token for user"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /users/all [get]
func (gs *GState) ListAllUsers(c *fiber.Ctx) error {
// Get all users from the database
users, err := gs.Db.GetAllUsersApplication()
if err != nil {
log.Info("Error getting users from db:", err) // Debug print
return c.Status(500).SendString(err.Error())
}
log.Info("Returning all users")
// Return the list of users as JSON
return c.JSON(users)
}
func (gs *GState) GetAllUsersProject(c *fiber.Ctx) error {
// Get all users from a project
projectName := c.Params("projectName")
users, err := gs.Db.GetAllUsersProject(projectName)
if err != nil {
log.Info("Error getting users from project:", err) // Debug print
return c.Status(500).SendString(err.Error())
}
log.Info("Returning all users")
// Return the list of users as JSON
return c.JSON(users)
}
// @Summary PromoteToAdmin
// @Description promote chosen user to admin
// @Tags User
// @Accept json
// @Produce plain
// @Param NewUser body types.NewUser true "user info"
// @Success 200 {json} json "Successfully promoted user"
// @Failure 400 {string} string "Bad request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /promoteToAdmin [post]
func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error {
// Extract the username from the request body
var newUser types.NewUser
if err := c.BodyParser(&newUser); err != nil {
return c.Status(400).SendString("Bad request")
}
username := newUser.Username
log.Info("Promoting user to admin:", username) // Debug print
// Promote the user to a site admin in the database
if err := gs.Db.PromoteToAdmin(username); err != nil {
log.Info("Error promoting user to admin:", err) // Debug print
return c.Status(500).SendString(err.Error())
}
log.Info("User promoted to admin successfully:", username) // Debug print
// Return a success message
return c.SendStatus(fiber.StatusOK)
}
// ChangeUserName changes a user's username in the database
func (gs *GState) ChangeUserName(c *fiber.Ctx) error {
// Check token and get username of current user
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
adminUsername := claims["name"].(string)
log.Info(adminUsername)
// Extract the necessary parameters from the request
data := new(types.StrNameChange)
if err := c.BodyParser(data); err != nil {
log.Info("Error parsing username")
return c.Status(400).SendString(err.Error())
}
// Check if the current user is an admin
isAdmin, err := gs.Db.IsSiteAdmin(adminUsername)
if err != nil {
log.Warn("Error checking if admin:", err)
return c.Status(500).SendString(err.Error())
} else if !isAdmin {
log.Warn("Tried changing name when not admin")
return c.Status(401).SendString("You cannot change name unless you are an admin")
}
// Change the user's name in the database
if err := gs.Db.ChangeUserName(data.PrevName, data.NewName); err != nil {
return c.Status(500).SendString(err.Error())
}
// Return a success message
return c.SendStatus(fiber.StatusOK)
}

View file

@ -0,0 +1,42 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// AddUserToProjectHandler is a handler that adds a user to a project with a specified role
func AddUserToProjectHandler(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
pm_name := claims["name"].(string)
project := c.Params("projectName")
username := c.Query("userName")
// Check if the user is a project manager
isPM, err := db.GetDb(c).IsProjectManager(pm_name, project)
if err != nil {
log.Info("Error checking if user is project manager:", err)
return c.Status(500).SendString(err.Error())
}
if !isPM {
log.Info("User: ", pm_name, " is not a project manager in project: ", project)
return c.Status(403).SendString("User is not a project manager")
}
// Add the user to the project with the specified role
err = db.GetDb(c).AddUserToProject(username, project, "member")
if err != nil {
log.Info("Error adding user to project:", err)
return c.Status(500).SendString(err.Error())
}
// Return success message
log.Info("User : ", username, " added to project: ", project)
return c.SendStatus(fiber.StatusOK)
}

View file

@ -0,0 +1,30 @@
package projects
import (
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
)
// CreateProject is a simple handler that creates a new project
func CreateProject(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
p := new(types.NewProject)
if err := c.BodyParser(p); err != nil {
return c.Status(400).SendString(err.Error())
}
// Get the username from the token and set it as the owner of the project
// This is ugly but
claims := user.Claims.(jwt.MapClaims)
owner := claims["name"].(string)
if err := db.GetDb(c).AddProject(p.Name, p.Description, owner); err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Status(200).SendString("Project added")
}

View file

@ -0,0 +1,19 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
)
func DeleteProject(c *fiber.Ctx) error {
projectID := c.Params("projectID")
username := c.Params("username")
if err := db.GetDb(c).DeleteProject(projectID, username); err != nil {
return c.Status(500).SendString((err.Error()))
}
return c.Status(200).SendString("Project deleted")
}

View file

@ -0,0 +1,38 @@
package projects
import (
"strconv"
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
// GetProject retrieves a specific project by its ID
func GetProject(c *fiber.Ctx) error {
// Extract the project ID from the request parameters or body
projectID := c.Params("projectID")
if projectID == "" {
log.Info("No project ID provided")
return c.Status(400).SendString("No project ID provided")
}
log.Info("Getting project with ID: ", projectID)
// Parse the project ID into an integer
projectIDInt, err := strconv.Atoi(projectID)
if err != nil {
log.Info("Invalid project ID")
return c.Status(400).SendString("Invalid project ID")
}
// Get the project from the database by its ID
project, err := db.GetDb(c).GetProject(projectIDInt)
if err != nil {
log.Info("Error getting project:", err)
return c.Status(500).SendString(err.Error())
}
// Return the project as JSON
log.Info("Returning project: ", project.Name)
return c.JSON(project)
}

View file

@ -0,0 +1,63 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func GetProjectTimesHandler(c *fiber.Ctx) error {
// Get the username from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Get project
projectName := c.Params("projectName")
if projectName == "" {
log.Info("No project name provided")
return c.Status(400).SendString("No project name provided")
}
// Get all users in the project and roles
userProjects, err := db.GetDb(c).GetAllUsersProject(projectName)
if err != nil {
log.Info("Error getting users in project:", err)
return c.Status(500).SendString(err.Error())
}
// If the user is member
isMember := false
for _, userProject := range userProjects {
if userProject.Username == username {
isMember = true
break
}
}
// If the user is admin
if !isMember {
isAdmin, err := db.GetDb(c).IsSiteAdmin(username)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
if !isAdmin {
log.Info("User is neither a project member nor a site admin:", username)
return c.Status(403).SendString("User is neither a project member nor a site admin")
}
}
// Get project times
projectTimes, err := db.GetDb(c).GetProjectTimes(projectName)
if err != nil {
log.Info("Error getting project times:", err)
return c.Status(500).SendString(err.Error())
}
// Return project times as JSON
log.Info("Returning project times for project:", projectName)
return c.JSON(projectTimes)
}

View file

@ -0,0 +1,26 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
// GetUserProjects returns all projects that the user is a member of
func GetUserProjects(c *fiber.Ctx) error {
username := c.Params("username")
if username == "" {
log.Info("No username provided")
return c.Status(400).SendString("No username provided")
}
// Then dip into the database to get the projects
projects, err := db.GetDb(c).GetProjectsForUser(username)
if err != nil {
return c.Status(500).SendString(err.Error())
}
// Return a json serialized list of projects
return c.JSON(projects)
}

View file

@ -0,0 +1,32 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// IsProjectManagerHandler is a handler that checks if a user is a project manager for a given project
func IsProjectManagerHandler(c *fiber.Ctx) error {
// Get the username from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract necessary parameters from the request query string
projectName := c.Params("projectName")
log.Info("Checking if user ", username, " is a project manager for project ", projectName)
// Check if the user is a project manager for the specified project
isManager, err := db.GetDb(c).IsProjectManager(username, projectName)
if err != nil {
log.Info("Error checking project manager status:", err)
return c.Status(500).SendString(err.Error())
}
// Return the result as JSON
return c.JSON(fiber.Map{"isProjectManager": isManager})
}

View file

@ -0,0 +1,55 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func ListAllUsersProject(c *fiber.Ctx) error {
// Extract the project name from the request parameters or body
projectName := c.Params("projectName")
if projectName == "" {
log.Info("No project name provided")
return c.Status(400).SendString("No project name provided")
}
// Get the user token
userToken := c.Locals("user").(*jwt.Token)
claims := userToken.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Check if the user is a project manager for the specified project
isManager, err := db.GetDb(c).IsProjectManager(username, projectName)
if err != nil {
log.Info("Error checking project manager status:", err)
return c.Status(500).SendString(err.Error())
}
// If the user is not a project manager, check if the user is a site admin
if !isManager {
isAdmin, err := db.GetDb(c).IsSiteAdmin(username)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
if !isAdmin {
log.Info("User is neither a project manager nor a site admin:", username)
return c.Status(403).SendString("User is neither a project manager nor a site admin")
}
}
// Get all users associated with the project from the database
users, err := db.GetDb(c).GetAllUsersProject(projectName)
if err != nil {
log.Info("Error getting users for project:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Returning users for project: ", projectName)
// Return the list of users as JSON
return c.JSON(users)
}

View file

@ -0,0 +1,51 @@
package projects
import (
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// ProjectRoleChange is a handler that changes a user's role within a project
func ProjectRoleChange(c *fiber.Ctx) error {
//check token and get username of current user
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract the necessary parameters from the request
data := new(types.RoleChange)
if err := c.BodyParser(data); err != nil {
log.Info("error parsing username, project or role")
return c.Status(400).SendString(err.Error())
}
// Check if user is trying to change its own role
if username == data.UserName {
log.Info("Can't change your own role")
return c.Status(403).SendString("Can't change your own role")
}
log.Info("Changing role for user: ", data.UserName, " in project: ", data.Projectname, " to: ", data.Role)
// Dubble diping and checcking if current user is
if ismanager, err := db.GetDb(c).IsProjectManager(username, data.Projectname); err != nil {
log.Warn("Error checking if projectmanager:", err)
return c.Status(500).SendString(err.Error())
} else if !ismanager {
log.Warn("User is not projectmanager")
return c.Status(401).SendString("User is not projectmanager")
}
// Change the user's role within the project in the database
if err := db.GetDb(c).ChangeUserRole(data.UserName, data.Projectname, data.Role); err != nil {
return c.Status(500).SendString(err.Error())
}
// Return a success message
return c.SendStatus(fiber.StatusOK)
}

View file

@ -0,0 +1,51 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// @Summary Promote to project manager
// @Description Promote a user to project manager
// @Tags Auth
// @Security JWT
// @Accept plain
// @Produce plain
// @Param projectName path string true "Project name"
// @Param userName query string true "User name"
// @Failure 500 {string} string "Internal server error"
// @Failure 403 {string} string "Forbidden"
// @Router /promote/{projectName} [put]
//
// Login logs in a user and returns a JWT token
// Promote to project manager
func PromoteToPm(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
pm_name := claims["name"].(string)
project := c.Params("projectName")
new_pm_name := c.Query("userName")
// Check if the user is a project manager
isPM, err := db.GetDb(c).IsProjectManager(pm_name, project)
if err != nil {
log.Info("Error checking if user is project manager:", err)
return c.Status(500).SendString(err.Error())
}
if !isPM {
log.Info("User: ", pm_name, " is not a project manager in project: ", project)
return c.Status(403).SendString("User is not a project manager")
}
// Add the user to the project with the specified role
err = db.GetDb(c).ChangeUserRole(new_pm_name, project, "project_manager")
// Return success message
log.Info("User : ", new_pm_name, " promoted to project manager in project: ", project)
return c.SendStatus(fiber.StatusOK)
}

View file

@ -0,0 +1,35 @@
package projects
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func RemoveProject(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Check if the user is a site admin
isAdmin, err := db.GetDb(c).IsSiteAdmin(username)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
if !isAdmin {
log.Info("User is not a site admin:", username)
return c.Status(403).SendString("User is not a site admin")
}
projectName := c.Params("projectName")
if err := db.GetDb(c).RemoveProject(projectName); err != nil {
return c.Status(500).SendString((err.Error()))
}
return c.Status(200).SendString("Project deleted")
}

View file

@ -0,0 +1,56 @@
package reports
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// GetAllWeeklyReports retrieves all weekly reports for a user in a specific project
func GetAllWeeklyReports(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract project name and week from query parameters
projectName := c.Params("projectName")
target_user := c.Query("targetUser") // The user whose reports are being requested
// If the target user is not empty, use it as the username
if target_user == "" {
target_user = username
}
log.Info(username, " trying to get all weekly reports for: ", target_user)
if projectName == "" {
log.Info("Missing project name")
return c.Status(400).SendString("Missing project name")
}
// If the user is not a project manager, they can only view their own reports
pm, err := db.GetDb(c).IsProjectManager(username, projectName)
if err != nil {
log.Info("Error checking if user is project manager:", err)
return c.Status(500).SendString(err.Error())
}
if pm == false && target_user != username {
log.Info("Unauthorized access")
return c.Status(403).SendString("Unauthorized access")
}
// Retrieve weekly reports for the user in the project from the database
reports, err := db.GetDb(c).GetAllWeeklyReports(target_user, projectName)
if err != nil {
log.Error("Error getting weekly reports for user:", target_user, "in project:", projectName, ":", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Returning weekly report")
// Return the retrieved weekly report
return c.JSON(reports)
}

View file

@ -0,0 +1,45 @@
package reports
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func GetUnsignedReports(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
projectManagerUsername := claims["name"].(string)
// Extract project name and week from query parameters
projectName := c.Params("projectName")
log.Info("Getting unsigned reports for")
if projectName == "" {
log.Info("Missing project name")
return c.Status(400).SendString("Missing project name")
}
// Get the project manager's ID
isProjectManager, err := db.GetDb(c).IsProjectManager(projectManagerUsername, projectName)
if err != nil {
log.Info("Failed to get project manager ID")
return c.Status(500).SendString("Failed to get project manager ID")
}
log.Info("User is Project Manager: ", isProjectManager)
// Call the database function to get the unsigned weekly reports
reports, err := db.GetDb(c).GetUnsignedWeeklyReports(projectName)
if err != nil {
log.Info("Error getting unsigned weekly reports:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Returning unsigned reports")
// Return the list of unsigned reports
return c.JSON(reports)
}

View file

@ -0,0 +1,65 @@
package reports
import (
"strconv"
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// Handler for retrieving weekly report
func GetWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the request
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract project name and week from query parameters
projectName := c.Query("projectName")
week := c.Query("week")
target_user := c.Query("targetUser") // The user whose report is being requested
// If the target user is not empty, use it as the username
if target_user == "" {
target_user = username
}
log.Info(username, " trying to get weekly report for: ", target_user)
if projectName == "" || week == "" {
log.Info("Missing project name or week number")
return c.Status(400).SendString("Missing project name or week number")
}
// Convert week to integer
weekInt, err := strconv.Atoi(week)
if err != nil {
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number")
}
// If the token user is not an admin, check if the target user is the same as the token user
pm, err := db.GetDb(c).IsProjectManager(username, projectName)
if err != nil {
log.Info("Error checking if user is project manager:", err)
return c.Status(500).SendString(err.Error())
}
if pm == false && target_user != username {
log.Info("Unauthorized access")
return c.Status(403).SendString("Unauthorized access")
}
// Call the database function to get the weekly report
report, err := db.GetDb(c).GetWeeklyReport(target_user, projectName, weekInt)
if err != nil {
log.Info("Error getting weekly report from db:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Returning weekly report")
// Return the retrieved weekly report
return c.JSON(report)
}

View file

@ -0,0 +1,41 @@
package reports
import (
"strconv"
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func SignReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
projectManagerUsername := claims["name"].(string)
// Extract report ID from the path
reportId, err := strconv.Atoi(c.Params("reportId"))
if err != nil {
log.Info("Invalid report ID")
return c.Status(400).SendString("Invalid report ID")
}
// Get the project manager's ID
projectManagerID, err := db.GetDb(c).GetUserId(projectManagerUsername)
if err != nil {
log.Info("Failed to get project manager ID for user: ", projectManagerUsername)
return c.Status(500).SendString("Failed to get project manager ID")
}
// Call the database function to sign the weekly report
err = db.GetDb(c).SignWeeklyReport(reportId, projectManagerID)
if err != nil {
log.Info("Error signing weekly report:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Project manager ID: ", projectManagerID, " signed report ID: ", reportId)
return c.Status(200).SendString("Weekly report signed successfully")
}

View file

@ -0,0 +1,41 @@
package reports
import (
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func SubmitWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
report := new(types.NewWeeklyReport)
if err := c.BodyParser(report); err != nil {
log.Info("Error parsing weekly report")
return c.Status(400).SendString(err.Error())
}
// Make sure all the fields of the report are valid
if report.Week < 1 || report.Week > 52 {
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number")
}
if report.DevelopmentTime < 0 || report.MeetingTime < 0 || report.AdminTime < 0 || report.OwnWorkTime < 0 || report.StudyTime < 0 || report.TestingTime < 0 {
log.Info("Invalid time report")
return c.Status(400).SendString("Invalid time report")
}
if err := db.GetDb(c).AddWeeklyReport(report.ProjectName, username, report.Week, report.DevelopmentTime, report.MeetingTime, report.AdminTime, report.OwnWorkTime, report.StudyTime, report.TestingTime); err != nil {
log.Info("Error adding weekly report to db:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Weekly report added")
return c.Status(200).SendString("Time report added")
}

View file

@ -0,0 +1,44 @@
package reports
import (
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func UpdateWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Parse the request body into an UpdateWeeklyReport struct
var updateReport types.UpdateWeeklyReport
if err := c.BodyParser(&updateReport); err != nil {
log.Info("Error parsing weekly report")
return c.Status(400).SendString(err.Error())
}
// Make sure all the fields of the report are valid
if updateReport.Week < 1 || updateReport.Week > 52 {
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number")
}
if updateReport.DevelopmentTime < 0 || updateReport.MeetingTime < 0 || updateReport.AdminTime < 0 || updateReport.OwnWorkTime < 0 || updateReport.StudyTime < 0 || updateReport.TestingTime < 0 {
log.Info("Invalid time report")
return c.Status(400).SendString("Invalid time report")
}
// Update the weekly report in the database
if err := db.GetDb(c).UpdateWeeklyReport(updateReport.ProjectName, username, updateReport.Week, updateReport.DevelopmentTime, updateReport.MeetingTime, updateReport.AdminTime, updateReport.OwnWorkTime, updateReport.StudyTime, updateReport.TestingTime); err != nil {
log.Info("Error updating weekly report in db:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("Weekly report updated")
return c.Status(200).SendString("Weekly report updated")
}

View file

@ -0,0 +1,44 @@
package users
import (
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// ChangeUserName changes a user's username in the database
func ChangeUserName(c *fiber.Ctx) error {
// Check token and get username of current user
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
adminUsername := claims["name"].(string)
log.Info(adminUsername)
// Extract the necessary parameters from the request
data := new(types.StrNameChange)
if err := c.BodyParser(data); err != nil {
log.Info("Error parsing username")
return c.Status(400).SendString(err.Error())
}
// Check if the current user is an admin
isAdmin, err := db.GetDb(c).IsSiteAdmin(adminUsername)
if err != nil {
log.Warn("Error checking if admin:", err)
return c.Status(500).SendString(err.Error())
} else if !isAdmin {
log.Warn("Tried changing name when not admin")
return c.Status(401).SendString("You cannot change name unless you are an admin")
}
// Change the user's name in the database
if err := db.GetDb(c).ChangeUserName(data.PrevName, data.NewName); err != nil {
return c.Status(500).SendString(err.Error())
}
// Return a success message
return c.SendStatus(fiber.StatusOK)
}

View file

@ -0,0 +1,22 @@
package users
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
func GetAllUsersProject(c *fiber.Ctx) error {
// Get all users from a project
projectName := c.Params("projectName")
users, err := db.GetDb(c).GetAllUsersProject(projectName)
if err != nil {
log.Info("Error getting users from project:", err) // Debug print
return c.Status(500).SendString(err.Error())
}
log.Info("Returning all users")
// Return the list of users as JSON
return c.JSON(users)
}

View file

@ -0,0 +1,32 @@
package users
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
// @Summary ListsAllUsers
// @Description lists all users
// @Tags User
// @Produce json
// @Security JWT
// @Success 200 {array} string "Successfully returned all users"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /users/all [get]
//
// ListAllUsers returns a list of all users in the application database
func ListAllUsers(c *fiber.Ctx) error {
// Get all users from the database
users, err := db.GetDb(c).GetAllUsersApplication()
if err != nil {
log.Info("Error getting users from db:", err) // Debug print
return c.Status(500).SendString(err.Error())
}
log.Info("Returning all users")
// Return the list of users as JSON
return c.JSON(users)
}

View file

@ -0,0 +1,66 @@
package users
import (
"time"
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// @Summary Login
// @Description Logs in a user and returns a JWT token
// @Tags Auth
// @Accept json
// @Produce json
// @Param body body types.NewUser true "User credentials"
// @Success 200 {object} types.Token "JWT token"
// @Failure 400 {string} string "Bad request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /login [post]
//
// Login logs in a user and returns a JWT token
func Login(c *fiber.Ctx) error {
// The body type is identical to a NewUser
u := new(types.NewUser)
if err := c.BodyParser(u); err != nil {
log.Warn("Error parsing body")
return c.Status(400).SendString(err.Error())
}
log.Info("Username logging in:", u.Username)
if !db.GetDb(c).CheckUser(u.Username, u.Password) {
log.Info("User not found")
return c.SendStatus(fiber.StatusUnauthorized)
}
isAdmin, err := db.GetDb(c).IsSiteAdmin(u.Username)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
// Create the Claims
claims := jwt.MapClaims{
"name": u.Username,
"admin": isAdmin,
"exp": time.Now().Add(time.Hour * 72).Unix(),
}
// Create token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
log.Info("Token created for user:", u.Username)
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte("secret"))
if err != nil {
log.Warn("Error signing token")
return c.SendStatus(fiber.StatusInternalServerError)
}
println("Successfully signed token for user:", u.Username)
return c.JSON(types.Token{Token: t})
}

View file

@ -0,0 +1,50 @@
package users
import (
"time"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// @Summary LoginRenews
// @Description Renews the users token.
// @Tags Auth
// @Produce json
// @Security JWT
// @Success 200 {object} types.Token "Successfully signed token for user"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /loginrenew [post]
//
// LoginRenew renews the users token
func LoginRenew(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
log.Info("Renewing token for user:", user.Claims.(jwt.MapClaims)["name"])
// Renewing the token means we trust whatever is already in the token
claims := user.Claims.(jwt.MapClaims)
// 72 hour expiration time
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
// Create token with old claims, but new expiration time
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"name": claims["name"],
"admin": claims["admin"],
"exp": claims["exp"],
})
// Sign it with top secret key
t, err := token.SignedString([]byte("secret"))
if err != nil {
log.Warn("Error signing token")
return c.SendStatus(fiber.StatusInternalServerError) // 500
}
log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"])
return c.JSON(types.Token{Token: t})
}

View file

@ -0,0 +1,45 @@
package users
import (
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
// @Summary PromoteToAdmin
// @Description Promote chosen user to site admin
// @Tags User
// @Accept json
// @Produce plain
// @Security JWT
// @Param NewUser body types.NewUser true "user info"
// @Success 200 {object} types.Token "Successfully promoted user"
// @Failure 400 {string} string "Bad request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 500 {string} string "Internal server error"
// @Router /promoteToAdmin [post]
//
// PromoteToAdmin promotes a user to a site admin
func PromoteToAdmin(c *fiber.Ctx) error {
// Extract the username from the request body
var newUser types.NewUser
if err := c.BodyParser(&newUser); err != nil {
return c.Status(400).SendString("Bad request")
}
username := newUser.Username
log.Info("Promoting user to admin:", username) // Debug print
// Promote the user to a site admin in the database
if err := db.GetDb(c).PromoteToAdmin(username); err != nil {
log.Info("Error promoting user to admin:", err) // Debug print
return c.Status(500).SendString(err.Error())
}
log.Info("User promoted to admin successfully:", username) // Debug print
// Return a success message
return c.SendStatus(fiber.StatusOK)
}

View file

@ -0,0 +1,38 @@
package users
import (
db "ttime/internal/database"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
// @Summary Register
// @Description Register a new user
// @Tags Auth
// @Accept json
// @Produce plain
// @Param NewUser body types.NewUser true "User to register"
// @Success 200 {string} string "User added"
// @Failure 400 {string} string "Bad request"
// @Failure 500 {string} string "Internal server error"
// @Router /register [post]
//
// Register is a simple handler that registers a new user
func Register(c *fiber.Ctx) error {
u := new(types.NewUser)
if err := c.BodyParser(u); err != nil {
log.Warn("Error parsing body")
return c.Status(400).SendString(err.Error())
}
log.Info("Adding user:", u.Username)
if err := db.GetDb(c).AddUser(u.Username, u.Password); err != nil {
log.Warn("Error adding user:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("User added:", u.Username)
return c.Status(200).SendString("User added")
}

View file

@ -0,0 +1,43 @@
package users
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// @Summary UserDelete
// @Description UserDelete deletes a user from the database
// @Tags User
// @Accept json
// @Produce plain
// @Security JWT
// @Success 200 {string} string "User deleted"
// @Failure 403 {string} string "You can only delete yourself"
// @Failure 500 {string} string "Internal server error"
// @Failure 401 {string} string "Unauthorized"
// @Router /userdelete/{username} [delete]
//
// UserDelete deletes a user from the database
func UserDelete(c *fiber.Ctx) error {
// Read from path parameters
username := c.Params("username")
// Read username from Locals
auth_username := c.Locals("user").(*jwt.Token).Claims.(jwt.MapClaims)["name"].(string)
if username == auth_username {
log.Info("User tried to delete itself")
return c.Status(403).SendString("You can't delete yourself")
}
if err := db.GetDb(c).RemoveUser(username); err != nil {
log.Warn("Error deleting user:", err)
return c.Status(500).SendString(err.Error())
}
log.Info("User deleted:", username)
return c.Status(200).SendString("User deleted")
}

View file

@ -65,3 +65,24 @@ type WeeklyReport struct {
// The project manager who signed it
SignedBy *int `json:"signedBy" db:"signed_by"`
}
type UpdateWeeklyReport struct {
// The name of the project, as it appears in the database
ProjectName string `json:"projectName"`
// The name of the user
UserName string `json:"userName"`
// The week number
Week int `json:"week"`
// Total time spent on development
DevelopmentTime int `json:"developmentTime"`
// Total time spent in meetings
MeetingTime int `json:"meetingTime"`
// Total time spent on administrative tasks
AdminTime int `json:"adminTime"`
// Total time spent on personal projects
OwnWorkTime int `json:"ownWorkTime"`
// Total time spent on studying
StudyTime int `json:"studyTime"`
// Total time spent on testing
TestingTime int `json:"testingTime"`
}

View file

@ -18,8 +18,8 @@ func (u *User) ToPublicUser() (*PublicUser, error) {
// Should be used when registering, for example
type NewUser struct {
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username" example:"username123"`
Password string `json:"password" example:"password123"`
}
// PublicUser represents a user that is safe to send over the API (no password)

View file

@ -6,7 +6,9 @@ import (
_ "ttime/docs"
"ttime/internal/config"
"ttime/internal/database"
"ttime/internal/handlers"
"ttime/internal/handlers/projects"
"ttime/internal/handlers/reports"
"ttime/internal/handlers/users"
"github.com/BurntSushi/toml"
"github.com/gofiber/fiber/v2"
@ -23,15 +25,22 @@ import (
// @license.name AGPL
// @license.url https://www.gnu.org/licenses/agpl-3.0.html
//@securityDefinitions.apikey bererToken
//@in header
//@name Authorization
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
// @description Use the JWT token provided by the login endpoint to authenticate requests. **Prefix the token with "Bearer ".**
// @host localhost:8080
// @BasePath /api
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
/**
Main function for starting the server and initializing configurations.
Reads configuration from file, pretty prints it, connects to the database,
migrates it, and sets up routes for the server.
*/
func main() {
conf, err := config.ReadConfigFromFile("config.toml")
@ -48,24 +57,28 @@ func main() {
// Connect to the database
db := database.DbConnect(conf.DbPath)
// Migrate the database
if err = db.Migrate(); err != nil {
fmt.Println("Error migrating database: ", err)
os.Exit(1)
}
// Migrate sample data, should not be used in production
if err = db.MigrateSampleData(); err != nil {
fmt.Println("Error migrating sample data: ", err)
os.Exit(1)
}
// Get our global state
gs := handlers.NewGlobalState(db)
// Create the server
server := fiber.New()
// We want some logs
server.Use(logger.New())
// Sets up db middleware, accessed as Local "db" key
server.Use(database.DbMiddleware(&db))
// Mounts the swagger documentation, this is available at /swagger/index.html
server.Get("/swagger/*", swagger.HandlerDefault)
@ -73,34 +86,52 @@ func main() {
// This will likely be replaced by an embedded filesystem in the future
server.Static("/", "./static")
// Register our unprotected routes
server.Post("/api/register", gs.Register)
server.Post("/api/login", gs.Login)
// Create a group for our API
api := server.Group("/api")
// Every route from here on will require a valid JWT
// Register our unprotected routes
api.Post("/register", users.Register)
api.Post("/login", users.Login)
// Every route from here on will require a valid
// JWT bearer token authentication in the header
server.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{Key: []byte("secret")},
}))
// Protected routes (require a valid JWT bearer token authentication header)
server.Post("/api/submitWeeklyReport", gs.SubmitWeeklyReport)
server.Get("/api/getUserProjects/:username", gs.GetUserProjects)
server.Post("/api/loginrenew", gs.LoginRenew)
server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches
server.Delete("api/project/:projectID", gs.DeleteProject) // WIP
server.Post("/api/project", gs.CreateProject) // WIP
server.Get("/api/project/:projectId", gs.GetProject)
server.Get("/api/project/getAllUsers", gs.GetAllUsersProject)
server.Get("/api/getWeeklyReport", gs.GetWeeklyReport)
server.Post("/api/signReport", gs.SignReport)
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
server.Put("/api/changeUserName", gs.ChangeUserName)
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
server.Get("/api/users/all", gs.ListAllUsers)
server.Get("/api/getWeeklyReportsUser/:projectName", gs.GetWeeklyReportsUserHandler)
server.Get("/api/checkIfProjectManager/:projectName", gs.IsProjectManagerHandler)
server.Post("/api/ProjectRoleChange", gs.ProjectRoleChange)
server.Get("/api/getUsersProject/:projectName", gs.ListAllUsersProject)
// All user related routes
// userGroup := api.Group("/user") // Not currently in use
api.Get("/users/all", users.ListAllUsers)
api.Get("/project/getAllUsers", users.GetAllUsersProject)
api.Post("/login", users.Login)
api.Post("/register", users.Register)
api.Post("/loginrenew", users.LoginRenew)
api.Post("/promoteToAdmin", users.PromoteToAdmin)
api.Put("/changeUserName", users.ChangeUserName)
api.Delete("/userdelete/:username", users.UserDelete) // Perhaps just use POST to avoid headaches
// All project related routes
// projectGroup := api.Group("/project") // Not currently in use
api.Get("/getProjectTimes/:projectName", projects.GetProjectTimesHandler)
api.Get("/getUserProjects/:username", projects.GetUserProjects)
api.Get("/project/:projectId", projects.GetProject)
api.Get("/checkIfProjectManager/:projectName", projects.IsProjectManagerHandler)
api.Get("/getUsersProject/:projectName", projects.ListAllUsersProject)
api.Post("/project", projects.CreateProject)
api.Post("/ProjectRoleChange", projects.ProjectRoleChange)
api.Put("/promoteToPm/:projectName", projects.PromoteToPm)
api.Put("/addUserToProject/:projectName", projects.AddUserToProjectHandler)
api.Delete("/removeProject/:projectName", projects.RemoveProject)
api.Delete("/project/:projectID", projects.DeleteProject)
// All report related routes
// reportGroup := api.Group("/report") // Not currently in use
api.Get("/getWeeklyReport", reports.GetWeeklyReport)
api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports)
api.Get("/getAllWeeklyReports/:projectName", reports.GetAllWeeklyReports)
api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport)
api.Put("/signReport/:reportId", reports.SignReport)
api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport)
// Announce the port we are listening on and start the server
err = server.Listen(fmt.Sprintf(":%d", conf.Port))

View file

@ -13,7 +13,6 @@ FROM docker.io/golang:alpine as go
RUN apk add gcompat
RUN apk add gcc
RUN apk add musl-dev
RUN apk add make
RUN apk add sqlite
WORKDIR /build
ADD backend/go.mod backend/go.sum ./
@ -24,9 +23,7 @@ RUN go mod download
# Add the source code
ADD backend .
RUN make migrate
# RUN go build -o server
RUN go build -o server
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o ./server ./main.go
# Strip the binary for a smaller image
@ -37,6 +34,7 @@ FROM docker.io/alpine:latest as runner
RUN adduser -D nonroot
RUN addgroup nonroot nonroot
WORKDIR /app
RUN chown nonroot:nonroot /app
# Copy the frontend SPA build into public
COPY --from=client /build/dist static
@ -44,9 +42,6 @@ COPY --from=client /build/dist static
# Copy the server binary
COPY --from=go /build/server server
# Copy the database
COPY --from=go /build/db.sqlite3 db.sqlite3
# Expose port 8080
EXPOSE 8080

2
frontend/.prettierignore Normal file
View file

@ -0,0 +1,2 @@
goTypes.ts
GenApi.ts

View file

@ -1,13 +1,16 @@
import { NewProjMember } from "../Components/AddMember";
import { ProjectRoleChange } from "../Components/ChangeRole";
import { projectTimes } from "../Components/GetProjectTimes";
import { ProjectMember } from "../Components/GetUsersInProject";
import {
UpdateWeeklyReport,
NewWeeklyReport,
NewUser,
User,
Project,
NewProject,
UserProjectMember,
WeeklyReport,
StrNameChange,
NewProjMember,
} from "../Types/goTypes";
/**
@ -49,7 +52,6 @@ interface API {
* @returns {Promise<APIResponse<boolean>>} A promise containing the API response indicating if the user is a project manager.
*/
checkIfProjectManager(
username: string,
projectName: string,
token: string,
): Promise<APIResponse<boolean>>;
@ -74,10 +76,7 @@ interface API {
* @param {string} token The authentication token.
* @returns {Promise<APIResponse<Project>>} A promise resolving to an API response with the created project.
*/
createProject(
project: NewProject,
token: string,
): Promise<APIResponse<Project>>;
createProject(project: NewProject, token: string): Promise<APIResponse<void>>;
/** Submits a weekly report
* @param {NewWeeklyReport} weeklyReport The weekly report object.
@ -87,18 +86,33 @@ interface API {
submitWeeklyReport(
weeklyReport: NewWeeklyReport,
token: string,
): Promise<APIResponse<NewWeeklyReport>>;
): Promise<APIResponse<string>>;
/** Gets a weekly report for a specific user, project and week
/**
* Updates a weekly report.
* @param {UpdateWeeklyReport} weeklyReport The updated weekly report object.
* @param {string} token The authentication token.
* @returns {Promise<APIResponse<string>>} A promise containing the API response with the updated report.
*/
updateWeeklyReport(
weeklyReport: UpdateWeeklyReport,
token: string,
): Promise<APIResponse<string>>;
/** Gets a weekly report for a specific user, project and week.
* Keep in mind that the user within the token needs to be PM
* of the project to get the report, unless the user is the target user.
* @param {string} projectName The name of the project.
* @param {string} week The week number.
* @param {string} token The authentication token.
* @param {string} targetUser The username of the target user. Defaults to token user.
* @returns {Promise<APIResponse<WeeklyReport>>} A promise resolving to an API response with the retrieved report.
*/
getWeeklyReport(
projectName: string,
week: string,
token: string,
targetUser?: string,
): Promise<APIResponse<WeeklyReport>>;
/**
@ -108,9 +122,10 @@ interface API {
* @param {string} token The token of the user
* @returns {APIResponse<WeeklyReport[]>} A list of weekly reports
*/
getWeeklyReportsForUser(
getAllWeeklyReportsForUser(
projectName: string,
token: string,
targetUser?: string,
): Promise<APIResponse<WeeklyReport[]>>;
/** Gets all the projects of a user
@ -129,6 +144,16 @@ interface API {
*/
getProject(id: number): Promise<APIResponse<Project>>;
/** Gets a projects reported time
* @param {string} projectName The name of the project.
* @param {string} token The usertoken.
* @returns {Promise<APIResponse<Times>>} A promise resolving to an API response containing the project times.
*/
getProjectTimes(
projectName: string,
token: string,
): Promise<APIResponse<projectTimes>>;
/** Gets a list of all users.
* @param {string} token The authentication token of the requesting user.
* @returns {Promise<APIResponse<string[]>>} A promise resolving to an API response containing the list of users.
@ -138,7 +163,18 @@ interface API {
getAllUsersProject(
projectName: string,
token: string,
): Promise<APIResponse<UserProjectMember[]>>;
): Promise<APIResponse<ProjectMember[]>>;
/** Gets all unsigned reports in a project.
* @param {string} projectName The name of the project.
* @param {string} token The authentication token.
* @returns {Promise<APIResponse<WeeklyReport[]>>} A promise resolving to an API response containing the list of unsigned reports.
*/
getUnsignedReportsInProject(
projectName: string,
token: string,
): Promise<APIResponse<WeeklyReport[]>>;
/**
* Changes the username of a user in the database.
* @param {StrNameChange} data The object containing the previous and new username.
@ -149,10 +185,48 @@ interface API {
data: StrNameChange,
token: string,
): Promise<APIResponse<void>>;
/**
* Changes the role of a user in the database.
* @param {RoleChange} roleInfo The object containing the previous and new username.
* @param {string} token The authentication token.
* @returns {Promise<APIResponse<void>>} A promise resolving to an API response.
*/
changeUserRole(
roleInfo: ProjectRoleChange,
token: string,
): Promise<APIResponse<void>>;
addUserToProject(
user: NewProjMember,
token: string,
): Promise<APIResponse<NewProjMember>>;
): Promise<APIResponse<void>>;
removeProject(
projectName: string,
token: string,
): Promise<APIResponse<string>>;
/**
* Signs a report. Keep in mind that the user which the token belongs to must be
* the project manager of the project the report belongs to.
*
* @param {number} reportId The id of the report to sign
* @param {string} token The authentication token
*/
signReport(reportId: number, token: string): Promise<APIResponse<string>>;
/**
* Promotes a user to project manager within a project.
*
* @param {string} userName The username of the user to promote
* @param {string} projectName The name of the project to promote the user in
* @returns {Promise<APIResponse<string>} A promise resolving to an API response.
*/
promoteToPm(
userName: string,
projectName: string,
token: string,
): Promise<APIResponse<string>>;
}
/** An instance of the API */
@ -208,19 +282,20 @@ export const api: API = {
},
async checkIfProjectManager(
username: string,
projectName: string,
token: string,
): Promise<APIResponse<boolean>> {
try {
const response = await fetch("/api/checkIfProjectManager", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
const response = await fetch(
`/api/checkIfProjectManager/${projectName}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
},
body: JSON.stringify({ username, projectName }),
});
);
if (!response.ok) {
return {
@ -232,14 +307,14 @@ export const api: API = {
return { success: true, data };
}
} catch (e) {
return { success: false, message: "fuck" };
return { success: false, message: "Failed to check if project manager" };
}
},
async createProject(
project: NewProject,
token: string,
): Promise<APIResponse<Project>> {
): Promise<APIResponse<void>> {
try {
const response = await fetch("/api/project", {
method: "POST",
@ -253,18 +328,17 @@ export const api: API = {
if (!response.ok) {
return { success: false, message: "Failed to create project" };
} else {
const data = (await response.json()) as Project;
return { success: true, data };
return { success: true };
}
} catch (e) {
return { success: false, message: "Failed to create project" };
return { success: false, message: "Failed to create project!" };
}
},
async addUserToProject(
user: NewProjMember,
token: string,
): Promise<APIResponse<NewProjMember>> {
): Promise<APIResponse<void>> {
try {
const response = await fetch("/api/addUserToProject", {
method: "PUT",
@ -306,6 +380,33 @@ export const api: API = {
}
},
async changeUserRole(
roleInfo: ProjectRoleChange,
token: string,
): Promise<APIResponse<void>> {
try {
const response = await fetch("/api/ProjectRoleChange", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(roleInfo),
});
if (!response.ok) {
if (response.status === 403) {
return { success: false, message: "Cannot change your own role" };
}
return { success: false, message: "Could not change role" };
} else {
return { success: true };
}
} catch (e) {
return { success: false, message: "Could not change role" };
}
},
async getUserProjects(
username: string,
token: string,
@ -336,10 +437,41 @@ export const api: API = {
}
},
async getProjectTimes(
projectName: string,
token: string,
): Promise<APIResponse<projectTimes>> {
try {
const response = await fetch(`/api/getProjectTimes/${projectName}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return Promise.resolve({
success: false,
message:
"Fetch error: " + response.status + ", failed to get project times",
});
} else {
const data = (await response.json()) as projectTimes;
return Promise.resolve({ success: true, data });
}
} catch (e) {
return Promise.resolve({
success: false,
message: "API error! Could not get times.",
});
}
},
async submitWeeklyReport(
weeklyReport: NewWeeklyReport,
token: string,
): Promise<APIResponse<NewWeeklyReport>> {
): Promise<APIResponse<string>> {
try {
const response = await fetch("/api/submitWeeklyReport", {
method: "POST",
@ -357,8 +489,8 @@ export const api: API = {
};
}
const data = (await response.json()) as NewWeeklyReport;
return { success: true, data };
const data = await response.text();
return { success: true, message: data };
} catch (e) {
return {
success: false,
@ -367,14 +499,46 @@ export const api: API = {
}
},
async updateWeeklyReport(
weeklyReport: UpdateWeeklyReport,
token: string,
): Promise<APIResponse<string>> {
try {
const response = await fetch("/api/updateWeeklyReport", {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(weeklyReport),
});
if (!response.ok) {
return {
success: false,
message: "Failed to update weekly report",
};
}
const data = await response.text();
return { success: true, message: data };
} catch (e) {
return {
success: false,
message: "Failed to update weekly report",
};
}
},
async getWeeklyReport(
projectName: string,
week: string,
token: string,
targetUser?: string,
): Promise<APIResponse<WeeklyReport>> {
try {
const response = await fetch(
`/api/getWeeklyReport?projectName=${projectName}&week=${week}`,
`/api/getWeeklyReport?projectName=${projectName}&week=${week}&targetUser=${targetUser}`,
{
method: "GET",
headers: {
@ -395,18 +559,22 @@ export const api: API = {
}
},
async getWeeklyReportsForUser(
async getAllWeeklyReportsForUser(
projectName: string,
token: string,
targetUser?: string,
): Promise<APIResponse<WeeklyReport[]>> {
try {
const response = await fetch(`/api/getWeeklyReportsUser/${projectName}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
const response = await fetch(
`/api/getAllWeeklyReports/${projectName}?targetUser=${targetUser}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
},
});
);
if (!response.ok) {
return {
@ -503,7 +671,7 @@ export const api: API = {
async getAllUsersProject(
projectName: string,
token: string,
): Promise<APIResponse<UserProjectMember[]>> {
): Promise<APIResponse<ProjectMember[]>> {
try {
const response = await fetch(`/api/getUsersProject/${projectName}`, {
method: "GET",
@ -519,7 +687,7 @@ export const api: API = {
message: "Failed to get users",
});
} else {
const data = (await response.json()) as UserProjectMember[];
const data = (await response.json()) as ProjectMember[];
return Promise.resolve({ success: true, data });
}
} catch (e) {
@ -530,6 +698,38 @@ export const api: API = {
}
},
async getUnsignedReportsInProject(
projectName: string,
token: string,
): Promise<APIResponse<WeeklyReport[]>> {
try {
const response = await fetch(`/api/getUnsignedReports/${projectName}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return {
success: false,
message:
"Failed to get unsigned reports for project: Response code " +
response.status,
};
} else {
const data = (await response.json()) as WeeklyReport[];
return { success: true, data };
}
} catch (e) {
return {
success: false,
message: "Failed to get unsigned reports for project, unknown error",
};
}
},
async changeUserName(
data: StrNameChange,
token: string,
@ -553,4 +753,88 @@ export const api: API = {
return { success: false, message: "Failed to change username" };
}
},
async removeProject(
projectName: string,
token: string,
): Promise<APIResponse<string>> {
try {
const response = await fetch(`/api/removeProject/${projectName}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return Promise.resolve({
success: false,
message: "Failed to remove project",
});
} else {
const data = await response.text();
return Promise.resolve({ success: true, message: data });
}
} catch (e) {
return Promise.resolve({
success: false,
message: "Failed to remove project",
});
}
},
async signReport(
reportId: number,
token: string,
): Promise<APIResponse<string>> {
try {
const response = await fetch(`/api/signReport/${reportId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return { success: false, message: "Failed to sign report" };
} else {
return { success: true, message: "Report signed" };
}
} catch (e) {
return { success: false, message: "Failed to sign report" };
}
},
async promoteToPm(
userName: string,
projectName: string,
token: string,
): Promise<APIResponse<string>> {
try {
const response = await fetch(
`/api/promoteToPm/${projectName}?userName=${userName}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
},
);
if (!response.ok) {
return {
success: false,
message: "Failed to promote user to project manager",
};
}
} catch (e) {
return {
success: false,
message: "Failed to promote user to project manager",
};
}
return { success: true, message: "User promoted to project manager" };
},
};

358
frontend/src/API/GenApi.ts Normal file
View file

@ -0,0 +1,358 @@
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/
export interface TypesNewUser {
/** @example "password123" */
password?: string;
/** @example "username123" */
username?: string;
}
export interface TypesToken {
token?: string;
}
export type QueryParamsType = Record<string | number, any>;
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
export interface FullRequestParams extends Omit<RequestInit, "body"> {
/** set parameter to `true` for call `securityWorker` for this request */
secure?: boolean;
/** request path */
path: string;
/** content type of request body */
type?: ContentType;
/** query params */
query?: QueryParamsType;
/** format of response (i.e. response.json() -> format: "json") */
format?: ResponseFormat;
/** request body */
body?: unknown;
/** base url */
baseUrl?: string;
/** request cancellation token */
cancelToken?: CancelToken;
}
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
export interface ApiConfig<SecurityDataType = unknown> {
baseUrl?: string;
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
customFetch?: typeof fetch;
}
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
data: D;
error: E;
}
type CancelToken = Symbol | string | number;
export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}
export class HttpClient<SecurityDataType = unknown> {
public baseUrl: string = "//localhost:8080/api";
private securityData: SecurityDataType | null = null;
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
private abortControllers = new Map<CancelToken, AbortController>();
private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams);
private baseApiParams: RequestParams = {
credentials: "same-origin",
headers: {},
redirect: "follow",
referrerPolicy: "no-referrer",
};
constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
Object.assign(this, apiConfig);
}
public setSecurityData = (data: SecurityDataType | null) => {
this.securityData = data;
};
protected encodeQueryParam(key: string, value: any) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
}
protected addQueryParam(query: QueryParamsType, key: string) {
return this.encodeQueryParam(key, query[key]);
}
protected addArrayQueryParam(query: QueryParamsType, key: string) {
const value = query[key];
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
}
protected toQueryString(rawQuery?: QueryParamsType): string {
const query = rawQuery || {};
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
return keys
.map((key) => (Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key)))
.join("&");
}
protected addQueryParams(rawQuery?: QueryParamsType): string {
const queryString = this.toQueryString(rawQuery);
return queryString ? `?${queryString}` : "";
}
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (input: any) =>
input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.Text]: (input: any) => (input !== null && typeof input !== "string" ? JSON.stringify(input) : input),
[ContentType.FormData]: (input: any) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
key,
property instanceof Blob
? property
: typeof property === "object" && property !== null
? JSON.stringify(property)
: `${property}`,
);
return formData;
}, new FormData()),
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
};
protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
return {
...this.baseApiParams,
...params1,
...(params2 || {}),
headers: {
...(this.baseApiParams.headers || {}),
...(params1.headers || {}),
...((params2 && params2.headers) || {}),
},
};
}
protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
if (this.abortControllers.has(cancelToken)) {
const abortController = this.abortControllers.get(cancelToken);
if (abortController) {
return abortController.signal;
}
return void 0;
}
const abortController = new AbortController();
this.abortControllers.set(cancelToken, abortController);
return abortController.signal;
};
public abortRequest = (cancelToken: CancelToken) => {
const abortController = this.abortControllers.get(cancelToken);
if (abortController) {
abortController.abort();
this.abortControllers.delete(cancelToken);
}
};
public request = async <T = any, E = any>({
body,
secure,
path,
type,
query,
format,
baseUrl,
cancelToken,
...params
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
const secureParams =
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
this.securityWorker &&
(await this.securityWorker(this.securityData))) ||
{};
const requestParams = this.mergeRequestParams(params, secureParams);
const queryString = query && this.toQueryString(query);
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
const responseFormat = format || requestParams.format;
return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, {
...requestParams,
headers: {
...(requestParams.headers || {}),
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
},
signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null,
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
}).then(async (response) => {
const r = response as HttpResponse<T, E>;
r.data = null as unknown as T;
r.error = null as unknown as E;
const data = !responseFormat
? r
: await response[responseFormat]()
.then((data) => {
if (r.ok) {
r.data = data;
} else {
r.error = data;
}
return r;
})
.catch((e) => {
r.error = e;
return r;
});
if (cancelToken) {
this.abortControllers.delete(cancelToken);
}
if (!response.ok) throw data;
return data;
});
};
}
/**
* @title TTime API
* @version 0.0.1
* @license AGPL (https://www.gnu.org/licenses/agpl-3.0.html)
* @baseUrl //localhost:8080/api
* @externalDocs https://swagger.io/resources/open-api/
* @contact
*
* This is the API for TTime, a time tracking application.
*/
export class GenApi<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
login = {
/**
* @description Logs in a user and returns a JWT token
*
* @tags Auth
* @name LoginCreate
* @summary Login
* @request POST:/login
*/
loginCreate: (body: TypesNewUser, params: RequestParams = {}) =>
this.request<TypesToken, string>({
path: `/login`,
method: "POST",
body: body,
type: ContentType.Json,
format: "json",
...params,
}),
};
loginrenew = {
/**
* @description Renews the users token.
*
* @tags Auth
* @name LoginrenewCreate
* @summary LoginRenews
* @request POST:/loginrenew
* @secure
*/
loginrenewCreate: (params: RequestParams = {}) =>
this.request<TypesToken, string>({
path: `/loginrenew`,
method: "POST",
secure: true,
format: "json",
...params,
}),
};
promoteToAdmin = {
/**
* @description Promote chosen user to site admin
*
* @tags User
* @name PromoteToAdminCreate
* @summary PromoteToAdmin
* @request POST:/promoteToAdmin
* @secure
*/
promoteToAdminCreate: (NewUser: TypesNewUser, params: RequestParams = {}) =>
this.request<TypesToken, string>({
path: `/promoteToAdmin`,
method: "POST",
body: NewUser,
secure: true,
type: ContentType.Json,
...params,
}),
};
register = {
/**
* @description Register a new user
*
* @tags Auth
* @name RegisterCreate
* @summary Register
* @request POST:/register
*/
registerCreate: (NewUser: TypesNewUser, params: RequestParams = {}) =>
this.request<string, string>({
path: `/register`,
method: "POST",
body: NewUser,
type: ContentType.Json,
...params,
}),
};
userdelete = {
/**
* @description UserDelete deletes a user from the database
*
* @tags User
* @name UserdeleteDelete
* @summary UserDelete
* @request DELETE:/userdelete/{username}
* @secure
*/
userdeleteDelete: (username: string, params: RequestParams = {}) =>
this.request<string, string>({
path: `/userdelete/${username}`,
method: "DELETE",
secure: true,
type: ContentType.Json,
...params,
}),
};
users = {
/**
* @description lists all users
*
* @tags User
* @name GetUsers
* @summary ListsAllUsers
* @request GET:/users/all
* @secure
*/
getUsers: (params: RequestParams = {}) =>
this.request<string[], string>({
path: `/users/all`,
method: "GET",
secure: true,
format: "json",
...params,
}),
};
}

View file

@ -1,5 +1,10 @@
import { APIResponse, api } from "../API/API";
import { NewProjMember } from "../Types/goTypes";
export interface NewProjMember {
username: string;
role: string;
projectname: string;
}
/**
* Tries to add a member to a project
@ -21,7 +26,7 @@ function AddMember(props: { memberToAdd: NewProjMember }): boolean {
props.memberToAdd,
localStorage.getItem("accessToken") ?? "",
)
.then((response: APIResponse<NewProjMember>) => {
.then((response: APIResponse<void>) => {
if (response.success) {
alert("Member added");
added = true;

View file

@ -1,6 +1,6 @@
import { useState } from "react";
import { APIResponse, api } from "../API/API";
import { NewProject, Project } from "../Types/goTypes";
import { NewProject } from "../Types/goTypes";
import InputField from "./InputField";
import Logo from "../assets/Logo.svg";
import Button from "./Button";
@ -10,27 +10,26 @@ import Button from "./Button";
* @param {Object} props - Project name and description
* @returns {boolean} True if created, false if not
*/
function CreateProject(props: { name: string; description: string }): boolean {
function CreateProject(props: { name: string; description: string }): void {
const project: NewProject = {
name: props.name,
description: props.description,
};
let created = false;
api
.createProject(project, localStorage.getItem("accessToken") ?? "")
.then((response: APIResponse<Project>) => {
.then((response: APIResponse<void>) => {
if (response.success) {
created = true;
alert("Project added!");
} else {
alert("Project NOT added!");
console.error(response.message);
}
})
.catch((error) => {
alert("Project NOT added!");
console.error("An error occurred during creation:", error);
});
return created;
}
/**
@ -48,7 +47,10 @@ function AddProject(): JSX.Element {
className="bg-white rounded px-8 pt-6 pb-8 mb-4 items-center justify-center flex flex-col w-fit h-fit"
onSubmit={(e) => {
e.preventDefault();
CreateProject({ name: name, description: description });
CreateProject({
name: name,
description: description,
});
}}
>
<img
@ -59,22 +61,26 @@ function AddProject(): JSX.Element {
<h3 className="pb-4 mb-2 text-center font-bold text-[18px]">
Create a new project
</h3>
<InputField
label="Name"
type="text"
value={name}
onChange={(e) => {
setName(e.target.value);
}}
/>
<InputField
label="Description"
type="text"
value={description}
onChange={(e) => {
setDescription(e.target.value);
}}
/>
<div className="space-y-3">
<InputField
label="Name"
type="text"
value={name}
onChange={(e) => {
e.preventDefault();
setName(e.target.value);
}}
/>
<InputField
label="Description"
type="text"
value={description}
onChange={(e) => {
e.preventDefault();
setDescription(e.target.value);
}}
/>
</div>
<div className="flex items-center justify-between">
<Button
text="Create"

View file

@ -1,15 +1,14 @@
import { useState } from "react";
import { NewProjMember } from "../Types/goTypes";
import Button from "./Button";
import GetAllUsers from "./GetAllUsers";
import AddMember from "./AddMember";
import AddMember, { NewProjMember } from "./AddMember";
import BackButton from "./BackButton";
/**
* Provides UI for adding a member to a project.
* @returns {JSX.Element} - Returns the component UI for adding a member
*/
function AddUserToProject(): JSX.Element {
function AddUserToProject(props: { projectName: string }): JSX.Element {
const [name, setName] = useState("");
const [users, setUsers] = useState<string[]>([]);
const [role, setRole] = useState("");
@ -18,7 +17,7 @@ function AddUserToProject(): JSX.Element {
const handleClick = (): boolean => {
const newMember: NewProjMember = {
username: name,
projectname: localStorage.getItem("projectName") ?? "",
projectname: props.projectName,
role: role,
};
return AddMember({ memberToAdd: newMember });
@ -33,13 +32,13 @@ function AddUserToProject(): JSX.Element {
Role chosen: [{role}]
</p>
<p className="pb-4 mb-2 text-center font-bold text-[18px]">
Project chosen: [{localStorage.getItem("projectName") ?? ""}]
Project chosen: [{props.projectName}]
</p>
<p className="p-1">Choose role:</p>
<div className="border-2 border-black p-2 rounded-xl text-center h-[10h] w-[16vh]">
<div className="border-2 border-black p-2 rounded-xl text-center h-[10h] w-[16] overflow-auto">
<ul className="text-center items-center font-medium space-y-2">
<li
className="h-[10h] w-[14vh] items-start p-1 border-2 border-black rounded-full bg-orange-200 hover:bg-orange-600 hover:text-slate-100 hover:cursor-pointer"
className="h-[10] w-[14] items-start px-2 py-1 border-2 border-black rounded-full bg-orange-200 hover:bg-orange-600 hover:text-slate-100 hover:cursor-pointer"
onClick={() => {
setRole("member");
}}
@ -47,7 +46,7 @@ function AddUserToProject(): JSX.Element {
{"Member"}
</li>
<li
className="h-[10h] w-[14vh] items-start p-1 border-2 border-black rounded-full bg-orange-200 hover:bg-orange-600 hover:text-slate-100 hover:cursor-pointer"
className="h-[10] w-[14] items-start px-2 py-1 border-2 border-black rounded-full bg-orange-200 hover:bg-orange-600 hover:text-slate-100 hover:cursor-pointer"
onClick={() => {
setRole("project_manager");
}}

View file

@ -0,0 +1,73 @@
//Info: This component is used to display all the time reports for a project. It will display the week number,
//total time spent, and if the report has been signed or not. The user can click on a report to edit it.
import { useEffect, useState } from "react";
import { WeeklyReport } from "../Types/goTypes";
import { Link, useParams } from "react-router-dom";
import { api } from "../API/API";
/**
* Renders a component that displays all the time reports for a specific project.
* @returns {JSX.Element} representing the component.
*/
function AllTimeReportsInProject(): JSX.Element {
const { username } = useParams();
const { projectName } = useParams();
const [weeklyReports, setWeeklyReports] = useState<WeeklyReport[]>([]);
useEffect(() => {
const getWeeklyReports = async (): Promise<void> => {
const token = localStorage.getItem("accessToken") ?? "";
const response = await api.getWeeklyReportsForDifferentUser(
projectName ?? "",
username ?? "",
token,
);
console.log(response);
if (response.success) {
setWeeklyReports(response.data ?? []);
} else {
console.error(response.message);
}
};
void getWeeklyReports();
}, []);
return (
<>
<h1 className="text-[30px] font-bold">{username}&apos;s Time Reports</h1>
<div className="border-4 border-black bg-white flex flex-col items-center justify-center min-h-[65vh] h-fit w-[50vw] rounded-3xl content-center overflow-scroll space-y-[10vh] p-[30px] text-[30px]">
{weeklyReports.map((newWeeklyReport, index) => (
<Link
to={`/editOthersTR/${projectName}/${username}/${newWeeklyReport.week}`}
key={index}
className="border-b-2 border-black w-full"
>
<div className="flex justify-between">
<h1>
<span className="font-bold">{"Week: "}</span>
{newWeeklyReport.week}
</h1>
<h1>
<span className="font-bold">{"Total Time: "}</span>
{newWeeklyReport.developmentTime +
newWeeklyReport.meetingTime +
newWeeklyReport.adminTime +
newWeeklyReport.ownWorkTime +
newWeeklyReport.studyTime +
newWeeklyReport.testingTime}{" "}
min
</h1>
<h1>
<span className="font-bold">{"Signed: "}</span>
NO
</h1>
</div>
</Link>
))}
</div>
</>
);
}
export default AllTimeReportsInProject;

View file

@ -0,0 +1,37 @@
import { APIResponse, api } from "../API/API";
export interface ProjectRoleChange {
username: string;
role: "project_manager" | "member" | "";
projectname: string;
}
export default function ChangeRole(roleChangeInfo: ProjectRoleChange): void {
if (
roleChangeInfo.username === "" ||
roleChangeInfo.role === "" ||
roleChangeInfo.projectname === ""
) {
// FOR DEBUG
// console.log(roleChangeInfo.role + ": Role");
// console.log(roleChangeInfo.projectname + ": P-Name");
// console.log(roleChangeInfo.username + ": U-name");
alert("You have to select a role");
return;
}
api
.changeUserRole(roleChangeInfo, localStorage.getItem("accessToken") ?? "")
.then((response: APIResponse<void>) => {
if (response.success) {
alert("Role changed successfully");
location.reload();
} else {
alert(response.message);
console.error(response.message);
}
})
.catch((error) => {
alert(error);
console.error("An error occurred during change:", error);
});
}

View file

@ -0,0 +1,73 @@
import { useState } from "react";
import Button from "./Button";
import ChangeRole, { ProjectRoleChange } from "./ChangeRole";
export default function ChangeRoles(props: {
projectName: string;
username: string;
}): JSX.Element {
const [selectedRole, setSelectedRole] = useState<
"project_manager" | "member" | ""
>("");
const handleRoleChange = (
event: React.ChangeEvent<HTMLInputElement>,
): void => {
if (event.target.value === "member") {
setSelectedRole(event.target.value);
} else if (event.target.value === "project_manager") {
setSelectedRole(event.target.value);
}
};
const handleSubmit = (event: React.FormEvent<HTMLFormElement>): void => {
event.preventDefault();
const roleChangeInfo: ProjectRoleChange = {
username: props.username,
projectname: props.projectName,
role: selectedRole,
};
ChangeRole(roleChangeInfo);
};
return (
<div className="overflow-auto rounded-lg">
<h1 className="font-bold text-[20px]">Select role:</h1>
<form onSubmit={handleSubmit}>
<div className="h-[7vh] self-start text-left font-medium overflow-auto border-2 border-black rounded-lg p-2">
<div className="hover:font-bold">
<label>
<input
type="radio"
value="project_manager"
checked={selectedRole === "project_manager"}
onChange={handleRoleChange}
className="ml-2 mr-2 mb-3"
/>
Project manager
</label>
</div>
<div className="hover:font-bold">
<label>
<input
type="radio"
value="member"
checked={selectedRole === "member"}
onChange={handleRoleChange}
className="ml-2 mr-2"
/>
Member
</label>
</div>
</div>
<Button
text="Change"
onClick={(): void => {
return;
}}
type="submit"
/>
</form>
</div>
);
}

View file

@ -1,61 +1,26 @@
import React, { useState } from "react";
import InputField from "./InputField";
import { api } from "../API/API";
function ChangeUsername(): JSX.Element {
const [newUsername, setNewUsername] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
setNewUsername(e.target.value);
};
const handleSubmit = async (): Promise<void> => {
try {
// Call the API function to change the username
const token = localStorage.getItem("accessToken");
if (!token) {
throw new Error("Access token not found");
}
const response = await api.changeUserName(
{ prevName: "currentName", newName: newUsername },
token,
);
import { APIResponse, api } from "../API/API";
import { StrNameChange } from "../Types/goTypes";
function ChangeUsername(props: { nameChange: StrNameChange }): void {
if (props.nameChange.newName === "") {
alert("You have to select a new name");
return;
}
api
.changeUserName(props.nameChange, localStorage.getItem("accessToken") ?? "")
.then((response: APIResponse<void>) => {
if (response.success) {
// Optionally, add a success message or redirect the user
console.log("Username changed successfully");
alert("Name changed successfully");
location.reload();
} else {
// Handle the error message
console.error("Failed to change username:", response.message);
setErrorMessage(response.message ?? "Failed to change username");
alert("Name not changed");
console.error(response.message);
}
} catch (error) {
console.error("Error changing username:", error);
// Optionally, handle the error
setErrorMessage("Failed to change username");
}
};
const handleButtonClick = (): void => {
handleSubmit().catch((error) => {
console.error("Error in handleSubmit:", error);
})
.catch((error) => {
alert("Name not changed");
console.error("An error occurred during change:", error);
});
};
return (
<div>
<InputField
label="New Username"
type="text"
value={newUsername}
onChange={handleChange}
/>
{errorMessage && <div>{errorMessage}</div>}
<button onClick={handleButtonClick}>Update Username</button>
</div>
);
}
export default ChangeUsername;

View file

@ -0,0 +1,33 @@
import { api, APIResponse } from "../API/API";
/**
* Use to delete a project from the system
* @param {string} props.projectToDelete - The projectname of project to delete
* @returns {void} Nothing
* @example
* const exampleProjectName = "project";
* DeleteProject({ projectToDelete: exampleProjectName });
*/
function DeleteProject(props: { projectToDelete: string }): void {
api
.removeProject(
props.projectToDelete,
localStorage.getItem("accessToken") ?? "",
)
.then((response: APIResponse<string>) => {
if (response.success) {
alert("Project has been deleted!");
location.reload();
} else {
alert("Project has not been deleted");
console.error(response.message);
}
})
.catch((error) => {
alert("project has not been deleted");
console.error("An error occurred during deletion:", error);
});
}
export default DeleteProject;

View file

@ -3,7 +3,7 @@ import { api, APIResponse } from "../API/API";
/**
* Use to remove a user from the system
* @param props - The username of user to remove
* @param {string} props.usernameToDelete - The username of user to remove
* @returns {boolean} True if removed, false if not
* @example
* const exampleUsername = "user";
@ -29,7 +29,7 @@ function DeleteUser(props: { usernameToDelete: string }): boolean {
})
.catch((error) => {
alert("User has not been deleted");
console.error("An error occurred during creation:", error);
console.error("An error occurred during deletion:", error);
});
return removed;
}

View file

@ -0,0 +1,77 @@
import { useState, useEffect } from "react";
import { Link, useParams } from "react-router-dom";
import { api } from "../API/API";
import { WeeklyReport } from "../Types/goTypes";
/**
* Renders a component that displays the projects a user is a part of and links to the projects start-page.
* @returns The JSX element representing the component.
*/
function DisplayUserProject(): JSX.Element {
const { projectName } = useParams();
const [unsignedReports, setUnsignedReports] = useState<WeeklyReport[]>([]);
//const navigate = useNavigate();
useEffect(() => {
const getUnsignedReports = async (): Promise<void> => {
const token = localStorage.getItem("accessToken") ?? "";
const response = await api.getUnsignedReportsInProject(
projectName ?? "",
token,
);
console.log(response);
if (response.success) {
setUnsignedReports(response.data ?? []);
} else {
console.error(response.message);
}
};
void getUnsignedReports();
}, [projectName]); // Include 'projectName' in the dependency array
return (
<>
<h1 className="font-bold text-[30px] mb-[20px]">
All Unsigned Reports In: {projectName}{" "}
</h1>
<div className="border-4 border-black bg-white flex flex-col items-center justify-center min-h-[65vh] h-fit w-[70vw] rounded-3xl content-center overflow-scroll space-y-[10vh] p-[30px] text-[20px]">
{unsignedReports.map((unsignedReport: WeeklyReport, index: number) => (
<h1 key={index} className="border-b-2 border-black w-full">
<div className="flex justify-between">
<div className="flex">
<span className="ml-6 mr-2 font-bold">UserID:</span>
<h1>{unsignedReport.userId}</h1>
<span className="ml-6 mr-2 font-bold">Week:</span>
<h1>{unsignedReport.week}</h1>
<span className="ml-6 mr-2 font-bold">Total Time:</span>
<h1>
{unsignedReport.developmentTime +
unsignedReport.meetingTime +
unsignedReport.adminTime +
unsignedReport.ownWorkTime +
unsignedReport.studyTime +
unsignedReport.testingTime}
</h1>
<span className="ml-6 mr-2 font-bold">Signed:</span>
<h1>NO</h1>
</div>
<div className="flex">
<div className="ml-auto flex space-x-4">
<Link
to={`/PMViewUnsignedReport/${projectName}/${unsignedReport.userId}/${unsignedReport.week}`}
>
<h1 className="underline cursor-pointer font-bold">
View Report
</h1>
</Link>
</div>
</div>
</div>
</h1>
))}
</div>
</>
);
}
export default DisplayUserProject;

View file

@ -1,7 +1,8 @@
import { useState } from "react";
import { Project } from "../Types/goTypes";
import { Link } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import GetProjects from "./GetProjects";
import { api } from "../API/API";
/**
* Renders a component that displays the projects a user is a part of and links to the projects start-page.
@ -9,22 +10,45 @@ import GetProjects from "./GetProjects";
*/
function DisplayUserProject(): JSX.Element {
const [projects, setProjects] = useState<Project[]>([]);
const navigate = useNavigate();
GetProjects({
setProjectsProp: setProjects,
username: localStorage.getItem("username") ?? "",
});
const handleProjectClick = async (projectName: string): Promise<void> => {
const token = localStorage.getItem("accessToken") ?? "";
const response = await api.checkIfProjectManager(projectName, token);
console.log(response.data);
if (response.success) {
if (
(response.data as unknown as { isProjectManager: boolean })
.isProjectManager
) {
navigate(`/PMProjectPage/${projectName}`);
} else {
navigate(`/project/${projectName}`);
}
} else {
// handle error
console.error(response.message);
}
};
return (
<>
<h1 className="font-bold text-[30px] mb-[20px]">Your Projects</h1>
<div className="border-4 border-black bg-white flex flex-col items-center justify-center min-h-[65vh] h-fit w-[50vw] rounded-3xl content-center overflow-scroll space-y-[10vh] p-[30px]">
{projects.map((project, index) => (
<Link to={`/project/${project.name}`} key={index}>
{projects.map((project) => (
<div
onClick={() => void handleProjectClick(project.name)}
key={project.id}
>
<h1 className="font-bold underline text-[30px] cursor-pointer">
{project.name}
</h1>
</Link>
</div>
))}
</div>
</>

View file

@ -1,5 +1,5 @@
import { useState, useEffect } from "react";
import { WeeklyReport, NewWeeklyReport } from "../Types/goTypes";
import { WeeklyReport, UpdateWeeklyReport } from "../Types/goTypes";
import { api } from "../API/API";
import { useNavigate, useParams } from "react-router-dom";
import Button from "./Button";
@ -18,47 +18,52 @@ export default function GetWeeklyReport(): JSX.Element {
const [testingTime, setTestingTime] = useState(0);
const token = localStorage.getItem("accessToken") ?? "";
const { projectName } = useParams();
const { fetchedWeek } = useParams();
const fetchWeeklyReport = async (): Promise<void> => {
const response = await api.getWeeklyReport(
projectName ?? "",
fetchedWeek?.toString() ?? "0",
token,
);
if (response.success) {
const report: WeeklyReport = response.data ?? {
reportId: 0,
userId: 0,
projectId: 0,
week: 0,
developmentTime: 0,
meetingTime: 0,
adminTime: 0,
ownWorkTime: 0,
studyTime: 0,
testingTime: 0,
};
setWeek(report.week);
setDevelopmentTime(report.developmentTime);
setMeetingTime(report.meetingTime);
setAdminTime(report.adminTime);
setOwnWorkTime(report.ownWorkTime);
setStudyTime(report.studyTime);
setTestingTime(report.testingTime);
} else {
console.error("Failed to fetch weekly report:", response.message);
}
};
const { projectName, fetchedWeek } = useParams<{
projectName: string;
fetchedWeek: string;
}>();
const username = localStorage.getItem("userName") ?? "";
console.log(projectName, fetchedWeek);
useEffect(() => {
void fetchWeeklyReport();
});
const fetchWeeklyReport = async (): Promise<void> => {
const response = await api.getWeeklyReport(
projectName ?? "",
fetchedWeek ?? "",
token,
);
const handleNewWeeklyReport = async (): Promise<void> => {
const newWeeklyReport: NewWeeklyReport = {
if (response.success) {
const report: WeeklyReport = response.data ?? {
reportId: 0,
userId: 0,
projectId: 0,
week: 0,
developmentTime: 0,
meetingTime: 0,
adminTime: 0,
ownWorkTime: 0,
studyTime: 0,
testingTime: 0,
};
setWeek(report.week);
setDevelopmentTime(report.developmentTime);
setMeetingTime(report.meetingTime);
setAdminTime(report.adminTime);
setOwnWorkTime(report.ownWorkTime);
setStudyTime(report.studyTime);
setTestingTime(report.testingTime);
} else {
console.error("Failed to fetch weekly report:", response.message);
}
};
void fetchWeeklyReport();
}, [projectName, fetchedWeek, token]);
const handleUpdateWeeklyReport = async (): Promise<void> => {
const updateWeeklyReport: UpdateWeeklyReport = {
userName: username,
projectName: projectName ?? "",
week,
developmentTime,
@ -69,13 +74,14 @@ export default function GetWeeklyReport(): JSX.Element {
testingTime,
};
await api.submitWeeklyReport(newWeeklyReport, token);
await api.updateWeeklyReport(updateWeeklyReport, token);
};
const navigate = useNavigate();
return (
<>
<h1 className="font-bold text-[30px] mb-[20px]">Edit Time Report</h1>
<div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
<form
onSubmit={(e) => {
@ -85,29 +91,16 @@ export default function GetWeeklyReport(): JSX.Element {
return;
}
e.preventDefault();
void handleNewWeeklyReport();
void handleUpdateWeeklyReport();
alert("Changes submitted");
navigate(-1);
}}
>
<div className="flex flex-col items-center">
<input
className="w-fill h-[5vh] font-sans text-[3vh] pl-[1vw] rounded-full text-center pt-[1vh] pb-[1vh] border-2 border-black"
type="week"
placeholder="Week"
value={
week === 0 ? "" : `2024-W${week.toString().padStart(2, "0")}`
}
onChange={(e) => {
const weekNumber = parseInt(e.target.value.split("-W")[1]);
setWeek(weekNumber);
}}
onKeyDown={(event) => {
event.preventDefault();
}}
onPaste={(event) => {
event.preventDefault();
}}
/>
<div className="flex flex-col w-1/2 border-b-2 border-black items-center justify-center">
<h1 className="font-bold text-[30px]"> Week: {week}</h1>
</div>
<table className="w-full text-center divide-y divide-x divide-white text-[30px]">
<thead>
<tr>
@ -127,13 +120,23 @@ export default function GetWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={developmentTime}
value={developmentTime === 0 ? "" : developmentTime}
onChange={(e) => {
setDevelopmentTime(parseInt(e.target.value));
if (e.target.value === "") {
setDevelopmentTime(0);
return;
} else {
setDevelopmentTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -146,13 +149,23 @@ export default function GetWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={meetingTime}
value={meetingTime === 0 ? "" : meetingTime}
onChange={(e) => {
setMeetingTime(parseInt(e.target.value));
if (e.target.value === "") {
setMeetingTime(0);
return;
} else {
setMeetingTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -165,13 +178,23 @@ export default function GetWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={adminTime}
value={adminTime === 0 ? "" : adminTime}
onChange={(e) => {
setAdminTime(parseInt(e.target.value));
if (e.target.value === "") {
setAdminTime(0);
return;
} else {
setAdminTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -184,13 +207,23 @@ export default function GetWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={ownWorkTime}
value={ownWorkTime === 0 ? "" : ownWorkTime}
onChange={(e) => {
setOwnWorkTime(parseInt(e.target.value));
if (e.target.value === "") {
setOwnWorkTime(0);
return;
} else {
setOwnWorkTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -203,13 +236,23 @@ export default function GetWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={studyTime}
value={studyTime === 0 ? "" : studyTime}
onChange={(e) => {
setStudyTime(parseInt(e.target.value));
if (e.target.value === "") {
setStudyTime(0);
return;
} else {
setStudyTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -222,13 +265,23 @@ export default function GetWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={testingTime}
value={testingTime === 0 ? "" : testingTime}
onChange={(e) => {
setTestingTime(parseInt(e.target.value));
if (e.target.value === "") {
setTestingTime(0);
return;
} else {
setTestingTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>

View file

@ -0,0 +1,59 @@
import { Dispatch, SetStateAction, useEffect } from "react";
import { api } from "../API/API";
/**
* Interface for reported time per category + total time reported
*/
export interface projectTimes {
admin: number;
development: number;
meeting: number;
own_work: number;
study: number;
testing: number;
totalTime?: number;
}
/**
* Gets all reported times for this project
* @param {Dispatch} props.setTimesProp - A setStateAction for the map you want to put times in
* @param {string} props.projectName - Username
* @returns {void} Nothing
* @example
* const projectName = "Example";
* const [times, setTimes] = useState<Times>();
* GetProjectTimes({ setTimesProp: setTimes, projectName: projectName });
*/
function GetProjectTimes(props: {
setTimesProp: Dispatch<SetStateAction<projectTimes | undefined>>;
projectName: string;
}): void {
const setTimes: Dispatch<SetStateAction<projectTimes | undefined>> =
props.setTimesProp;
useEffect(() => {
const fetchUsers = async (): Promise<void> => {
try {
const token = localStorage.getItem("accessToken") ?? "";
const response = await api.getProjectTimes(props.projectName, token);
if (response.success && response.data) {
// Calculates total time reported
response.data.totalTime = response.data.admin;
response.data.totalTime += response.data.development;
response.data.totalTime += response.data.meeting;
response.data.totalTime += response.data.own_work;
response.data.totalTime += response.data.study;
response.data.totalTime += response.data.testing;
setTimes(response.data);
} else {
console.error("Failed to fetch project times:", response.message);
}
} catch (error) {
console.error("Error fetching times:", error);
}
};
void fetchUsers();
}, [props.projectName, setTimes]);
}
export default GetProjectTimes;

View file

@ -4,11 +4,13 @@ import { api } from "../API/API";
/**
* Gets all projects that user is a member of
* @param props - A setStateAction for the array you want to put projects in
* @param {Dispatch} props.setProjectsProp - A setStateAction for the array you want to put projects in
* @param {string} props.username - Username
* @returns {void} Nothing
* @example
* const username = "Example";
* const [projects, setProjects] = useState<Project[]>([]);
* GetAllUsers({ setProjectsProp: setProjects });
* GetProjects({ setProjectsProp: setProjects, username: username });
*/
function GetProjects(props: {
setProjectsProp: Dispatch<React.SetStateAction<Project[]>>;

View file

@ -1,20 +1,25 @@
import { Dispatch, useEffect } from "react";
import { UserProjectMember } from "../Types/goTypes";
import { api } from "../API/API";
export interface ProjectMember {
Username: string;
UserRole: string;
}
/**
* Gets all projects that user is a member of
* @param props - A setStateAction for the array you want to put projects in
* Gets all members of a project
* @param string - The project's name
* @param Dispatch - A setStateAction for the array you want to put members in
* @returns {void} Nothing
* @example
* const [projects, setProjects] = useState<Project[]>([]);
* GetAllUsers({ setProjectsProp: setProjects });
* const [users, setUsers] = useState<User[]>([]);
* GetUsersInProject({ projectName: props.projectname, setUsersProp: setUsers });
*/
function GetUsersInProject(props: {
projectName: string;
setUsersProp: Dispatch<React.SetStateAction<UserProjectMember[]>>;
setUsersProp: Dispatch<React.SetStateAction<ProjectMember[]>>;
}): void {
const setUsers: Dispatch<React.SetStateAction<UserProjectMember[]>> =
const setUsers: Dispatch<React.SetStateAction<ProjectMember[]>> =
props.setUsersProp;
useEffect(() => {
const fetchUsers = async (): Promise<void> => {
@ -24,10 +29,10 @@ function GetUsersInProject(props: {
if (response.success) {
setUsers(response.data ?? []);
} else {
console.error("Failed to fetch projects:", response.message);
console.error("Failed to fetch members:", response.message);
}
} catch (error) {
console.error("Error fetching projects:", error);
console.error("Error fetching members:", error);
}
};
void fetchUsers();

View file

@ -19,7 +19,7 @@ function InputField(props: {
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
}): JSX.Element {
return (
<div className="mb-4">
<div className="">
<label
className="block text-gray-700 text-sm font-sans font-bold mb-2"
htmlFor={props.label}

View file

@ -25,22 +25,24 @@ function Login(props: {
}): JSX.Element {
return (
<form className="flex flex-col items-center" onSubmit={props.handleSubmit}>
<InputField
type="text"
label="Username"
onChange={(e) => {
props.setUsername(e.target.value);
}}
value={props.username}
/>
<InputField
type="password"
label="Password"
onChange={(e) => {
props.setPassword(e.target.value);
}}
value={props.password}
/>
<div className="space-y-3">
<InputField
type="text"
label="Username"
onChange={(e) => {
props.setUsername(e.target.value);
}}
value={props.username}
/>
<InputField
type="password"
label="Password"
onChange={(e) => {
props.setPassword(e.target.value);
}}
value={props.password}
/>
</div>
<Button
text="Login"
onClick={(): void => {

View file

@ -0,0 +1,72 @@
import Button from "./Button";
import DeleteUser from "./DeleteUser";
import UserProjectListAdmin from "./UserProjectListAdmin";
import { useState } from "react";
import ChangeRoleView from "./ChangeRoleView";
function MemberInfoModal(props: {
projectName: string;
username: string;
onClose: () => void;
}): JSX.Element {
const [showRoles, setShowRoles] = useState(false);
const handleChangeRole = (): void => {
if (showRoles) {
setShowRoles(false);
} else {
setShowRoles(true);
}
};
return (
<div
className="fixed inset-0 bg-black bg-opacity-30 backdrop-blur-sm
flex justify-center items-center"
>
<div className="border-4 border-black bg-white rounded-lg text-center flex flex-col">
<div className="mx-10">
<p className="font-bold text-[30px]">{props.username}</p>
<p
className="hover:font-bold hover:cursor-pointer underline"
onClick={handleChangeRole}
>
(Change Role)
</p>
{showRoles && (
<ChangeRoleView
projectName={props.projectName}
username={props.username}
/>
)}
<h2 className="font-bold text-[20px]">Member of these projects:</h2>
<UserProjectListAdmin username={props.username} />
<div className="items-center space-x-6">
<Button
text={"Delete"}
onClick={function (): void {
if (
window.confirm("Are you sure you want to delete this user?")
) {
DeleteUser({
usernameToDelete: props.username,
});
}
}}
type="button"
/>
<Button
text={"Close"}
onClick={function (): void {
setShowRoles(false);
props.onClose();
}}
type="button"
/>
</div>
</div>
</div>
</div>
);
}
export default MemberInfoModal;

View file

@ -12,66 +12,103 @@ import Button from "./Button";
*/
export default function NewWeeklyReport(): JSX.Element {
const [week, setWeek] = useState<number>(0);
const [developmentTime, setDevelopmentTime] = useState<number>();
const [meetingTime, setMeetingTime] = useState<number>();
const [adminTime, setAdminTime] = useState<number>();
const [ownWorkTime, setOwnWorkTime] = useState<number>();
const [studyTime, setStudyTime] = useState<number>();
const [testingTime, setTestingTime] = useState<number>();
const [developmentTime, setDevelopmentTime] = useState<number>(0);
const [meetingTime, setMeetingTime] = useState<number>(0);
const [adminTime, setAdminTime] = useState<number>(0);
const [ownWorkTime, setOwnWorkTime] = useState<number>(0);
const [studyTime, setStudyTime] = useState<number>(0);
const [testingTime, setTestingTime] = useState<number>(0);
const { projectName } = useParams();
const token = localStorage.getItem("accessToken") ?? "";
const handleNewWeeklyReport = async (): Promise<void> => {
const handleNewWeeklyReport = async (): Promise<boolean> => {
const newWeeklyReport: NewWeeklyReport = {
projectName: projectName ?? "",
week: week,
developmentTime: developmentTime ?? 0,
meetingTime: meetingTime ?? 0,
adminTime: adminTime ?? 0,
ownWorkTime: ownWorkTime ?? 0,
studyTime: studyTime ?? 0,
testingTime: testingTime ?? 0,
developmentTime: developmentTime,
meetingTime: meetingTime,
adminTime: adminTime,
ownWorkTime: ownWorkTime,
studyTime: studyTime,
testingTime: testingTime,
};
await api.submitWeeklyReport(newWeeklyReport, token);
const response = await api.submitWeeklyReport(newWeeklyReport, token);
console.log(response);
if (response.success) {
return true;
} else {
return false;
}
};
const navigate = useNavigate();
// Check if the browser is Chrome or Edge
const isChromeOrEdge = /Chrome|Edg/.test(navigator.userAgent);
return (
<>
<div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
<form
onSubmit={(e) => {
if (week === 0) {
alert("Please enter a week number");
e.preventDefault();
return;
}
e.preventDefault();
void handleNewWeeklyReport();
navigate(-1);
void (async (): Promise<void> => {
if (week === 0 || week > 53 || week < 1) {
alert("Please enter a valid week number");
return;
}
const success = await handleNewWeeklyReport();
if (!success) {
alert(
"A Time Report for this week already exists, please go to the edit page to edit it or change week number.",
);
return;
}
alert("Weekly report submitted successfully");
navigate(-1);
})();
}}
>
<div className="flex flex-col items-center">
<input
className="w-fill h-[5vh] font-sans text-[3vh] pl-[1vw] rounded-full text-center pt-[1vh] pb-[1vh] border-2 border-black"
type="week"
placeholder="Week"
onChange={(e) => {
const weekNumber = parseInt(e.target.value.split("-W")[1]);
setWeek(weekNumber);
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
{isChromeOrEdge ? (
<input
className="w-fill h-[5vh] font-sans text-[3vh] pl-[1vw] rounded-full text-center pt-[1vh] pb-[1vh] border-2 border-black"
type="week"
placeholder="Week"
onChange={(e) => {
const weekNumber = parseInt(e.target.value.split("-W")[1]);
setWeek(weekNumber);
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
event.preventDefault();
}}
onPaste={(event) => {
event.preventDefault();
}}
onPaste={(event) => {
event.preventDefault();
}}
/>
}}
/>
) : (
<input
className="w-fill h-[5vh] font-sans text-[3vh] pl-[1vw] rounded-full text-center pt-[1vh] pb-[1vh] border-2 border-black"
type="text"
placeholder="Week (Numbers Only)"
onChange={(e) => {
const weekNumber = parseInt(e.target.value);
setWeek(weekNumber);
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
event.preventDefault();
}}
onPaste={(event) => {
event.preventDefault();
}}
/>
)}
<table className="w-full text-center divide-y divide-x divide-white text-[30px]">
<thead>
<tr>
@ -91,13 +128,23 @@ export default function NewWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={developmentTime}
value={developmentTime === 0 ? "" : developmentTime}
onChange={(e) => {
setDevelopmentTime(parseInt(e.target.value));
if (e.target.value === "") {
setDevelopmentTime(0);
return;
} else {
setDevelopmentTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -110,13 +157,23 @@ export default function NewWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={meetingTime}
value={meetingTime === 0 ? "" : meetingTime}
onChange={(e) => {
setMeetingTime(parseInt(e.target.value));
if (e.target.value === "") {
setMeetingTime(0);
return;
} else {
setMeetingTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -129,13 +186,23 @@ export default function NewWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={adminTime}
value={adminTime === 0 ? "" : adminTime}
onChange={(e) => {
setAdminTime(parseInt(e.target.value));
if (e.target.value === "") {
setAdminTime(0);
return;
} else {
setAdminTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -148,13 +215,23 @@ export default function NewWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={ownWorkTime}
value={ownWorkTime === 0 ? "" : ownWorkTime}
onChange={(e) => {
setOwnWorkTime(parseInt(e.target.value));
if (e.target.value === "") {
setOwnWorkTime(0);
return;
} else {
setOwnWorkTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -167,13 +244,23 @@ export default function NewWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={studyTime}
value={studyTime === 0 ? "" : studyTime}
onChange={(e) => {
setStudyTime(parseInt(e.target.value));
if (e.target.value === "") {
setStudyTime(0);
return;
} else {
setStudyTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>
@ -186,13 +273,23 @@ export default function NewWeeklyReport(): JSX.Element {
type="number"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={testingTime}
value={testingTime === 0 ? "" : testingTime}
onChange={(e) => {
setTestingTime(parseInt(e.target.value));
if (e.target.value === "") {
setTestingTime(0);
return;
} else {
setTestingTime(parseInt(e.target.value));
}
}}
onKeyDown={(event) => {
const keyValue = event.key;
if (!/\d/.test(keyValue) && keyValue !== "Backspace")
if (
!/\d/.test(keyValue) &&
keyValue !== "Backspace" &&
keyValue !== "ArrowLeft" &&
keyValue !== "ArrowRight"
)
event.preventDefault();
}}
/>

View file

@ -0,0 +1,153 @@
import { useState, useEffect } from "react";
import { WeeklyReport } from "../Types/goTypes";
import { api } from "../API/API";
import { useParams } from "react-router-dom";
/**
* Renders the component for editing a weekly report.
* @returns JSX.Element
*/
//This component does not yet work as intended. It is supposed to display the weekly report of a user in a project.
export default function OtherUsersTR(): JSX.Element {
const [week, setWeek] = useState(0);
const [developmentTime, setDevelopmentTime] = useState(0);
const [meetingTime, setMeetingTime] = useState(0);
const [adminTime, setAdminTime] = useState(0);
const [ownWorkTime, setOwnWorkTime] = useState(0);
const [studyTime, setStudyTime] = useState(0);
const [testingTime, setTestingTime] = useState(0);
const token = localStorage.getItem("accessToken") ?? "";
const { projectName } = useParams();
const { username } = useParams();
const { fetchedWeek } = useParams();
useEffect(() => {
const fetchUsersWeeklyReport = async (): Promise<void> => {
const response = await api.getWeeklyReport(
projectName ?? "",
fetchedWeek?.toString() ?? "0",
token,
);
if (response.success) {
const report: WeeklyReport = response.data ?? {
reportId: 0,
userId: 0,
projectId: 0,
week: 0,
developmentTime: 0,
meetingTime: 0,
adminTime: 0,
ownWorkTime: 0,
studyTime: 0,
testingTime: 0,
};
setWeek(report.week);
setDevelopmentTime(report.developmentTime);
setMeetingTime(report.meetingTime);
setAdminTime(report.adminTime);
setOwnWorkTime(report.ownWorkTime);
setStudyTime(report.studyTime);
setTestingTime(report.testingTime);
} else {
console.error("Failed to fetch weekly report:", response.message);
}
};
void fetchUsersWeeklyReport();
});
return (
<>
<h1 className="text-[30px] font-bold">{username}&apos;s Report</h1>
<div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
<div className="flex flex-col items-center">
<div className="flex flex-col w-1/2 border-b-2 border-black items-center justify-center">
<h1 className="font-bold text-[30px]"> Week: {week}</h1>
</div>
<table className="w-full text-center divide-y divide-x divide-white text-[30px]">
<thead>
<tr>
<th className="w-1/2 py-2 border-b-2 border-black">Activity</th>
<th className="w-1/2 py-2 border-b-2 border-black">
Total Time (min)
</th>
</tr>
</thead>
<tbody className="divide-y divide-black">
<tr className="h-[10vh]">
<td>Development</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={developmentTime === 0 ? "" : developmentTime}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Meeting</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={meetingTime === 0 ? "" : meetingTime}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Administration</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={adminTime === 0 ? "" : adminTime}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Own Work</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={ownWorkTime === 0 ? "" : ownWorkTime}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Studies</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={studyTime === 0 ? "" : studyTime}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Testing</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
value={testingTime === 0 ? "" : testingTime}
/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</>
);
}

View file

@ -1,31 +1,54 @@
import { useState } from "react";
import { useEffect, useRef, useState } from "react";
import Button from "./Button";
import { UserProjectMember } from "../Types/goTypes";
import GetUsersInProject from "./GetUsersInProject";
import GetUsersInProject, { ProjectMember } from "./GetUsersInProject";
import { Link } from "react-router-dom";
import GetProjectTimes, { projectTimes } from "./GetProjectTimes";
import DeleteProject from "./DeleteProject";
function ProjectInfoModal(props: {
isVisible: boolean;
projectname: string;
onClose: () => void;
onClick: (username: string) => void;
}): JSX.Element {
const [users, setUsers] = useState<UserProjectMember[]>([]);
const [users, setUsers] = useState<ProjectMember[]>([]);
const [times, setTimes] = useState<projectTimes>();
const totalTime = useRef(0);
GetUsersInProject({ projectName: props.projectname, setUsersProp: setUsers });
if (!props.isVisible) return <></>;
GetProjectTimes({ setTimesProp: setTimes, projectName: props.projectname });
useEffect(() => {
if (times?.totalTime !== undefined) {
totalTime.current = times.totalTime;
}
}, [times]);
return (
<div
className="fixed inset-0 bg-black bg-opacity-30 backdrop-blur-sm
flex justify-center items-center"
>
<div className="border-4 border-black bg-white p-2 rounded-2xl text-center h-[47vh] w-[40] flex flex-col">
<div className="pl-20 pr-20">
<div className="border-4 border-black bg-white p-2 rounded-2xl text-center h-[61vh] w-[40] overflow-auto">
<div className="pl-10 pr-10">
<h1 className="font-bold text-[32px] mb-[20px]">
{localStorage.getItem("projectName") ?? ""}
{props.projectname}
</h1>
<h2 className="font-bold text-[24px] mb-[20px]">Project members:</h2>
<div className="border-2 border-black p-2 rounded-lg text-center overflow-scroll h-[26vh]">
<div className="p-1 text-center">
<h2 className="text-[20px] font-bold">Statistics:</h2>
</div>
<div className="border-2 border-black rounded-lg h-[8vh] text-left divide-y-2 flex flex-col overflow-auto mx-10">
<p className="p-2">Number of members: {users.length}</p>
<p className="p-2">
Total time reported:{" "}
{Math.floor(totalTime.current / 60 / 24) + " d "}
{Math.floor((totalTime.current / 60) % 24) + " h "}
{(totalTime.current % 60) + " m "}
</p>
</div>
<div className="h-[6vh] p-7 text-center">
<h2 className="text-[20px] font-bold">Project members:</h2>
</div>
<div className="border-2 border-black p-2 rounded-lg text-center overflow-auto h-[24vh] mx-10">
<ul className="text-left font-medium space-y-2">
<div></div>
{users.map((user) => (
@ -45,31 +68,44 @@ function ProjectInfoModal(props: {
))}
</ul>
</div>
</div>
<div className="space-x-16">
<Button
text={"Delete"}
onClick={function (): void {
//DELETE PROJECT
}}
type="button"
/>
<Link to={"/adminProjectAddMember"}>
<div className="space-x-5 my-2">
<Button
text={"Add Member"}
text={"Delete"}
onClick={function (): void {
return;
if (
window.confirm(
"Are you sure you want to delete this project?",
)
) {
DeleteProject({
projectToDelete: props.projectname,
});
}
}}
type="button"
/>
</Link>
<Button
text={"Close"}
onClick={function (): void {
props.onClose();
}}
type="button"
/>
<Link
to={{
pathname: "/adminProjectAddMember",
search: props.projectname,
}}
>
<Button
text={"Add Member"}
onClick={function (): void {
return;
}}
type="button"
/>
</Link>
<Button
text={"Close"}
onClick={function (): void {
props.onClose();
}}
type="button"
/>
</div>
</div>
</div>
</div>

View file

@ -1,7 +1,7 @@
import { useState } from "react";
import { NewProject } from "../Types/goTypes";
import ProjectInfoModal from "./ProjectInfoModal";
import UserInfoModal from "./UserInfoModal";
import MemberInfoModal from "./MemberInfoModal";
/**
* A list of projects for admin manage projects page, that sets an onClick
@ -18,7 +18,7 @@ export function ProjectListAdmin(props: {
projects: NewProject[];
}): JSX.Element {
const [projectModalVisible, setProjectModalVisible] = useState(false);
const [projectname, setProjectname] = useState("");
const [projectName, setProjectName] = useState("");
const [userModalVisible, setUserModalVisible] = useState(false);
const [username, setUsername] = useState("");
@ -28,39 +28,36 @@ export function ProjectListAdmin(props: {
};
const handleClickProject = (projectname: string): void => {
setProjectname(projectname);
localStorage.setItem("projectName", projectname);
setProjectName(projectname);
setProjectModalVisible(true);
};
const handleCloseProject = (): void => {
setProjectname("");
setProjectName("");
setProjectModalVisible(false);
};
const handleCloseUser = (): void => {
setProjectname("");
setUsername("");
setUserModalVisible(false);
};
return (
<>
<ProjectInfoModal
onClose={handleCloseProject}
onClick={handleClickUser}
isVisible={projectModalVisible}
projectname={projectname}
/>
<UserInfoModal
manageMember={true}
onClose={handleCloseUser}
//TODO: CHANGE TO REMOVE USER FROM PROJECT
onDelete={() => {
return;
}}
isVisible={userModalVisible}
username={username}
/>
{projectModalVisible && (
<ProjectInfoModal
onClose={handleCloseProject}
onClick={handleClickUser}
projectname={projectName}
/>
)}
{userModalVisible && (
<MemberInfoModal
onClose={handleCloseUser}
username={username}
projectName={projectName}
/>
)}
<div>
<ul className="font-bold underline text-[30px] cursor-pointer padding">
{props.projects.map((project) => (

View file

@ -1,91 +1,39 @@
import { useEffect, useState } from "react";
import { useState } from "react";
import { Link, useParams } from "react-router-dom";
import GetUsersInProject, { ProjectMember } from "./GetUsersInProject";
function ProjectMembers(): JSX.Element {
const { projectName } = useParams();
const [projectMembers, setProjectMembers] = useState<ProjectMember[]>([]);
// const getProjectMembers = async (): Promise<void> => {
// const token = localStorage.getItem("accessToken") ?? "";
// const response = await api.getProjectMembers(projectName ?? "", token);
// console.log(response);
// if (response.success) {
// setProjectMembers(response.data ?? []);
// } else {
// console.error(response.message);
// }
// };
interface ProjectMember {
username: string;
role: string;
}
const mockProjectMembers = [
{
username: "username1",
role: "Project Manager",
},
{
username: "username2",
role: "System Manager",
},
{
username: "username3",
role: "Developer",
},
{
username: "username4",
role: "Tester",
},
{
username: "username5",
role: "Tester",
},
{
username: "username6",
role: "Tester",
},
];
const getProjectMembers = async (): Promise<void> => {
// Use the mock data
setProjectMembers(mockProjectMembers);
await Promise.resolve();
};
useEffect(() => {
void getProjectMembers();
GetUsersInProject({
projectName: projectName ?? "",
setUsersProp: setProjectMembers,
});
return (
<>
<h1 className="font-bold text-[30px] mb-[20px]">
All Members In: {projectName}{" "}
</h1>
<div className="border-4 border-black bg-white flex flex-col items-center justify-center min-h-[65vh] h-fit w-[70vw] rounded-3xl content-center overflow-scroll space-y-[10vh] p-[30px] text-[20px]">
{projectMembers.map((projectMember, index) => (
{projectMembers.map((projectMember: ProjectMember, index: number) => (
<h1 key={index} className="border-b-2 border-black w-full">
<div className="flex justify-between">
<div className="flex">
<h1>{projectMember.username}</h1>
<h1>{projectMember.Username}</h1>
<span className="ml-6 mr-2 font-bold">Role:</span>
<h1>{projectMember.role}</h1>
<h1>{projectMember.UserRole}</h1>
</div>
<div className="flex">
<div className="ml-auto flex space-x-4">
<Link
to={`/viewReports/${projectName}/${projectMember.username}`}
to={`/otherUsersTimeReports/${projectName}/${projectMember.Username}`}
>
<h1 className="underline cursor-pointer font-bold">
View Reports
</h1>
</Link>
<Link
to={`/changeRole/${projectName}/${projectMember.username}`}
>
<h1 className="underline cursor-pointer font-bold">
Change Role
</h1>
</Link>
</div>
</div>
</div>

View file

@ -22,6 +22,8 @@ export default function Register(): JSX.Element {
const response = await api.registerUser(newUser);
if (response.success) {
alert("User added!");
setPassword("");
setUsername("");
} else {
alert("User not added");
setErrMessage(response.message ?? "Unknown error");
@ -47,22 +49,24 @@ export default function Register(): JSX.Element {
<h3 className="pb-4 mb-2 text-center font-bold text-[18px]">
Register New User
</h3>
<InputField
label="Username"
type="text"
value={username ?? ""}
onChange={(e) => {
setUsername(e.target.value);
}}
/>
<InputField
label="Password"
type="password"
value={password ?? ""}
onChange={(e) => {
setPassword(e.target.value);
}}
/>
<div className="space-y-3">
<InputField
label="Username"
type="text"
value={username ?? ""}
onChange={(e) => {
setUsername(e.target.value);
}}
/>
<InputField
label="Password"
type="password"
value={password ?? ""}
onChange={(e) => {
setPassword(e.target.value);
}}
/>
</div>
<div className="flex items-center justify-between">
<Button
text="Register"

View file

@ -0,0 +1,138 @@
import { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
import { api } from "../API/API";
import { projectTimes } from "./GetProjectTimes";
/**
* Renders the component for showing total time per role in a project.
* @returns JSX.Element
*/
export default function TimePerRole(): JSX.Element {
const [development, setDevelopment] = useState<number>();
const [meeting, setMeeting] = useState<number>();
const [admin, setAdmin] = useState<number>();
const [own_work, setOwnWork] = useState<number>();
const [study, setStudy] = useState<number>();
const [testing, setTesting] = useState<number>();
const token = localStorage.getItem("accessToken") ?? "";
const { projectName } = useParams();
const fetchTimePerActivity = async (): Promise<void> => {
const response = await api.getProjectTimes(projectName ?? "", token);
{
if (response.success) {
const report: projectTimes = response.data ?? {
development: 0,
meeting: 0,
admin: 0,
own_work: 0,
study: 0,
testing: 0,
};
setDevelopment(report.development);
setMeeting(report.meeting);
setAdmin(report.admin);
setOwnWork(report.own_work);
setStudy(report.study);
setTesting(report.testing);
} else {
console.error("Failed to fetch weekly report:", response.message);
}
}
};
useEffect(() => {
void fetchTimePerActivity();
});
return (
<>
<h1 className="font-bold text-[30px] mb-[20px]">
Total Time Per Activity In: {projectName}{" "}
</h1>
<div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
<div className="flex flex-col items-center">
<table className="w-full text-center divide-y divide-x divide-white text-[30px]">
<thead>
<tr>
<th className="w-1/2 py-2 border-b-2 border-black">Activity</th>
<th className="w-1/2 py-2 border-b-2 border-black">
Total Time (min)
</th>
</tr>
</thead>
<tbody className="divide-y divide-black">
<tr className="h-[10vh]">
<td>Development</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={development}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Meeting</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={meeting}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Administration</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={admin}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Own Work</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={own_work}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Studies</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={study}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Testing</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={testing}
readOnly
/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</>
);
}

View file

@ -0,0 +1,141 @@
import { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
/**
* Renders the component for showing total time per role in a project.
* @returns JSX.Element
*/
export default function TimePerRole(): JSX.Element {
const [PManagerTime, setPManagerTime] = useState(0);
const [SManagerTime, setSManagerTime] = useState(0);
const [DeveloperTime, setDeveloperTime] = useState(0);
const [TesterTime, setTesterTime] = useState(0);
// const token = localStorage.getItem("accessToken") ?? "";
// const username = localStorage.getItem("username") ?? "";
const { projectName } = useParams();
// const fetchTimePerRole = async (): Promise<void> => {
// const response = await api.getTimePerRole(
// username,
// projectName ?? "",
// token,
// );
// {
// if (response.success) {
// const report: TimePerRole = response.data ?? {
// PManagerTime: 0,
// SManagerTime: 0,
// DeveloperTime: 0,
// TesterTime: 0,
// };
// } else {
// console.error("Failed to fetch weekly report:", response.message);
// }
// }
interface TimePerRole {
PManager: number;
SManager: number;
Developer: number;
Tester: number;
}
const fetchTimePerRole = async (): Promise<void> => {
// Use mock data
const report: TimePerRole = {
PManager: 120,
SManager: 80,
Developer: 200,
Tester: 150,
};
// Set the state with the mock data
setPManagerTime(report.PManager);
setSManagerTime(report.SManager);
setDeveloperTime(report.Developer);
setTesterTime(report.Tester);
await Promise.resolve();
};
useEffect(() => {
void fetchTimePerRole();
});
return (
<>
<h1 className="font-bold text-[30px] mb-[20px]">
Total Time Per Role In: {projectName}{" "}
</h1>
<div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
<div className="flex flex-col items-center">
<table className="w-full text-center divide-y divide-x divide-white text-[30px]">
<thead>
<tr>
<th className="w-1/2 py-2 border-b-2 border-black">Role</th>
<th className="w-1/2 py-2 border-b-2 border-black">
Total Time (min)
</th>
</tr>
</thead>
<tbody className="divide-y divide-black">
<tr className="h-[10vh]">
<td>Project Manager</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={PManagerTime}
onKeyDown={(event) => {
event.preventDefault();
}}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>System Manager</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={SManagerTime}
onKeyDown={(event) => {
event.preventDefault();
}}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Administration</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={DeveloperTime}
onKeyDown={(event) => {
event.preventDefault();
}}
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Own Work</td>
<td>
<input
type="string"
className="border-2 border-black rounded-md text-center w-1/2"
value={TesterTime}
onKeyDown={(event) => {
event.preventDefault();
}}
/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</>
);
}

View file

@ -1,72 +1,99 @@
import { Link } from "react-router-dom";
import Button from "./Button";
import DeleteUser from "./DeleteUser";
import UserProjectListAdmin from "./UserProjectListAdmin";
import { useState } from "react";
import InputField from "./InputField";
import ChangeUsername from "./ChangeUsername";
import { StrNameChange } from "../Types/goTypes";
function UserInfoModal(props: {
isVisible: boolean;
manageMember: boolean;
username: string;
onClose: () => void;
onDelete: (username: string) => void;
}): JSX.Element {
if (!props.isVisible) return <></>;
const ManageUserOrMember = (check: boolean): JSX.Element => {
if (check) {
return (
<Link to="/AdminChangeRole">
<p className="mb-[20px] hover:font-bold hover:cursor-pointer underline">
(Change Role)
</p>
</Link>
);
const [showInput, setShowInput] = useState(false);
const [newUsername, setNewUsername] = useState("");
if (!props.isVisible) {
return <></>;
}
const handleChangeNameView = (): void => {
if (showInput) {
setShowInput(false);
} else {
setShowInput(true);
}
return (
<Link to="/AdminChangeUserName">
<p className="mb-[20px] hover:font-bold hover:cursor-pointer underline">
(Change Username)
</p>
</Link>
);
};
const handleClickChangeName = (): void => {
const nameChange: StrNameChange = {
prevName: props.username,
newName: newUsername,
};
ChangeUsername({ nameChange: nameChange });
};
return (
<div
className="fixed inset-0 bg-black bg-opacity-30 backdrop-blur-sm
flex justify-center items-center"
>
<div className="border-4 border-black bg-white p-2 rounded-lg text-center flex flex-col">
<p className="font-bold text-[30px]">{props.username}</p>
{ManageUserOrMember(props.manageMember)}
<div>
<h2 className="font-bold text-[22px] mb-[20px]">
Member of these projects:
</h2>
<div className="pr-6 pl-6">
<UserProjectListAdmin username={props.username} />
<div className="border-4 border-black bg-white rounded-lg text-center flex flex-col">
<div className="mx-10">
<p className="font-bold text-[30px]">{props.username}</p>
<p
className="mb-[10px] hover:font-bold hover:cursor-pointer underline"
onClick={handleChangeNameView}
>
(Change Username)
</p>
{showInput && (
<div>
<InputField
label={"New username"}
type={"text"}
value={newUsername}
onChange={function (e): void {
e.defaultPrevented;
setNewUsername(e.target.value);
}}
/>
<Button
text={"Change"}
onClick={function (): void {
handleClickChangeName();
}}
type={"submit"}
/>
</div>
)}
<h2 className="font-bold text-[20px]">Member of these projects:</h2>
<UserProjectListAdmin username={props.username} />
<div className="items-center space-x-6">
<Button
text={"Delete"}
onClick={function (): void {
if (
window.confirm("Are you sure you want to delete this user?")
) {
DeleteUser({
usernameToDelete: props.username,
});
}
}}
type="button"
/>
<Button
text={"Close"}
onClick={function (): void {
setNewUsername("");
setShowInput(false);
props.onClose();
}}
type="button"
/>
</div>
</div>
<div className="items-center space-x-6 pr-6 pl-6">
<Button
text={"Delete"}
onClick={function (): void {
if (
window.confirm("Are you sure you want to delete this user?")
) {
DeleteUser({
usernameToDelete: props.username,
});
}
}}
type="button"
/>
<Button
text={"Close"}
onClick={function (): void {
props.onClose();
}}
type="button"
/>
</div>
</div>
</div>
);

View file

@ -1,6 +1,5 @@
import { useState } from "react";
import UserInfoModal from "./UserInfoModal";
import DeleteUser from "./DeleteUser";
/**
* A list of users for admin manage users page, that sets an onClick
@ -30,9 +29,7 @@ export function UserListAdmin(props: { users: string[] }): JSX.Element {
return (
<>
<UserInfoModal
manageMember={false}
onClose={handleClose}
onDelete={() => DeleteUser}
isVisible={modalVisible}
username={username}
/>

View file

@ -1,35 +1,17 @@
import { useEffect, useState } from "react";
import { api } from "../API/API";
import { useState } from "react";
import { Project } from "../Types/goTypes";
import GetProjects from "./GetProjects";
function UserProjectListAdmin(props: { username: string }): JSX.Element {
const [projects, setProjects] = useState<Project[]>([]);
useEffect(() => {
const fetchProjects = async (): Promise<void> => {
try {
const token = localStorage.getItem("accessToken") ?? "";
const username = props.username;
const response = await api.getUserProjects(username, token);
if (response.success) {
setProjects(response.data ?? []);
} else {
console.error("Failed to fetch projects:", response.message);
}
} catch (error) {
console.error("Error fetching projects:", error);
}
};
void fetchProjects();
}, [props.username]);
GetProjects({ setProjectsProp: setProjects, username: props.username });
return (
<div className="border-2 border-black bg-white p-2 rounded-lg text-center">
<ul>
<div className="border-2 border-black bg-white rounded-lg text-left overflow-auto h-[15vh] font-medium">
<ul className="divide-y-2">
{projects.map((project) => (
<li key={project.id}>
<li className="mx-2 my-1" key={project.id}>
<span>{project.name}</span>
</li>
))}

View file

@ -0,0 +1,191 @@
import { useState, useEffect } from "react";
import { WeeklyReport } from "../Types/goTypes";
import { api } from "../API/API";
import { useNavigate, useParams } from "react-router-dom";
import Button from "./Button";
/**
* Renders the component for editing a weekly report.
* @returns JSX.Element
*/
//This component does not yet work as intended. It is supposed to display the weekly report of a user in a project.
export default function GetOtherUsersReport(): JSX.Element {
const [week, setWeek] = useState(0);
const [developmentTime, setDevelopmentTime] = useState(0);
const [meetingTime, setMeetingTime] = useState(0);
const [adminTime, setAdminTime] = useState(0);
const [ownWorkTime, setOwnWorkTime] = useState(0);
const [studyTime, setStudyTime] = useState(0);
const [testingTime, setTestingTime] = useState(0);
const [reportId, setReportId] = useState(0);
const token = localStorage.getItem("accessToken") ?? "";
const { projectName } = useParams();
const { username } = useParams();
const { fetchedWeek } = useParams();
useEffect(() => {
const fetchUsersWeeklyReport = async (): Promise<void> => {
const response = await api.getWeeklyReport(
projectName ?? "",
fetchedWeek?.toString() ?? "0",
token,
);
if (response.success) {
const report: WeeklyReport = response.data ?? {
reportId: 0,
userId: 0,
projectId: 0,
week: 0,
developmentTime: 0,
meetingTime: 0,
adminTime: 0,
ownWorkTime: 0,
studyTime: 0,
testingTime: 0,
};
setReportId(report.reportId);
setWeek(report.week);
setDevelopmentTime(report.developmentTime);
setMeetingTime(report.meetingTime);
setAdminTime(report.adminTime);
setOwnWorkTime(report.ownWorkTime);
setStudyTime(report.studyTime);
setTestingTime(report.testingTime);
} else {
console.error("Failed to fetch weekly report:", response.message);
}
};
void fetchUsersWeeklyReport();
});
const handleSignWeeklyReport = async (): Promise<void> => {
await api.signReport(reportId, token);
};
const navigate = useNavigate();
return (
<>
<h1 className="text-[30px] font-bold">
{" "}
UserId: {username}&apos;s Report
</h1>
<div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
<form
onSubmit={(e) => {
e.preventDefault();
void handleSignWeeklyReport();
alert("Report successfully signed!");
navigate(-1);
}}
>
<div className="flex flex-col items-center">
<div className="flex flex-col w-1/2 border-b-2 border-black items-center justify-center">
<h1 className="font-bold text-[30px]"> Week: {week}</h1>
</div>
<table className="w-full text-center divide-y divide-x divide-white text-[30px]">
<thead>
<tr>
<th className="w-1/2 py-2 border-b-2 border-black">
Activity
</th>
<th className="w-1/2 py-2 border-b-2 border-black">
Total Time (min)
</th>
</tr>
</thead>
<tbody className="divide-y divide-black">
<tr className="h-[10vh]">
<td>Development</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
defaultValue={
developmentTime === 0 ? "" : developmentTime
}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Meeting</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
defaultValue={meetingTime === 0 ? "" : meetingTime}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Administration</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
defaultValue={adminTime === 0 ? "" : adminTime}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Own Work</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
defaultValue={ownWorkTime === 0 ? "" : ownWorkTime}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Studies</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
defaultValue={studyTime === 0 ? "" : studyTime}
readOnly
/>
</td>
</tr>
<tr className="h-[10vh]">
<td>Testing</td>
<td>
<input
type="text"
min="0"
className="border-2 border-black rounded-md text-center w-1/2"
defaultValue={testingTime === 0 ? "" : testingTime}
readOnly
/>
</td>
</tr>
</tbody>
</table>
<Button
text="Sign Report"
onClick={(): void => {
return;
}}
type="submit"
/>
</div>
</form>
</div>
</>
);
}

View file

@ -0,0 +1,23 @@
import { useEffect } from "react";
import { GenApi } from "../API/GenApi";
// Instantiation of the API
const api = new GenApi();
export function GenApiDemo(): JSX.Element {
// Sync wrapper around the loginCreate method
const register = async (): Promise<void> => {
const response = await api.login.loginCreate({
username: "admin",
password: "admin",
});
console.log(response.data); // This should be the inner type of the response
};
// Call the wrapper
useEffect(() => {
void register();
});
return <></>;
}

View file

@ -1,28 +0,0 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button";
import ChangeUsername from "../../Components/ChangeUsername";
function AdminChangeUsername(): JSX.Element {
const content = (
<>
<ChangeUsername />
</>
);
const buttons = (
<>
<Button
text="Finish"
onClick={(): void => {
return;
}}
type="button"
/>
<BackButton />
</>
);
return <BasicWindow content={content} buttons={buttons} />;
}
export default AdminChangeUsername;

View file

@ -1,11 +1,11 @@
import { useLocation } from "react-router-dom";
import AddUserToProject from "../../Components/AddUserToProject";
import BasicWindow from "../../Components/BasicWindow";
function AdminProjectAddMember(): JSX.Element {
const content = <AddUserToProject />;
const projectName = useLocation().search.slice(1);
const content = <AddUserToProject projectName={projectName} />;
const buttons = <></>;
return <BasicWindow content={content} buttons={buttons} />;
}
export default AdminProjectAddMember;

View file

@ -1,23 +0,0 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button";
function AdminProjectChangeUserRole(): JSX.Element {
const content = <></>;
const buttons = (
<>
<Button
text="Change"
onClick={(): void => {
return;
}}
type="button"
/>
<BackButton />
</>
);
return <BasicWindow content={content} buttons={buttons} />;
}
export default AdminProjectChangeUserRole;

View file

@ -1,23 +0,0 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button";
function AdminProjectManageMembers(): JSX.Element {
const content = <></>;
const buttons = (
<>
<Button
text="Add Member"
onClick={(): void => {
return;
}}
type="button"
/>
<BackButton />
</>
);
return <BasicWindow content={content} buttons={buttons} />;
}
export default AdminProjectManageMembers;

View file

@ -1,23 +0,0 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button";
function AdminProjectPage(): JSX.Element {
const content = <></>;
const buttons = (
<>
<Button
text="Delete"
onClick={(): void => {
return;
}}
type="button"
/>
<BackButton />
</>
);
return <BasicWindow content={content} buttons={buttons} />;
}
export default AdminProjectPage;

View file

@ -1,23 +0,0 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button";
function AdminProjectViewMemberInfo(): JSX.Element {
const content = <></>;
const buttons = (
<>
<Button
text="Remove"
onClick={(): void => {
return;
}}
type="button"
/>
<BackButton />
</>
);
return <BasicWindow content={content} buttons={buttons} />;
}
export default AdminProjectViewMemberInfo;

View file

@ -1,28 +0,0 @@
import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button";
import BackButton from "../../Components/BackButton";
import UserProjectListAdmin from "../../Components/UserProjectListAdmin";
function AdminViewUserInfo(): JSX.Element {
const content = (
<>
<UserProjectListAdmin />
</>
);
const buttons = (
<>
<Button
text="Delete"
onClick={(): void => {
return;
}}
type="button"
/>
<BackButton />
</>
);
return <BasicWindow content={content} buttons={buttons} />;
}
export default AdminViewUserInfo;

View file

@ -4,23 +4,23 @@ body{
@keyframes backgroundTransition {
0% {
background-image: url('src/assets/1.jpg');
background-image: url('../assets/1.jpg');
animation-timing-function: ease-out;
}
25% {
background-image: url('src/assets/2.jpg');
background-image: url('../assets/2.jpg');
animation-timing-function: ease-in;
}
50% {
background-image: url('src/assets/3.jpg');
background-image: url('../assets/3.jpg');
animation-timing-function: ease-out;
}
75% {
background-image: url('src/assets/4.jpg');
background-image: url('../assets/4.jpg');
animation-timing-function: ease-in;
}
100% {
background-image: url('src/assets/1.jpg');
background-image: url('../assets/1.jpg');
animation-timing-function: ease-out;
}
}

View file

@ -1,8 +1,13 @@
import BasicWindow from "../../Components/BasicWindow";
import BackButton from "../../Components/BackButton";
import AllTimeReportsInProjectOtherUser from "../../Components/AllTimeReportsInProjectOtherUser";
function PMOtherUsersTR(): JSX.Element {
const content = <></>;
const content = (
<>
<AllTimeReportsInProjectOtherUser />
</>
);
const buttons = (
<>

View file

@ -8,16 +8,13 @@ function PMProjectMembers(): JSX.Element {
const { projectName } = useParams();
const content = (
<>
<h1 className="font-bold text-[30px] mb-[20px]">
All Members In: {projectName}{" "}
</h1>
<ProjectMembers />
</>
);
const buttons = (
<>
<Link to="/PM-time-activity">
<Link to={`/PMtimeactivity/${projectName}`}>
<Button
text="Time / Activity"
onClick={(): void => {
@ -26,15 +23,6 @@ function PMProjectMembers(): JSX.Element {
type={"button"}
/>
</Link>
<Link to="/PM-time-role">
<Button
text="Time / Role"
onClick={(): void => {
return;
}}
type={"button"}
/>
</Link>
<BackButton />
</>
);

View file

@ -1,14 +1,11 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow";
import TimeReport from "../../Components/NewWeeklyReport";
import TimePerActivity from "../../Components/TimePerActivity";
function PMTotalTimeActivity(): JSX.Element {
const content = (
<>
<h1 className="font-bold text-[30px] mb-[20px]">
Total Time Per Activity
</h1>
<TimeReport />
<TimePerActivity />
</>
);

View file

@ -1,8 +1,13 @@
import BasicWindow from "../../Components/BasicWindow";
import BackButton from "../../Components/BackButton";
import TimePerRole from "../../Components/TimePerRole";
function PMTotalTimeRole(): JSX.Element {
const content = <></>;
const content = (
<>
<TimePerRole />
</>
);
const buttons = (
<>

View file

@ -1,8 +1,13 @@
import BasicWindow from "../../Components/BasicWindow";
import BackButton from "../../Components/BackButton";
import DisplayUnsignedReports from "../../Components/DisplayUnsignedReports";
function PMUnsignedReports(): JSX.Element {
const content = <></>;
const content = (
<>
<DisplayUnsignedReports />
</>
);
const buttons = (
<>

View file

@ -0,0 +1,20 @@
import BasicWindow from "../../Components/BasicWindow";
import BackButton from "../../Components/BackButton";
import OtherUsersTR from "../../Components/OtherUsersTR";
function PMViewOtherUsersTR(): JSX.Element {
const content = (
<>
<OtherUsersTR />
</>
);
const buttons = (
<>
<BackButton />
</>
);
return <BasicWindow content={content} buttons={buttons} />;
}
export default PMViewOtherUsersTR;

View file

@ -1,34 +1,16 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button";
import TimeReport from "../../Components/NewWeeklyReport";
import ViewOtherTimeReport from "../../Components/ViewOtherTimeReport";
function PMViewUnsignedReport(): JSX.Element {
const content = (
<>
<h1 className="font-bold text-[30px] mb-[20px]">
Username&apos;s Time Report
</h1>
<TimeReport />
<ViewOtherTimeReport />
</>
);
const buttons = (
<>
<Button
text="Sign"
onClick={(): void => {
return;
}}
type="button"
/>
<Button
text="Save"
onClick={(): void => {
return;
}}
type="button"
/>
<BackButton />
</>
);

View file

@ -5,7 +5,6 @@ import EditWeeklyReport from "../../Components/EditWeeklyReport";
function UserEditTimeReportPage(): JSX.Element {
const content = (
<>
<h1 className="font-bold text-[30px] mb-[20px]">Edit Time Report</h1>
<EditWeeklyReport />
</>
);

View file

@ -124,6 +124,44 @@ export interface WeeklyReport {
*/
signedBy?: number /* int */;
}
export interface UpdateWeeklyReport {
/**
* The name of the project, as it appears in the database
*/
projectName: string;
/**
* The name of the user
*/
userName: string;
/**
* The week number
*/
week: number /* int */;
/**
* Total time spent on development
*/
developmentTime: number /* int */;
/**
* Total time spent in meetings
*/
meetingTime: number /* int */;
/**
* Total time spent on administrative tasks
*/
adminTime: number /* int */;
/**
* Total time spent on personal projects
*/
ownWorkTime: number /* int */;
/**
* Total time spent on studying
*/
studyTime: number /* int */;
/**
* Total time spent on testing
*/
testingTime: number /* int */;
}
//////////
// source: project.go
@ -151,16 +189,9 @@ export interface NewProject {
*/
export interface RoleChange {
username: string;
role: "project_manager" | "user";
role: 'project_manager' | 'user';
projectname: string;
}
export interface NewProjMember {
username: string;
projectname: string;
role: string;
}
export interface NameChange {
id: number /* int */;
name: string;
@ -191,11 +222,6 @@ export interface PublicUser {
userId: string;
username: string;
}
export interface UserProjectMember {
Username: string;
UserRole: string;
}
/**
* wrapper type for token
*/

View file

@ -18,19 +18,14 @@ import PMTotalTimeRole from "./Pages/ProjectManagerPages/PMTotalTimeRole.tsx";
import PMUnsignedReports from "./Pages/ProjectManagerPages/PMUnsignedReports.tsx";
import PMViewUnsignedReport from "./Pages/ProjectManagerPages/PMViewUnsignedReport.tsx";
import AdminManageUsers from "./Pages/AdminPages/AdminManageUsers.tsx";
import AdminViewUserInfo from "./Pages/AdminPages/AdminViewUserInfo.tsx";
import AdminManageProjects from "./Pages/AdminPages/AdminManageProjects.tsx";
import AdminAddProject from "./Pages/AdminPages/AdminAddProject.tsx";
import AdminAddUser from "./Pages/AdminPages/AdminAddUser.tsx";
import AdminChangeUsername from "./Pages/AdminPages/AdminChangeUsername.tsx";
import AdminProjectAddMember from "./Pages/AdminPages/AdminProjectAddMember.tsx";
import AdminProjectChangeUserRole from "./Pages/AdminPages/AdminProjectChangeUserRole.tsx";
import AdminProjectManageMembers from "./Pages/AdminPages/AdminProjectManageMembers.tsx";
import AdminProjectStatistics from "./Pages/AdminPages/AdminProjectStatistics.tsx";
import AdminProjectViewMemberInfo from "./Pages/AdminPages/AdminProjectViewMemberInfo.tsx";
import AdminProjectPage from "./Pages/AdminPages/AdminProjectPage.tsx";
import NotFoundPage from "./Pages/NotFoundPage.tsx";
import UnauthorizedPage from "./Pages/UnauthorizedPage.tsx";
import PMViewOtherUsersTR from "./Pages/ProjectManagerPages/PMViewOtherUsersTR.tsx";
// This is where the routes are mounted
const router = createBrowserRouter([
@ -60,7 +55,7 @@ const router = createBrowserRouter([
element: <UserViewTimeReportsPage />,
},
{
path: "/editTimeReport/:projectName/:weekNumber",
path: "/editTimeReport/:projectName/:fetchedWeek",
element: <UserEditTimeReportPage />,
},
{
@ -68,9 +63,13 @@ const router = createBrowserRouter([
element: <PMChangeRole />,
},
{
path: "/otherUsersTimeReports",
path: "/otherUsersTimeReports/:projectName/:username",
element: <PMOtherUsersTR />,
},
{
path: "/editOthersTR/:projectName/:username/:fetchedWeek",
element: <PMViewOtherUsersTR />,
},
{
path: "/projectMembers/:projectName",
element: <PMProjectMembers />,
@ -80,11 +79,11 @@ const router = createBrowserRouter([
element: <PMProjectPage />,
},
{
path: "/PMTimeActivity",
path: "/PMTimeActivity/:projectName",
element: <PMTotalTimeActivity />,
},
{
path: "/PMTimeRole",
path: "/PMTimeRole/:projectName",
element: <PMTotalTimeRole />,
},
{
@ -92,37 +91,17 @@ const router = createBrowserRouter([
element: <PMUnsignedReports />,
},
{
path: "/PMViewUnsignedReport",
path: "/PMViewUnsignedReport/:projectName/:username/:fetchedWeek",
element: <PMViewUnsignedReport />,
},
{
path: "/adminChangeUsername",
element: <AdminChangeUsername />,
},
{
path: "/adminProjectAddMember",
element: <AdminProjectAddMember />,
},
{
path: "/adminProjectChangeUserRole",
element: <AdminProjectChangeUserRole />,
},
{
path: "/adminProjectManageMembers",
element: <AdminProjectManageMembers />,
},
{
path: "/adminProjectPage",
element: <AdminProjectPage />,
},
{
path: "/adminProjectStatistics",
element: <AdminProjectStatistics />,
},
{
path: "/adminProjectViewMembers",
element: <AdminProjectViewMemberInfo />,
},
{
path: "/addProject",
element: <AdminAddProject />,
@ -131,10 +110,6 @@ const router = createBrowserRouter([
path: "/adminAddUser",
element: <AdminAddUser />,
},
{
path: "/adminUserInfo",
element: <AdminViewUserInfo />,
},
{
path: "/adminManageProject",
element: <AdminManageProjects />,

View file

@ -1,28 +0,0 @@
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y=
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

6
package-lock.json generated
View file

@ -1,6 +0,0 @@
{
"name": "TTime",
"lockfileVersion": 3,
"requires": true,
"packages": {}
}

151
testing/helpers.py Normal file
View file

@ -0,0 +1,151 @@
import requests
import string
import random
import json
# Helper function for the TTime API testing suite
# For style guide, see:
# https://peps.python.org/pep-0008/#function-and-variable-names
# https://google.github.io/styleguide/pyguide.html#316-naming
##################
## Static Paths ##
##################
base_url = "http://localhost:8080"
registerPath = base_url + "/api/register"
loginPath = base_url + "/api/login"
addProjectPath = base_url + "/api/project"
submitReportPath = base_url + "/api/submitWeeklyReport"
getWeeklyReportPath = base_url + "/api/getWeeklyReport"
getProjectPath = base_url + "/api/project"
signReportPath = base_url + "/api/signReport"
addUserToProjectPath = base_url + "/api/addUserToProject"
promoteToAdminPath = base_url + "/api/promoteToAdmin"
getUserProjectsPath = base_url + "/api/getUserProjects"
getAllWeeklyReportsPath = base_url + "/api/getAllWeeklyReports"
checkIfProjectManagerPath = base_url + "/api/checkIfProjectManager"
ProjectRoleChangePath = base_url + "/api/ProjectRoleChange"
getUsersProjectPath = base_url + "/api/getUsersProject"
getUnsignedReportsPath = base_url + "/api/getUnsignedReports"
getChangeUserNamePath = base_url + "/api/changeUserName"
getUpdateWeeklyReportPath = base_url + "/api/updateWeeklyReport"
removeProjectPath = base_url + "/api/removeProject"
promoteToPmPath = base_url + "/api/promoteToPm"
debug_output = True
def gprint(*args, **kwargs):
print("\033[92m", *args, "\033[00m", **kwargs)
def dprint(*args, **kwargs):
if debug_output:
print(*args, **kwargs)
def randomString(len=10):
"""Generate a random string of fixed length"""
letters = string.ascii_lowercase
return "".join(random.choice(letters) for i in range(len))
############ ############ ############ ############ ############
# Posts the username and password to the register endpoint
def register(username: string, password: string):
dprint("Registering with username: ", username, " and password: ", password)
response = requests.post(
registerPath, json={"username": username, "password": password}
)
dprint(response.text)
return response
# Posts the username and password to the login endpoint
def login(username: string, password: string):
dprint("Logging in with username: ", username, " and password: ", password)
response = requests.post(
loginPath, json={"username": username, "password": password}
)
dprint(response.text)
return response
# Register a user and return the token
def register_and_login(username: string, password: string) -> string:
register(username, password)
response = login(username, password)
return response.json()["token"]
def create_project(
token: string, project_name: string, description: string = "Test description"
):
dprint("Creating project with name: ", project_name)
response = requests.post(
addProjectPath,
headers={"Authorization": "Bearer " + token},
json={"name": project_name, "description": description},
)
dprint(response.text)
return response
# Add a user to a project, requires the user withing the token to be a project manager of said project
def addToProject(token: string, username: string, project_name: string):
dprint("Adding user with username: ", username, " to project: ", project_name)
response = requests.put(
addUserToProjectPath + "/" + project_name,
headers={"Authorization": "Bearer " + token},
params={"userName": username},
)
dprint(response.text)
return response
def promoteToManager(token: string, username: string, project_name: string):
dprint(
"Promoting user with username: ",
username,
" to project manager of project: ",
project_name,
)
response = requests.put(
promoteToPmPath + "/" + project_name,
headers={"Authorization": "Bearer " + token},
params={"userName": username},
)
dprint(response.text)
return response
def submitReport(token: string, report):
dprint("Submitting report: ", report)
response = requests.post(
submitReportPath,
json=report,
headers={"Authorization": "Bearer " + token},
)
return response
def getReport(token: string, username: string, projectName: string):
# Retrieve the report ID
response = requests.get(
getWeeklyReportPath,
headers={"Authorization": "Bearer " + token},
params={"username": username, "projectName": projectName, "week": 1},
)
return response.json()
def signReport(project_manager_token: string, report_id: int):
return requests.put(
signReportPath + "/" + str(report_id),
headers={"Authorization": "Bearer " + project_manager_token},
)

View file

@ -1,58 +1,23 @@
import requests
import string
import random
debug_output = False
def gprint(*args, **kwargs):
print("\033[92m", *args, "\033[00m", **kwargs)
# This modules contains helper functions for the tests
from helpers import *
print("Running Tests...")
def dprint(*args, **kwargs):
if debug_output:
print(*args, **kwargs)
def randomString(len=10):
"""Generate a random string of fixed length"""
letters = string.ascii_lowercase
return "".join(random.choice(letters) for i in range(len))
# Defined once per test run
username = "user_" + randomString()
projectName = "project_" + randomString()
# The base URL of the API
base_url = "http://localhost:8080"
# Endpoint to test
registerPath = base_url + "/api/register"
loginPath = base_url + "/api/login"
addProjectPath = base_url + "/api/project"
submitReportPath = base_url + "/api/submitWeeklyReport"
getWeeklyReportPath = base_url + "/api/getWeeklyReport"
getProjectPath = base_url + "/api/project"
signReportPath = base_url + "/api/signReport"
addUserToProjectPath = base_url + "/api/addUserToProject"
promoteToAdminPath = base_url + "/api/promoteToAdmin"
getUserProjectsPath = base_url + "/api/getUserProjects"
getWeeklyReportsUserPath = base_url + "/api/getWeeklyReportsUser"
checkIfProjectManagerPath = base_url + "/api/checkIfProjectManager"
ProjectRoleChangePath = base_url + "/api/ProjectRoleChange"
getUsersProjectPath = base_url + "/api/getUsersProject"
getChangeUserNamePath = base_url + "/api/changeUserName"
#ta bort auth i handlern för att få testet att gå igenom
# ta bort auth i handlern för att få testet att gå igenom
def test_ProjectRoleChange():
dprint("Testing ProjectRoleChange")
localUsername = randomString()
localProjectName = randomString()
register(localUsername, "username_password")
token = login(localUsername, "username_password").json()[
"token"
]
token = login(localUsername, "username_password").json()["token"]
# Just checking since this test is built somewhat differently than the others
assert token != None, "Login failed"
@ -80,13 +45,14 @@ def test_ProjectRoleChange():
def test_get_user_projects():
username = "user2"
password = "123"
dprint("Testing get user projects")
loginResponse = login("user2", "123")
loginResponse = login(username, password)
# Check if the user is added to the project
response = requests.get(
getUserProjectsPath,
json={"username": "user2"},
getUserProjectsPath + "/" + username,
headers={"Authorization": "Bearer " + loginResponse.json()["token"]},
)
dprint(response.text)
@ -95,26 +61,6 @@ def test_get_user_projects():
gprint("test_get_user_projects successful")
# Posts the username and password to the register endpoint
def register(username: string, password: string):
dprint("Registering with username: ", username, " and password: ", password)
response = requests.post(
registerPath, json={"username": username, "password": password}
)
dprint(response.text)
return response
# Posts the username and password to the login endpoint
def login(username: string, password: string):
dprint("Logging in with username: ", username, " and password: ", password)
response = requests.post(
loginPath, json={"username": username, "password": password}
)
dprint(response.text)
return response
# Test function to login
def test_login():
response = login(username, "always_same")
@ -130,6 +76,7 @@ def test_create_user():
assert response.status_code == 200, "Registration failed"
gprint("test_create_user successful")
# Test function to add a project
def test_add_project():
loginResponse = login(username, "always_same")
@ -143,6 +90,7 @@ def test_add_project():
assert response.status_code == 200, "Add project failed"
gprint("test_add_project successful")
# Test function to submit a report
def test_submit_report():
token = login(username, "always_same").json()["token"]
@ -164,6 +112,7 @@ def test_submit_report():
assert response.status_code == 200, "Submit report failed"
gprint("test_submit_report successful")
# Test function to get a weekly report
def test_get_weekly_report():
token = login(username, "always_same").json()["token"]
@ -191,91 +140,58 @@ def test_get_project():
# Test function to add a user to a project
def test_add_user_to_project():
# Log in as a site admin
admin_username = randomString()
admin_password = "admin_password"
dprint(
"Registering with username: ", admin_username, " and password: ", admin_password
)
response = requests.post(
registerPath, json={"username": admin_username, "password": admin_password}
)
dprint(response.text)
# User to create
pm_user = "user" + randomString()
pm_passwd = "password"
admin_token = login(admin_username, admin_password).json()["token"]
response = requests.post(
promoteToAdminPath,
json={"username": admin_username},
headers={"Authorization": "Bearer " + admin_token},
)
dprint(response.text)
assert response.status_code == 200, "Promote to site admin failed"
dprint("Admin promoted to site admin successfully")
# User to add
member_user = "member" + randomString()
member_passwd = "password"
# Create a new user to add to the project
new_user = randomString()
register(new_user, "new_user_password")
# Name of the project to be created
project_name = "project" + randomString()
# Add the new user to the project as a member
response = requests.put(
addUserToProjectPath,
json={"projectName": projectName, "username": new_user, "role": "member"},
headers={"Authorization": "Bearer " + admin_token},
)
pm_token = register_and_login(pm_user, pm_passwd)
register(member_user, member_passwd)
dprint(response.text)
response = create_project(pm_token, project_name)
assert response.status_code == 200, "Create project failed"
# Promote the user to project manager
response = addToProject(pm_token, member_user, project_name)
assert response.status_code == 200, "Add user to project failed"
gprint("test_add_user_to_project successful")
# Test function to sign a report
def test_sign_report():
# Create a project manager user
project_manager = randomString()
register(project_manager, "project_manager_password")
# Pm user
pm_username = "pm" + randomString()
pm_password = "admin_password2"
# Register an admin
admin_username = randomString()
admin_password = "admin_password2"
dprint(
"Registering with username: ", admin_username, " and password: ", admin_password
)
response = requests.post(
registerPath, json={"username": admin_username, "password": admin_password}
)
dprint(response.text)
# User to add
member_user = "member" + randomString()
member_passwd = "password"
# Log in as the admin
admin_token = login(admin_username, admin_password).json()["token"]
response = requests.post(
promoteToAdminPath,
json={"username": admin_username},
headers={"Authorization": "Bearer " + admin_token},
)
# Name of the project to be created
project_name = "project" + randomString()
response = requests.put(
addUserToProjectPath,
json={
"projectName": projectName,
"username": project_manager,
"role": "project_manager",
},
headers={"Authorization": "Bearer " + admin_token},
)
assert response.status_code == 200, "Add project manager to project failed"
dprint("Project manager added to project successfully")
# Register and get the tokens for both users
pm_token = register_and_login(pm_username, pm_password)
member_token = register_and_login(member_user, member_passwd)
# Log in as the project manager
project_manager_token = login(project_manager, "project_manager_password").json()[
"token"
]
# Create the project
response = create_project(pm_token, project_name)
assert response.status_code == 200, "Create project failed"
# Add the user to the project
response = addToProject(pm_token, member_user, project_name)
# Submit a report for the project
token = login(username, "always_same").json()["token"]
response = requests.post(
submitReportPath,
json={
"projectName": projectName,
"week": 2,
response = submitReport(
member_token,
{
"projectName": project_name,
"week": 1,
"developmentTime": 10,
"meetingTime": 5,
"adminTime": 5,
@ -283,53 +199,40 @@ def test_sign_report():
"studyTime": 10,
"testingTime": 10,
},
headers={"Authorization": "Bearer " + token},
)
assert response.status_code == 200, "Submit report failed"
dprint("Submit report successful")
# Retrieve the report ID
response = requests.get(
getWeeklyReportPath,
headers={"Authorization": "Bearer " + token},
params={"username": username, "projectName": projectName, "week": 1},
)
dprint(response.text)
report_id = response.json()["reportId"]
report_id = getReport(member_token, member_user, project_name)["reportId"]
# Sign the report as the project manager
response = requests.post(
signReportPath,
json={"reportId": report_id},
headers={"Authorization": "Bearer " + project_manager_token},
)
response = signReport(pm_token, report_id)
assert response.status_code == 200, "Sign report failed"
dprint("Sign report successful")
# Retrieve the report ID again for confirmation
response = requests.get(
getWeeklyReportPath,
headers={"Authorization": "Bearer " + token},
params={"username": username, "projectName": projectName, "week": 1},
)
dprint(response.text)
report_id = getReport(member_token, member_user, project_name)["reportId"]
assert report_id != None, "Get report failed"
gprint("test_sign_report successful")
# Test function to get weekly reports for a user in a project
def test_get_weekly_reports_user():
def test_get_all_weekly_reports():
# Log in as the user
token = login(username, "always_same").json()["token"]
# Get weekly reports for the user in the project
response = requests.get(
getWeeklyReportsUserPath + "/" + projectName,
getAllWeeklyReportsPath + "/" + projectName,
headers={"Authorization": "Bearer " + token},
params={"targetUser": username},
)
dprint(response.text)
assert response.status_code == 200, "Get weekly reports for user failed"
gprint("test_get_weekly_reports_user successful")
# Test function to check if a user is a project manager
def test_check_if_project_manager():
# Log in as the user
@ -345,6 +248,7 @@ def test_check_if_project_manager():
assert response.status_code == 200, "Check if project manager failed"
gprint("test_check_if_project_manager successful")
def test_ensure_manager_of_created_project():
# Create a new user to add to the project
newUser = "karen_" + randomString()
@ -368,6 +272,7 @@ def test_ensure_manager_of_created_project():
assert response.json()["isProjectManager"] == True, "User is not project manager"
gprint("test_ensure_admin_of_created_project successful")
def test_change_user_name():
# Register a new user
new_user = randomString()
@ -405,6 +310,7 @@ def test_change_user_name():
assert response.status_code == 200, "Change user name failed"
gprint("test_change_user_name successful")
def test_list_all_users_project():
# Log in as a user who is a member of the project
admin_username = randomString()
@ -433,7 +339,166 @@ def test_list_all_users_project():
assert response.status_code == 200, "List all users project failed"
gprint("test_list_all_users_project sucessful")
def test_update_weekly_report():
# Log in as the user
token = login(username, "always_same").json()["token"]
# Prepare the JSON data for updating the weekly report
update_data = {
"projectName": projectName,
"userName": username,
"week": 1,
"developmentTime": 8,
"meetingTime": 6,
"adminTime": 4,
"ownWorkTime": 11,
"studyTime": 8,
"testingTime": 18,
}
# Send a request to update the weekly report
response = requests.put(
getUpdateWeeklyReportPath,
json=update_data,
headers={"Authorization": "Bearer " + token},
)
# Check if the update was successful
assert response.status_code == 200, "Update weekly report failed"
gprint("test_update_weekly_report successful")
def test_remove_project():
admin_username = randomString()
admin_password = "admin_password2"
dprint(
"Registering with username: ", admin_username, " and password: ", admin_password
)
response = requests.post(
registerPath, json={"username": admin_username, "password": admin_password}
)
dprint(response.text)
# Log in as the admin
admin_token = login(admin_username, admin_password).json()["token"]
response = requests.post(
promoteToAdminPath,
json={"username": admin_username},
headers={"Authorization": "Bearer " + admin_token},
)
# Create a new project
new_project = randomString()
response = requests.post(
addProjectPath,
json={"name": new_project, "description": "This is a project"},
headers={"Authorization": "Bearer " + admin_token},
)
assert response.status_code == 200, "Add project failed"
# Remove the project
response = requests.delete(
removeProjectPath + "/" + new_project,
headers={"Authorization": "Bearer " + admin_token},
)
assert response.status_code == 200, "Remove project failed"
gprint("test_remove_project successful")
def test_get_unsigned_reports():
# Log in as the user
token = login("user2", "123").json()["token"]
# Make a request to get all unsigned reports
response = requests.get(
getUnsignedReportsPath + "/" + projectName,
headers={"Authorization": "Bearer " + token},
)
assert response.status_code == 200, "Get unsigned reports failed"
gprint("test_get_unsigned_reports successful")
def test_get_other_users_report_as_pm():
# Create user
user = randomString()
register(user, "password")
# Create project
project = randomString()
pm_token = login(user, "password").json()["token"]
response = requests.post(
addProjectPath,
json={"name": project, "description": "This is a project"},
headers={"Authorization": "Bearer " + pm_token},
)
assert response.status_code == 200, "Add project failed"
# Create other user
other_user = randomString()
register(other_user, "password")
user_token = login(other_user, "password").json()["token"]
# Add other user to project
response = requests.put(
addUserToProjectPath + "/" + project,
headers={"Authorization": "Bearer " + pm_token}, # note pm_token
params={"userName": other_user},
)
assert response.status_code == 200, "Add user to project failed"
# Submit report as other user
response = requests.post(
submitReportPath,
json={
"projectName": project,
"week": 1,
"developmentTime": 10,
"meetingTime": 5,
"adminTime": 5,
"ownWorkTime": 10,
"studyTime": 10,
"testingTime": 10,
},
headers={"Authorization": "Bearer " + user_token},
)
assert response.status_code == 200, "Submit report failed"
# Get report as project manager
response = requests.get(
getWeeklyReportPath,
headers={"Authorization": "Bearer " + pm_token},
params={"targetUser": other_user, "projectName": project, "week": 1},
)
assert response.status_code == 200, "Get weekly report failed"
def test_promote_to_manager():
# User to create
pm_user = "user" + randomString()
pm_passwd = "password"
# User to promote
member_user = "member" + randomString()
member_passwd = "password"
# Name of the project to be created
project_name = "project" + randomString()
pm_token = register_and_login(pm_user, pm_passwd)
member_token = register_and_login(member_user, member_passwd)
response = create_project(pm_token, project_name)
assert response.status_code == 200, "Create project failed"
# Promote the user to project manager
response = promoteToManager(pm_token, member_user, project_name)
assert response.status_code == 200, "Promote to manager failed"
if __name__ == "__main__":
test_promote_to_manager()
test_remove_project()
test_get_user_projects()
test_create_user()
test_login()
@ -443,9 +508,12 @@ if __name__ == "__main__":
test_get_project()
test_sign_report()
test_add_user_to_project()
test_get_weekly_reports_user()
test_get_all_weekly_reports()
test_check_if_project_manager()
test_ProjectRoleChange()
test_ensure_manager_of_created_project()
test_get_unsigned_reports()
test_list_all_users_project()
test_change_user_name()
test_update_weekly_report()
test_get_other_users_report_as_pm()