mirror of
				https://github.com/abhinavxd/libredesk.git
				synced 2025-10-30 19:43:35 +00:00 
			
		
		
		
	Compare commits
	
		
			504 Commits
		
	
	
		
			v0.4.1-alp
			...
			v0.8.0-bet
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | f3acc37405 | ||
|  | 562babf222 | ||
|  | 93e94432f5 | ||
|  | ec63604163 | ||
|  | f06da2a861 | ||
|  | 98f16854c8 | ||
|  | cc36ef5a3a | ||
|  | 969d6ea4f9 | ||
|  | 326ccdf9d4 | ||
|  | d6a8e76472 | ||
|  | f95b374b74 | ||
|  | a1db6ccb31 | ||
|  | 267a6027ee | ||
|  | 3471263710 | ||
|  | 7469e296d2 | ||
|  | 44ffc77c4e | ||
|  | 3ec061d8f1 | ||
|  | 48b8d14f8f | ||
|  | 6231a9e131 | ||
|  | d63302843b | ||
|  | a652f380b2 | ||
|  | a4a9a9ccd3 | ||
|  | 71865e389e | ||
|  | ae470be4c8 | ||
|  | 636742c34b | ||
|  | de77c03f66 | ||
|  | b7092744fd | ||
|  | 6f300bb073 | ||
|  | a8ca12fb9a | ||
|  | e4bec993e6 | ||
|  | efc01be7d3 | ||
|  | ec72c5af90 | ||
|  | 490417cf9d | ||
|  | 4f54db3d1b | ||
|  | 210b8bb53b | ||
|  | a0e1ccf117 | ||
|  | faf2082561 | ||
|  | 50baa8491b | ||
|  | 8e89e4e0d4 | ||
|  | b15413b7ca | ||
|  | 701e5b2580 | ||
|  | dbd4e97f7e | ||
|  | 007c332a7d | ||
|  | 4fcad4fd81 | ||
|  | bece58bdec | ||
|  | 6d2d8f78d4 | ||
|  | 98492a1869 | ||
|  | 18b50b11c8 | ||
|  | 5a1628f710 | ||
|  | 12ebe32ba3 | ||
|  | fce2587a9d | ||
|  | 7d92ac9cce | ||
|  | 3ce3c5e0ee | ||
|  | 35ad00ec51 | ||
|  | 9ec96be959 | ||
|  | 6ca36d611f | ||
|  | 5a87d24d72 | ||
|  | 7d4e7e68c3 | ||
|  | 5b941fd993 | ||
|  | 63e348e512 | ||
|  | 10a845dc81 | ||
|  | 0228989202 | ||
|  | 3f7d151d33 | ||
|  | a516773b14 | ||
|  | f6d3bd543f | ||
|  | 074d147bb6 | ||
|  | c1c14f7f54 | ||
|  | 634fc66e9f | ||
|  | 78b8607d8f | ||
|  | 0dec822c1c | ||
|  | 958f5e38c0 | ||
|  | 550a3fa801 | ||
|  | 6bbfbe8cf6 | ||
|  | f9ed326d72 | ||
|  | e0dc0285a4 | ||
|  | b971619ea6 | ||
|  | 69accaebef | ||
|  | 27de73536e | ||
|  | df108a3363 | ||
|  | 266c3dab72 | ||
|  | bf2c1fff6f | ||
|  | 2930af0c4f | ||
|  | 389c4e3dd3 | ||
|  | 9a119e6dc3 | ||
|  | ee178d383d | ||
|  | fc4db676d9 | ||
|  | 70cb3d0f80 | ||
|  | c9920c3377 | ||
|  | 6d62c3a4ba | ||
|  | d9b5fb8f0f | ||
|  | 3de320f1fb | ||
|  | be977dcff2 | ||
|  | 5e19f13e18 | ||
|  | ccc5940dd9 | ||
|  | 4203b82e90 | ||
|  | ba07e224c2 | ||
|  | 3fff65150f | ||
|  | c4fcf6bd91 | ||
|  | 5ea1b9e84c | ||
|  | 5b522888bc | ||
|  | dc2250ce50 | ||
|  | 839a06f0d2 | ||
|  | d2e5d85e3a | ||
|  | 0737d22374 | ||
|  | d6af9d10ea | ||
|  | 6381fc23c2 | ||
|  | 6bb5728665 | ||
|  | 2322ec33b0 | ||
|  | 9132e11458 | ||
|  | e70f92d377 | ||
|  | 591108f094 | ||
|  | 1b2a5e4f36 | ||
|  | f613cc237b | ||
|  | c37258fccb | ||
|  | 1879d9d22b | ||
|  | b369e2f56a | ||
|  | ef56f1a74e | ||
|  | d274adb19b | ||
|  | d31fcb00b6 | ||
|  | 88d719ec4f | ||
|  | 147180a536 | ||
|  | faa195f0a6 | ||
|  | 4b0422d904 | ||
|  | 9303997cea | ||
|  | aba07b3096 | ||
|  | 27aac88f53 | ||
|  | cb6b0e420b | ||
|  | e004afd7d1 | ||
|  | 6a77d346dc | ||
|  | 60c89cb617 | ||
|  | b7d4b187e8 | ||
|  | 2bf45f32de | ||
|  | 981372ab86 | ||
|  | 803196985d | ||
|  | ebf6a980e8 | ||
|  | 813ef91964 | ||
|  | 3b9fb7a08d | ||
|  | 7fb86f140c | ||
|  | aa8d326fa1 | ||
|  | ca9a0a5892 | ||
|  | 73e2950174 | ||
|  | e7b8e5c4bb | ||
|  | 582c906440 | ||
|  | f3881ee0aa | ||
|  | b557c2ca4b | ||
|  | 30884d3536 | ||
|  | bce0d1d12f | ||
|  | 67a4f6a162 | ||
|  | ec28ac8f3a | ||
|  | bc71fcfdc1 | ||
|  | bc0bee8f6a | ||
|  | 499fc0dad1 | ||
|  | 03b932c1c0 | ||
|  | 012de059e7 | ||
|  | 6357faf6c8 | ||
|  | f7a12cffd3 | ||
|  | 6487bf9a0a | ||
|  | 53d5715429 | ||
|  | b561e79440 | ||
|  | e567acbe59 | ||
|  | 57d0e90b5f | ||
|  | 5a0e3a8072 | ||
|  | d95a5f40cf | ||
|  | 6981a0790d | ||
|  | 55bc9bfc91 | ||
|  | 67db2e5ff2 | ||
|  | 64304c2384 | ||
|  | c5fe6aaadd | ||
|  | fea7eef658 | ||
|  | 475e400810 | ||
|  | 641ae0540e | ||
|  | dc6fede081 | ||
|  | 28dcd6cb2f | ||
|  | ade833fb7b | ||
|  | 5bcb0a2ad9 | ||
|  | ad2f685fec | ||
|  | 26c7df538c | ||
|  | 625a08d0aa | ||
|  | bf1510b9c3 | ||
|  | bae896d38d | ||
|  | 37b7c05b30 | ||
|  | eb05368f18 | ||
|  | 7ef510894b | ||
|  | 69268a3a84 | ||
|  | fcd3462d25 | ||
|  | fbf502451a | ||
|  | dc909ceb4f | ||
|  | cc1432b3e4 | ||
|  | d532a99771 | ||
|  | 50baa3f38e | ||
|  | 63a8f04408 | ||
|  | ea0b7d6d52 | ||
|  | 5d6897a960 | ||
|  | c4a95672fe | ||
|  | 2efd07b405 | ||
|  | 0b9cf38826 | ||
|  | b44c314299 | ||
|  | 2e1188e443 | ||
|  | afeec39b59 | ||
|  | fb2a08ec1a | ||
|  | 7f2df0082c | ||
|  | 6c523ac447 | ||
|  | 02fc57c35a | ||
|  | cd0a357695 | ||
|  | 2dc751e602 | ||
|  | 8bc0cce993 | ||
|  | f6e2fc1956 | ||
|  | 5fe5ac5882 | ||
|  | 975577555d | ||
|  | f43acb77a1 | ||
|  | 331c84fa56 | ||
|  | 9314efb9d9 | ||
|  | 5c8481af97 | ||
|  | d9bc4d1c0d | ||
|  | 087c8ad491 | ||
|  | 65cac843cb | ||
|  | 23b0481f24 | ||
|  | 9a651702ce | ||
|  | a0203f882e | ||
|  | 75425ca0dd | ||
|  | c2849fa63d | ||
|  | b20c7845ac | ||
|  | 38a5b25b1f | ||
|  | 9dce155ebc | ||
|  | 314341b40d | ||
|  | 1f6e3322aa | ||
|  | 102ba99b3c | ||
|  | 8285575f1c | ||
|  | 01d3b590a9 | ||
|  | 210e0de1ae | ||
|  | 1f8fdf2ef6 | ||
|  | 696e4780ac | ||
|  | 3998798e54 | ||
|  | 70b5da29e1 | ||
|  | 88ef5d26db | ||
|  | 54bad59392 | ||
|  | 506bb91e20 | ||
|  | d1478e1971 | ||
|  | 5583b472f7 | ||
|  | b715483260 | ||
|  | 8ce0464603 | ||
|  | a84ed1ed32 | ||
|  | 7426a09478 | ||
|  | 8ad2f078ac | ||
|  | 9226063db3 | ||
|  | a9fd4fe2b6 | ||
|  | 7e8c9962c3 | ||
|  | cf20142e40 | ||
|  | 8654a04dcf | ||
|  | 4c766d8ccb | ||
|  | cb1ec7eb8e | ||
|  | a89c3dbe04 | ||
|  | e2319714ca | ||
|  | 172f78262e | ||
|  | f53d5f188f | ||
|  | 55ec962003 | ||
|  | d3b1955cb2 | ||
|  | fac496fef2 | ||
|  | c36a425a1e | ||
|  | f43ab5041e | ||
|  | cd0ff1b67d | ||
|  | 5bc065469d | ||
|  | 77be86b1f4 | ||
|  | dde84c65b0 | ||
|  | f2d4969733 | ||
|  | aeececd001 | ||
|  | fdeeda8bca | ||
|  | 45bae57183 | ||
|  | a345b2e322 | ||
|  | 490aaedb48 | ||
|  | 87361e5cda | ||
|  | c039d5a20f | ||
|  | 53f15a3a7e | ||
|  | a397d3d3ea | ||
|  | 4ca123e6a1 | ||
|  | 7dd5abdda6 | ||
|  | c16144a2bf | ||
|  | 7f1c2c2f11 | ||
|  | d8a681d17e | ||
|  | f657a873bc | ||
|  | 88e07c324d | ||
|  | 6c9eca3d81 | ||
|  | 07b185050e | ||
|  | 66886c34e5 | ||
|  | 0af7265178 | ||
|  | f722de2fe4 | ||
|  | 6b2be57049 | ||
|  | e1b2ec8a4b | ||
|  | 8d47a7456d | ||
|  | 62023695a5 | ||
|  | a212ed4afb | ||
|  | 8e6bea09fe | ||
|  | 71e2e3cd8a | ||
|  | 59f5084bec | ||
|  | 87e1477811 | ||
|  | 10d3da608c | ||
|  | 0de7c91641 | ||
|  | 61ec075bd6 | ||
|  | 0b2c607cd3 | ||
|  | 0556318714 | ||
|  | 7b35cf0abf | ||
|  | 8619aa8e17 | ||
|  | 25db57805e | ||
|  | 3b2d0d049f | ||
|  | 1c6d03a4c2 | ||
|  | 062e0c39da | ||
|  | 67090fb052 | ||
|  | c434de130b | ||
|  | 4e4f07f2e8 | ||
|  | 19a507c88f | ||
|  | ac61d43688 | ||
|  | 7f8e3ccbbc | ||
|  | facce8bdad | ||
|  | 8acad27b75 | ||
|  | 24fbe14804 | ||
|  | 061677f2b0 | ||
|  | 450b609d47 | ||
|  | 971a433f3d | ||
|  | 220321bb8c | ||
|  | d5ba70667d | ||
|  | a9f9d368b9 | ||
|  | 2fc642c34e | ||
|  | 488f14e87c | ||
|  | 3702a61d74 | ||
|  | b01f6f812d | ||
|  | a0c77bc12e | ||
|  | 8bc511509c | ||
|  | 0254bab266 | ||
|  | 91372f5339 | ||
|  | d69a8c58d1 | ||
|  | 4e893ef876 | ||
|  | 5770188e4d | ||
|  | 8bd7895ccf | ||
|  | e10bb45582 | ||
|  | a397bc059b | ||
|  | 4a305ff889 | ||
|  | 616410c0a9 | ||
|  | 408e1fc142 | ||
|  | bc586fe775 | ||
|  | a49038f965 | ||
|  | 4cfe0ccbd9 | ||
|  | acbb94447c | ||
|  | cd429b9751 | ||
|  | 78d073c499 | ||
|  | 8083ad93b4 | ||
|  | ad99dee544 | ||
|  | a5eeb03f0d | ||
|  | c81f6496ea | ||
|  | 143a12e3c3 | ||
|  | e2d6a214c4 | ||
|  | 4a3afc83a5 | ||
|  | bb512d5ecd | ||
|  | 7957dbbd4a | ||
|  | 199778e771 | ||
|  | b2a53b18d5 | ||
|  | 576c678403 | ||
|  | 9bfe014d1e | ||
|  | 1b536bdc69 | ||
|  | c02339f311 | ||
|  | 1e7ab144b6 | ||
|  | e998529827 | ||
|  | 0a57a2724e | ||
|  | d2248d34c5 | ||
|  | 33f2f67ba8 | ||
|  | 7075ca214c | ||
|  | e68325d609 | ||
|  | 2499df866f | ||
|  | be5779e201 | ||
|  | 2d868b7df1 | ||
|  | 374aabcb10 | ||
|  | e69b1c3e6d | ||
|  | 1821647695 | ||
|  | b4f2186150 | ||
|  | 6d588f7a4e | ||
|  | 2a382d6036 | ||
|  | c639bfba40 | ||
|  | 82aac02a97 | ||
|  | c348a5c9b7 | ||
|  | 008f71d7b4 | ||
|  | 9b41aa0e9a | ||
|  | c60a0788d9 | ||
|  | 013b5bf37e | ||
|  | df0dfb480f | ||
|  | 2daefccd79 | ||
|  | f69e8dd4f8 | ||
|  | d171958223 | ||
|  | 3b7550fcf3 | ||
|  | 0de712762c | ||
|  | 6b6549cb03 | ||
|  | cd4b9a9c23 | ||
|  | e19f817c5f | ||
|  | 5ce8ed72ba | ||
|  | 4ec564ee2e | ||
|  | 19f08ec76a | ||
|  | dd8053b2bb | ||
|  | 72b92d6c66 | ||
|  | 497b54fc49 | ||
|  | 9d18d3d08d | ||
|  | 6bea14e7a9 | ||
|  | 25f23735d5 | ||
|  | 3888793450 | ||
|  | 88e4a55952 | ||
|  | 9aa9a5e1b2 | ||
|  | a3098a1dbd | ||
|  | 76a24467e7 | ||
|  | 4361250c73 | ||
|  | 7d9650be2e | ||
|  | eb707fd8de | ||
|  | 36077b1837 | ||
|  | d5499229b5 | ||
|  | 5e90dfee5a | ||
|  | 1875a62e00 | ||
|  | f60c4e8cb6 | ||
|  | 495ff02067 | ||
|  | 5afec04c07 | ||
|  | 56f00e791e | ||
|  | dcede8a461 | ||
|  | 39fd5c9165 | ||
|  | 4b8a954043 | ||
|  | 6ac9f28a32 | ||
|  | 8101c202fa | ||
|  | 09746fb365 | ||
|  | f59ea59a2e | ||
|  | a2cdd728c0 | ||
|  | ac59a5defc | ||
|  | 05fbe39315 | ||
|  | c7c65a3d83 | ||
|  | 5bf6b7df47 | ||
|  | c034c21fa5 | ||
|  | 4ed241a03d | ||
|  | 6b00f70c37 | ||
|  | c51073d289 | ||
|  | d03d4477de | ||
|  | 3b211dc372 | ||
|  | 6b4f243b74 | ||
|  | 9ff5a53ebb | ||
|  | 9b9282dfd9 | ||
|  | 698e2d960e | ||
|  | a8db8f64b5 | ||
|  | a5a9d1304c | ||
|  | f688be1c88 | ||
|  | d3eb3499df | ||
|  | 721f7c811c | ||
|  | a33e1453a8 | ||
|  | b6ce6975c9 | ||
|  | 860b216e2b | ||
|  | eaa2b1ddcf | ||
|  | 0f12b2a3f3 | ||
|  | def0bb8e4c | ||
|  | a41c360cdb | ||
|  | 159cca6866 | ||
|  | 83f553227a | ||
|  | 28a6a3d246 | ||
|  | 7e16cc1a74 | ||
|  | aeef7d4ad7 | ||
|  | f0358f67f0 | ||
|  | 12f2453f5a | ||
|  | 2742be5619 | ||
|  | d837defbc9 | ||
|  | 5cc849e7eb | ||
|  | 729faf980c | ||
|  | a36c81141b | ||
|  | 756147a2c9 | ||
|  | 88a641fe09 | ||
|  | 785da6715c | ||
|  | 32401fa231 | ||
|  | 83b891c92a | ||
|  | f277f76a0a | ||
|  | 5f1a40acba | ||
|  | d90b9c2be7 | ||
|  | 43184ec2f3 | ||
|  | 2fdcf68a22 | ||
|  | 4bef3e80a2 | ||
|  | 09703c1090 | ||
|  | 45541c221a | ||
|  | fc0e0a8fff | ||
|  | d1f931106d | ||
|  | 227aa26c35 | ||
|  | 79a3f0ff70 | ||
|  | eefacdbda2 | ||
|  | 3783cce1be | ||
|  | a4cb373f32 | ||
|  | 99e8949be6 | ||
|  | 1240051825 | ||
|  | 5398d4ec41 | ||
|  | fd4e47dc68 | ||
|  | 1ff7317c4d | ||
|  | d6449b9336 | ||
|  | 580fb76a39 | ||
|  | 91889423a2 | ||
|  | f12efe5511 | ||
|  | 56187ddc46 | ||
|  | 47af51d0dd | ||
|  | 47a3985a51 | ||
|  | 3f11af13b8 | ||
|  | da629c864c | ||
|  | 6fb35b90b3 | ||
|  | 9892f9dae7 | ||
|  | 277586f025 | ||
|  | f3070e13a7 | ||
|  | 8ed29df11c | ||
|  | 36d91de8f7 | ||
|  | 57c1948379 | ||
|  | 772152c40c | 
							
								
								
									
										16
									
								
								.github/ISSUE_TEMPLATE/confirmed-bug.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								.github/ISSUE_TEMPLATE/confirmed-bug.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | |||||||
|  | --- | ||||||
|  | name: Confirmed Bug Report | ||||||
|  | about: Report a confirmed bug in Libredesk | ||||||
|  | title: "[Bug] <brief summary>" | ||||||
|  | labels: bug | ||||||
|  | assignees: "" | ||||||
|  | --- | ||||||
|  |  | ||||||
|  | **Version:** | ||||||
|  | - libredesk: [eg: v0.7.0] | ||||||
|  |  | ||||||
|  | **Description of the bug and steps to reproduce:** | ||||||
|  | A clear and concise description of what the bug is. | ||||||
|  |  | ||||||
|  | **Logs / Screenshots:** | ||||||
|  | Attach any relevant logs or screenshots to help diagnose the issue. | ||||||
							
								
								
									
										16
									
								
								.github/ISSUE_TEMPLATE/possible-bug.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								.github/ISSUE_TEMPLATE/possible-bug.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | |||||||
|  | --- | ||||||
|  | name: Possible Bug Report | ||||||
|  | about: Something in Libredesk might be broken but needs confirmation | ||||||
|  | title: "[Possible Bug] <brief summary>" | ||||||
|  | labels: bug, needs-investigation | ||||||
|  | assignees: "" | ||||||
|  | --- | ||||||
|  |  | ||||||
|  | **Version:** | ||||||
|  |  - libredesk: [eg: v0.7.0] | ||||||
|  |   | ||||||
|  | **Description of the bug and steps to reproduce:** | ||||||
|  | A clear and concise description of what the bug is. | ||||||
|  |  | ||||||
|  | **Logs / Screenshots:** | ||||||
|  | Attach any relevant logs or screenshots to help diagnose the issue. | ||||||
							
								
								
									
										47
									
								
								.github/workflows/crowdin.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								.github/workflows/crowdin.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | |||||||
|  | name: Crowdin | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   push: | ||||||
|  |     paths: | ||||||
|  |       # Only trigger a Crowdin update when the source localization file is | ||||||
|  |       # updated. | ||||||
|  |       - 'i18n/en.json' | ||||||
|  |     # Only watches for changes happening on "main" branch. | ||||||
|  |     branches: [ main ] | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   crowdin: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     # Only run on the original repository, not forks | ||||||
|  |     if: github.event.repository.fork == false | ||||||
|  |     steps: | ||||||
|  |       - name: Checkout | ||||||
|  |         uses: actions/checkout@v4 | ||||||
|  |  | ||||||
|  |       - name: Crowdin push | ||||||
|  |         uses: crowdin/github-action@v2 | ||||||
|  |         with: | ||||||
|  |           # Send source (english) strings to Crowdin. | ||||||
|  |           upload_sources: true | ||||||
|  |           # See: https://crowdin.github.io/crowdin-cli/commands | ||||||
|  |           # /crowdin-upload#options | ||||||
|  |           upload_sources_args: '--preserve-hierarchy --delete-obsolete' | ||||||
|  |           # Don't upload or download translations. | ||||||
|  |           upload_translations: false | ||||||
|  |           download_translations: false | ||||||
|  |           # Source language file. | ||||||
|  |           source: 'i18n/en.json' | ||||||
|  |           # Translations files. | ||||||
|  |           translation: 'i18n/%two_letters_code%.json' | ||||||
|  |         env: | ||||||
|  |           # Crowdin.com > Project > Tools > API > Project ID. | ||||||
|  |           CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }} | ||||||
|  |           # When creating a personal token in Crowdin, you'll be asked to select | ||||||
|  |           # the necessary scopes. The basic Crowdin Personal Token scopes are | ||||||
|  |           # the following: | ||||||
|  |           #  - Projects (List, Get, Create, Edit) -> Read | ||||||
|  |           #  - Translation Status -> Read Only | ||||||
|  |           #  - Source files & strings -> Read and Write | ||||||
|  |           #  - Translations -> Read and Write | ||||||
|  |           CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }} | ||||||
|  |  | ||||||
							
								
								
									
										71
									
								
								.github/workflows/frontend-ci.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								.github/workflows/frontend-ci.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,71 @@ | |||||||
|  | name: CI | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   push: | ||||||
|  |     branches: [main] | ||||||
|  |   pull_request: | ||||||
|  |     branches: [main] | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   build-and-test: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     services: | ||||||
|  |       db: | ||||||
|  |         image: postgres:17-alpine | ||||||
|  |         ports: | ||||||
|  |           - 5432:5432 | ||||||
|  |         env: | ||||||
|  |           POSTGRES_USER: libredesk | ||||||
|  |           POSTGRES_PASSWORD: libredesk | ||||||
|  |           POSTGRES_DB: libredesk | ||||||
|  |         options: >- | ||||||
|  |           --health-cmd="pg_isready -U libredesk" | ||||||
|  |           --health-interval=10s | ||||||
|  |           --health-timeout=5s | ||||||
|  |           --health-retries=5 | ||||||
|  |       redis: | ||||||
|  |         image: redis | ||||||
|  |         ports: | ||||||
|  |           - 6379:6379 | ||||||
|  |  | ||||||
|  |     steps: | ||||||
|  |       - uses: actions/checkout@v3 | ||||||
|  |  | ||||||
|  |       - name: Set up Go | ||||||
|  |         uses: actions/setup-go@v3 | ||||||
|  |         with: | ||||||
|  |           go-version: "1.24.3" | ||||||
|  |  | ||||||
|  |       - name: Set up Node.js | ||||||
|  |         uses: actions/setup-node@v3 | ||||||
|  |         with: | ||||||
|  |           node-version: "20" | ||||||
|  |  | ||||||
|  |       - name: Install pnpm | ||||||
|  |         run: npm install -g pnpm | ||||||
|  |  | ||||||
|  |       - name: Install cypress deps | ||||||
|  |         run: sudo apt-get update && sudo apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libnss3 libxss1 libasound2t64 libxtst6 xauth xvfb | ||||||
|  |  | ||||||
|  |       - name: Build binary and frontend | ||||||
|  |         run: make build | ||||||
|  |  | ||||||
|  |       - name: Configure app | ||||||
|  |         run: | | ||||||
|  |           cp config.sample.toml config.toml | ||||||
|  |           sed -i 's/host = "db"/host = "127.0.0.1"/' config.toml | ||||||
|  |           sed -i 's/address = "redis:6379"/address = "localhost:6379"/' config.toml | ||||||
|  |  | ||||||
|  |       - name: Run unit tests for frontend | ||||||
|  |         run: cd frontend && pnpm test:run | ||||||
|  |  | ||||||
|  |       - name: Install db schema and run tests | ||||||
|  |         env: | ||||||
|  |           LIBREDESK_SYSTEM_USER_PASSWORD: "StrongPass!123" | ||||||
|  |         run: | | ||||||
|  |           ./libredesk --install --idempotent-install --yes --config ./config.toml | ||||||
|  |           ./libredesk --upgrade --yes --config ./config.toml | ||||||
|  |           ./libredesk --config ./config.toml & | ||||||
|  |           sleep 10 | ||||||
|  |           cd frontend | ||||||
|  |           pnpm run test:e2e:ci | ||||||
							
								
								
									
										31
									
								
								.github/workflows/github-pages.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										31
									
								
								.github/workflows/github-pages.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,31 +0,0 @@ | |||||||
| name: Deploy MkDocs |  | ||||||
|  |  | ||||||
| on: |  | ||||||
|   push: |  | ||||||
|     branches: |  | ||||||
|       - main |  | ||||||
|  |  | ||||||
| jobs: |  | ||||||
|   deploy: |  | ||||||
|     runs-on: ubuntu-latest |  | ||||||
|     steps: |  | ||||||
|       - uses: actions/checkout@v3 |  | ||||||
|  |  | ||||||
|       - uses: actions/setup-python@v4 |  | ||||||
|         with: |  | ||||||
|           python-version: 3.x |  | ||||||
|  |  | ||||||
|       - run: pip install mkdocs-material |  | ||||||
|  |  | ||||||
|       - run: | |  | ||||||
|           if [ -f requirements.txt ]; then |  | ||||||
|             pip install -r requirements.txt; |  | ||||||
|           fi |  | ||||||
|  |  | ||||||
|       - run: cd docs && mkdocs build |  | ||||||
|  |  | ||||||
|       - name: Deploy to GitHub Pages |  | ||||||
|         uses: peaceiris/actions-gh-pages@v3 |  | ||||||
|         with: |  | ||||||
|           github_token: ${{ secrets.GITHUB_TOKEN }} |  | ||||||
|           publish_dir: ./docs/site |  | ||||||
							
								
								
									
										22
									
								
								.github/workflows/go.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								.github/workflows/go.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | name: Go | ||||||
|  |  | ||||||
|  | on: [push] | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   test: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |  | ||||||
|  |     steps: | ||||||
|  |       - name: Checkout code | ||||||
|  |         uses: actions/checkout@v4 | ||||||
|  |  | ||||||
|  |       - name: Set up Go | ||||||
|  |         uses: actions/setup-go@v5 | ||||||
|  |         with: | ||||||
|  |           go-version: "1.24.3" | ||||||
|  |  | ||||||
|  |       - name: Install dependencies | ||||||
|  |         run: go get -v ./... | ||||||
|  |  | ||||||
|  |       - name: Run tests | ||||||
|  |         run: make test | ||||||
							
								
								
									
										2
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							| @@ -40,7 +40,7 @@ jobs: | |||||||
|       - name: Set up Go |       - name: Set up Go | ||||||
|         uses: actions/setup-go@v5 |         uses: actions/setup-go@v5 | ||||||
|         with: |         with: | ||||||
|           go-version: "1.21" |           go-version: "1.24.3" | ||||||
|           cache: true |           cache: true | ||||||
|  |  | ||||||
|       - name: Set up Node.js |       - name: Set up Node.js | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
| FROM alpine:latest | FROM alpine:latest | ||||||
|  |  | ||||||
| # Install necessary packages | # Install necessary packages | ||||||
| RUN apk --no-cache add ca-certificates | RUN apk --no-cache add ca-certificates tzdata | ||||||
|  |  | ||||||
| # Set the working directory to /libredesk | # Set the working directory to /libredesk | ||||||
| WORKDIR /libredesk | WORKDIR /libredesk | ||||||
|   | |||||||
							
								
								
									
										14
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								Makefile
									
									
									
									
									
								
							| @@ -38,7 +38,7 @@ frontend-build: install-deps | |||||||
| .PHONY: run-backend | .PHONY: run-backend | ||||||
| run-backend: | run-backend: | ||||||
| 	@echo "→ Running backend..." | 	@echo "→ Running backend..." | ||||||
| 	CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -X 'main.frontendDir=frontend/dist'" cmd/*.go | 	CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -X 'github.com/abhinavxd/libredesk/internal/version.Version=${VERSION}' -X 'main.frontendDir=frontend/dist'" cmd/*.go | ||||||
|  |  | ||||||
| # Run the JS frontend server in development mode. | # Run the JS frontend server in development mode. | ||||||
| .PHONY: run-frontend | .PHONY: run-frontend | ||||||
| @@ -52,8 +52,8 @@ run-frontend: | |||||||
| .PHONY: build-backend | .PHONY: build-backend | ||||||
| build-backend: $(STUFFBIN) | build-backend: $(STUFFBIN) | ||||||
| 	@echo "→ Building backend..." | 	@echo "→ Building backend..." | ||||||
| 	@CGO_ENABLED=0 go build -a\ | 	@CGO_ENABLED=0 go build -a \ | ||||||
| 		-ldflags="-X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -s -w" \ | 		-ldflags="-X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -X 'github.com/abhinavxd/libredesk/internal/version.Version=${VERSION}' -s -w" \ | ||||||
| 		-o ${BIN} cmd/*.go | 		-o ${BIN} cmd/*.go | ||||||
|  |  | ||||||
| # Main build target: builds both frontend and backend, then stuffs static assets into the binary. | # Main build target: builds both frontend and backend, then stuffs static assets into the binary. | ||||||
| @@ -71,4 +71,10 @@ stuff: $(STUFFBIN) | |||||||
| .PHONY: demo-build | .PHONY: demo-build | ||||||
| demo-build: | demo-build: | ||||||
| 	@echo "→ Building in demo mode..." | 	@echo "→ Building in demo mode..." | ||||||
| 	@export VITE_DEMO_BUILD="true" && $(MAKE) build | 	@export VITE_DEMO_BUILD="true" && $(MAKE) build | ||||||
|  |  | ||||||
|  | # Run tests. | ||||||
|  | .PHONY: test | ||||||
|  | test: | ||||||
|  | 	@echo "→ Running tests..." | ||||||
|  | 	go test -count=1 ./... | ||||||
|   | |||||||
							
								
								
									
										43
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								README.md
									
									
									
									
									
								
							| @@ -3,19 +3,17 @@ | |||||||
|  |  | ||||||
| # Libredesk | # Libredesk | ||||||
|  |  | ||||||
| Open source, self-hosted customer support desk. Single binary app. | Modern, open source, self-hosted customer support desk. Single binary app.  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| Visit [libredesk.io](https://libredesk.io) for more info. Check out the [**Live demo**](https://demo.libredesk.io/). | Visit [libredesk.io](https://libredesk.io) for more info. Check out the [**Live demo**](https://demo.libredesk.io/). | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| > **CAUTION:** This project is currently in **alpha**. Features and APIs may change and are not yet fully tested. |  | ||||||
|  |  | ||||||
| ## Features | ## Features | ||||||
|  |  | ||||||
| - **Multi Inbox**   | - **Multi Shared Inbox**   | ||||||
|   Libredesk supports multiple inboxes, letting you manage conversations across teams effortlessly. |   Libredesk supports multiple shared inboxes, letting you manage conversations across teams effortlessly. | ||||||
| - **Granular Permissions**   | - **Granular Permissions**   | ||||||
|   Create custom roles with granular permissions for teams and individual agents. |   Create custom roles with granular permissions for teams and individual agents. | ||||||
| - **Smart Automation**   | - **Smart Automation**   | ||||||
| @@ -30,12 +28,16 @@ Visit [libredesk.io](https://libredesk.io) for more info. Check out the [**Live | |||||||
|   Distribute workload with auto assignment rules. Auto-assign conversations based on agent capacity or custom criteria. |   Distribute workload with auto assignment rules. Auto-assign conversations based on agent capacity or custom criteria. | ||||||
| - **SLA Management**   | - **SLA Management**   | ||||||
|   Set and track response time targets. Get notified when conversations are at risk of breaching SLA commitments. |   Set and track response time targets. Get notified when conversations are at risk of breaching SLA commitments. | ||||||
| - **Business Intelligence**   | - **Custom attributes**   | ||||||
|   Connect your favorite BI tools like Metabase and create custom dashboards and reports with your support data—without lock-ins. |   Create custom attributes for contacts or conversations such as the subscription plan or the date of their first purchase.  | ||||||
| - **AI-Assisted Response Rewrite**   | - **AI-Assist**   | ||||||
|   Instantly rewrite responses with AI to make them more friendly, professional, or polished. |   Instantly rewrite responses with AI to make them more friendly, professional, or polished. | ||||||
|  | - **Activity logs**   | ||||||
|  |   Track all actions performed by agents and admins—updates and key events across the system—for auditing and accountability. | ||||||
|  | - **Webhooks**   | ||||||
|  |   Integrate with external systems using real-time HTTP notifications for conversation and message events. | ||||||
| - **Command Bar**   | - **Command Bar**   | ||||||
|   Opens with a simple shortcut (CTRL+k) and lets you quickly perform actions on conversations. |   Opens with a simple shortcut (CTRL+K) and lets you quickly perform actions on conversations. | ||||||
|  |  | ||||||
| And more checkout - [libredesk.io](https://libredesk.io) | And more checkout - [libredesk.io](https://libredesk.io) | ||||||
|  |  | ||||||
| @@ -61,9 +63,9 @@ docker compose up -d | |||||||
| docker exec -it libredesk_app ./libredesk --set-system-user-password | docker exec -it libredesk_app ./libredesk --set-system-user-password | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| Go to `http://localhost:9000` and login with username `System` and the password you set using the --set-system-user-password command. | Go to `http://localhost:9000` and login with username `System` and the password you set using the `--set-system-user-password` command. | ||||||
|  |  | ||||||
| See [installation docs](https://libredesk.io/docs/installation/) | See [installation docs](https://docs.libredesk.io/getting-started/installation) | ||||||
|  |  | ||||||
| __________________ | __________________ | ||||||
|  |  | ||||||
| @@ -74,9 +76,18 @@ __________________ | |||||||
| - Run `./libredesk --set-system-user-password` to set the password for the System user. | - Run `./libredesk --set-system-user-password` to set the password for the System user. | ||||||
| - Run `./libredesk` and visit `http://localhost:9000` and login with username `System` and the password you set using the --set-system-user-password command. | - Run `./libredesk` and visit `http://localhost:9000` and login with username `System` and the password you set using the --set-system-user-password command. | ||||||
|  |  | ||||||
| See [installation docs](https://libredesk.io/docs/installation) | See [installation docs](https://docs.libredesk.io/getting-started/installation) | ||||||
| __________________ | __________________ | ||||||
|  |  | ||||||
|  |  | ||||||
| ## Developers | ## Developers | ||||||
| If you are interested in contributing, refer to the [developer setup](https://libredesk.io/docs/developer-setup/). The backend is written in Go and the frontend is Vue js 3 with Shadcn for UI components. | If you are interested in contributing, refer to the [developer setup](https://docs.libredesk.io/contributing/developer-setup). The backend is written in Go and the frontend is Vue js 3 with Shadcn for UI components. | ||||||
|  |  | ||||||
|  | ## Development Status | ||||||
|  |  | ||||||
|  | Libredesk is under active development.   | ||||||
|  | Track roadmap and progress on the GitHub Project Board:   [https://github.com/users/abhinavxd/projects/1](https://github.com/users/abhinavxd/projects/1) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | ## Translators | ||||||
|  | You can help translate Libredesk into your language on [Crowdin](https://crowdin.com/project/libredesk).   | ||||||
|   | |||||||
							
								
								
									
										36
									
								
								cmd/actvity_log.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								cmd/actvity_log.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"strconv" | ||||||
|  |  | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	"github.com/zerodha/fastglue" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // handleGetActivityLogs returns activity logs from the database. | ||||||
|  | func handleGetActivityLogs(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app         = r.Context.(*App) | ||||||
|  | 		order       = string(r.RequestCtx.QueryArgs().Peek("order")) | ||||||
|  | 		orderBy     = string(r.RequestCtx.QueryArgs().Peek("order_by")) | ||||||
|  | 		filters     = string(r.RequestCtx.QueryArgs().Peek("filters")) | ||||||
|  | 		page, _     = strconv.Atoi(string(r.RequestCtx.QueryArgs().Peek("page"))) | ||||||
|  | 		pageSize, _ = strconv.Atoi(string(r.RequestCtx.QueryArgs().Peek("page_size"))) | ||||||
|  | 		total       = 0 | ||||||
|  | 	) | ||||||
|  | 	logs, err := app.activityLog.GetAll(order, orderBy, filters, page, pageSize) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	if len(logs) > 0 { | ||||||
|  | 		total = logs[0].Total | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
|  | 		Results:    logs, | ||||||
|  | 		Total:      total, | ||||||
|  | 		PerPage:    pageSize, | ||||||
|  | 		TotalPages: (total + pageSize - 1) / pageSize, | ||||||
|  | 		Page:       page, | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										19
									
								
								cmd/ai.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								cmd/ai.go
									
									
									
									
									
								
							| @@ -5,6 +5,11 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | type aiCompletionReq struct { | ||||||
|  | 	PromptKey string `json:"prompt_key"` | ||||||
|  | 	Content   string `json:"content"` | ||||||
|  | } | ||||||
|  |  | ||||||
| type providerUpdateReq struct { | type providerUpdateReq struct { | ||||||
| 	Provider string `json:"provider"` | 	Provider string `json:"provider"` | ||||||
| 	APIKey   string `json:"api_key"` | 	APIKey   string `json:"api_key"` | ||||||
| @@ -13,11 +18,15 @@ type providerUpdateReq struct { | |||||||
| // handleAICompletion handles AI completion requests | // handleAICompletion handles AI completion requests | ||||||
| func handleAICompletion(r *fastglue.Request) error { | func handleAICompletion(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app       = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		promptKey = string(r.RequestCtx.PostArgs().Peek("prompt_key")) | 		req = aiCompletionReq{} | ||||||
| 		content   = string(r.RequestCtx.PostArgs().Peek("content")) |  | ||||||
| 	) | 	) | ||||||
| 	resp, err := app.ai.Completion(promptKey, content) |  | ||||||
|  | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	resp, err := app.ai.Completion(req.PromptKey, req.Content) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -43,7 +52,7 @@ func handleUpdateAIProvider(r *fastglue.Request) error { | |||||||
| 		req providerUpdateReq | 		req providerUpdateReq | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, "Error unmarshalling request", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
| 	} | 	} | ||||||
| 	if err := app.ai.UpdateProvider(req.Provider, req.APIKey); err != nil { | 	if err := app.ai.UpdateProvider(req.Provider, req.APIKey); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
|   | |||||||
							
								
								
									
										33
									
								
								cmd/auth.go
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								cmd/auth.go
									
									
									
									
									
								
							| @@ -6,6 +6,7 @@ import ( | |||||||
| 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/stringutil" | 	"github.com/abhinavxd/libredesk/internal/stringutil" | ||||||
|  | 	realip "github.com/ferluci/fast-realip" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
| @@ -22,20 +23,21 @@ func handleOIDCLogin(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error parsing provider id", "error", err) | 		app.lo.Error("error parsing provider id", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error parsing provider id.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set a state and save it in the session, to prevent CSRF attacks. | 	// Set a state and save it in the session, to prevent CSRF attacks. | ||||||
| 	state, err := stringutil.RandomAlphanumeric(32) | 	state, err := stringutil.RandomAlphanumeric(32) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error generating state", "error", err) | 		app.lo.Error("error generating state", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error generating state.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorGenerating", "name", "state"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.auth.SetSessionValues(r, map[string]interface{}{ | 	if err = app.auth.SetSessionValues(r, map[string]interface{}{ | ||||||
| 		oidcStateSessKey: state, | 		oidcStateSessKey: state, | ||||||
| 	}); err != nil { | 	}); err != nil { | ||||||
| 		app.lo.Error("error saving state in session", "error", err) | 		app.lo.Error("error saving state in session", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error saving state in session.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorSaving", "name", "{globals.terms.session}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	authURL, err := app.auth.LoginURL(providerID, state) | 	authURL, err := app.auth.LoginURL(providerID, state) | ||||||
| @@ -52,30 +54,32 @@ func handleOIDCCallback(r *fastglue.Request) error { | |||||||
| 		code            = string(r.RequestCtx.QueryArgs().Peek("code")) | 		code            = string(r.RequestCtx.QueryArgs().Peek("code")) | ||||||
| 		state           = string(r.RequestCtx.QueryArgs().Peek("state")) | 		state           = string(r.RequestCtx.QueryArgs().Peek("state")) | ||||||
| 		providerID, err = strconv.Atoi(string(r.RequestCtx.UserValue("id").(string))) | 		providerID, err = strconv.Atoi(string(r.RequestCtx.UserValue("id").(string))) | ||||||
|  | 		ip              = realip.FromRequest(r.RequestCtx) | ||||||
| 	) | 	) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error parsing provider id", "error", err) | 		app.lo.Error("error parsing provider id", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error parsing provider id.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Compare the state from the session with the state from the query. | 	// Compare the state from the session with the state from the query. | ||||||
| 	sessionState, err := app.auth.GetSessionValue(r, oidcStateSessKey) | 	sessionState, err := app.auth.GetSessionValue(r, oidcStateSessKey) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error getting state from session", "error", err) | 		app.lo.Error("error getting state from session", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error getting state from session.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.session}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
| 	if state != sessionState { | 	if state != sessionState { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, "Invalid state.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.Ts("globals.messages.mismatch", "name", "{globals.terms.state}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	_, claims, err := app.auth.ExchangeOIDCToken(r.RequestCtx, providerID, code) | 	_, claims, err := app.auth.ExchangeOIDCToken(r.RequestCtx, providerID, code) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error exchanging oidc token", "error", err) | 		app.lo.Error("error exchanging oidc token", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error exchanging OIDC token.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, | ||||||
|  | 			app.i18n.T("globals.messages.errorExchangingToken"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Lookup the user by email and set the session. | 	// Lookup the user by email and set the session. | ||||||
| 	user, err := app.user.GetAgentByEmail(claims.Email) | 	user, err := app.user.GetAgent(0, claims.Email) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -86,7 +90,18 @@ func handleOIDCCallback(r *fastglue.Request) error { | |||||||
| 		FirstName: user.FirstName, | 		FirstName: user.FirstName, | ||||||
| 		LastName:  user.LastName, | 		LastName:  user.LastName, | ||||||
| 	}, r); err != nil { | 	}, r); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error saving session.", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, | ||||||
|  | 			app.i18n.Ts("globals.messages.errorSaving", "name", "{globals.terms.session}"), nil, envelope.GeneralError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Update last login time. | ||||||
|  | 	if err := app.user.UpdateLastLoginAt(user.ID); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Insert activity log. | ||||||
|  | 	if err := app.activityLog.Login(user.ID, user.Email.String, ip); err != nil { | ||||||
|  | 		app.lo.Error("error creating login activity log", "error", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.Redirect("/", fasthttp.StatusFound, nil, "") | 	return r.Redirect("/", fasthttp.StatusFound, nil, "") | ||||||
|   | |||||||
| @@ -9,6 +9,10 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | type updateAutomationRuleExecutionModeReq struct { | ||||||
|  | 	Mode string `json:"mode"` | ||||||
|  | } | ||||||
|  |  | ||||||
| // handleGetAutomationRules gets all automation rules | // handleGetAutomationRules gets all automation rules | ||||||
| func handleGetAutomationRules(r *fastglue.Request) error { | func handleGetAutomationRules(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| @@ -41,10 +45,11 @@ func handleToggleAutomationRule(r *fastglue.Request) error { | |||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	) | 	) | ||||||
| 	if err := app.automation.ToggleRule(id); err != nil { | 	toggledRule, err := app.automation.ToggleRule(id) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Rule toggled successfully") | 	return r.SendEnvelope(toggledRule) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateAutomationRule updates an automation rule | // handleUpdateAutomationRule updates an automation rule | ||||||
| @@ -55,18 +60,18 @@ func handleUpdateAutomationRule(r *fastglue.Request) error { | |||||||
| 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	) | 	) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid rule `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&rule, "json"); err != nil { | 	if err := r.Decode(&rule, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.automation.UpdateRule(id, rule);err != nil { | 	updatedRule, err := app.automation.UpdateRule(id, rule) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Rule updated successfully") | 	return r.SendEnvelope(updatedRule) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleCreateAutomationRule creates a new automation rule | // handleCreateAutomationRule creates a new automation rule | ||||||
| @@ -76,12 +81,13 @@ func handleCreateAutomationRule(r *fastglue.Request) error { | |||||||
| 		rule = amodels.RuleRecord{} | 		rule = amodels.RuleRecord{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&rule, "json"); err != nil { | 	if err := r.Decode(&rule, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err := app.automation.CreateRule(rule); err != nil { | 	createdRule, err := app.automation.CreateRule(rule) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Rule created successfully") | 	return r.SendEnvelope(createdRule) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteAutomationRule deletes an automation rule | // handleDeleteAutomationRule deletes an automation rule | ||||||
| @@ -92,15 +98,12 @@ func handleDeleteAutomationRule(r *fastglue.Request) error { | |||||||
| 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	) | 	) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid rule `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  | 	if err = app.automation.DeleteRule(id); err != nil { | ||||||
| 	err = app.automation.DeleteRule(id) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Rule deleted successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateAutomationRuleWeights updates the weights of the automation rules | // handleUpdateAutomationRuleWeights updates the weights of the automation rules | ||||||
| @@ -110,27 +113,33 @@ func handleUpdateAutomationRuleWeights(r *fastglue.Request) error { | |||||||
| 		weights = make(map[int]int) | 		weights = make(map[int]int) | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&weights, "json"); err != nil { | 	if err := r.Decode(&weights, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	err := app.automation.UpdateRuleWeights(weights) | 	err := app.automation.UpdateRuleWeights(weights) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Weights updated successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateAutomationRuleExecutionMode updates the execution mode of the automation rules for a given type | // handleUpdateAutomationRuleExecutionMode updates the execution mode of the automation rules for a given type | ||||||
| func handleUpdateAutomationRuleExecutionMode(r *fastglue.Request) error { | func handleUpdateAutomationRuleExecutionMode(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app  = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		mode = string(r.RequestCtx.PostArgs().Peek("mode")) | 		req = updateAutomationRuleExecutionModeReq{} | ||||||
| 	) | 	) | ||||||
| 	if mode != amodels.ExecutionModeAll && mode != amodels.ExecutionModeFirstMatch { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid execution mode", nil, envelope.InputError) | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if req.Mode != amodels.ExecutionModeAll && req.Mode != amodels.ExecutionModeFirstMatch { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("automation.invalidRuleExecutionMode"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// Only new conversation rules can be updated as they are the only ones that have execution mode. | 	// Only new conversation rules can be updated as they are the only ones that have execution mode. | ||||||
| 	if err := app.automation.UpdateRuleExecutionMode(amodels.RuleTypeNewConversation, mode); err != nil { | 	if err := app.automation.UpdateRuleExecutionMode(amodels.RuleTypeNewConversation, req.Mode); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Execution mode updated successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -29,14 +29,14 @@ func handleGetBusinessHour(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid business hour `id`.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	businessHour, err := app.businessHours.Get(id) | 	businessHour, err := app.businessHours.Get(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		if err == businessHours.ErrBusinessHoursNotFound { | 		if err == businessHours.ErrBusinessHoursNotFound { | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusNotFound, err.Error(), nil, envelope.NotFoundError) | 			return r.SendErrorEnvelope(fasthttp.StatusNotFound, err.Error(), nil, envelope.NotFoundError) | ||||||
| 		} | 		} | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error fetching business hour", nil, "") | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.businessHour}"), nil, "") | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(businessHour) | 	return r.SendEnvelope(businessHour) | ||||||
| } | } | ||||||
| @@ -48,18 +48,19 @@ func handleCreateBusinessHours(r *fastglue.Request) error { | |||||||
| 		businessHours = models.BusinessHours{} | 		businessHours = models.BusinessHours{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&businessHours, "json"); err != nil { | 	if err := r.Decode(&businessHours, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if businessHours.Name == "" { | 	if businessHours.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty business hour `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := app.businessHours.Create(businessHours.Name, businessHours.Description, businessHours.IsAlwaysOpen, businessHours.Hours, businessHours.Holidays); err != nil { | 	createdBusinessHours, err := app.businessHours.Create(businessHours.Name, businessHours.Description, businessHours.IsAlwaysOpen, businessHours.Hours, businessHours.Holidays) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(createdBusinessHours) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteBusinessHour deletes the business hour with the given id. | // handleDeleteBusinessHour deletes the business hour with the given id. | ||||||
| @@ -69,14 +70,11 @@ func handleDeleteBusinessHour(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid business hour `id`.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  | 	if err = app.businessHours.Delete(id); err != nil { | ||||||
| 	err = app.businessHours.Delete(id) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -88,20 +86,17 @@ func handleUpdateBusinessHours(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid business hour `id`.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&businessHours, "json"); err != nil { | 	if err := r.Decode(&businessHours, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if businessHours.Name == "" { | 	if businessHours.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty business hour `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  | 	updatedBusinessHours, err := app.businessHours.Update(id, businessHours.Name, businessHours.Description, businessHours.IsAlwaysOpen, businessHours.Hours, businessHours.Holidays) | ||||||
| 	if err := app.businessHours.Update(id, businessHours.Name, businessHours.Description, businessHours.IsAlwaysOpen, businessHours.Hours, businessHours.Holidays); err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	return r.SendEnvelope(updatedBusinessHours) | ||||||
| 	return r.SendEnvelope(true) |  | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										63
									
								
								cmd/config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								cmd/config.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  |  | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	"github.com/zerodha/fastglue" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // handleGetConfig returns the public configuration needed for app initialization, this includes minimal app settings and enabled SSO providers (without secrets). | ||||||
|  | func handleGetConfig(r *fastglue.Request) error { | ||||||
|  | 	var app = r.Context.(*App) | ||||||
|  |  | ||||||
|  | 	// Get app settings | ||||||
|  | 	settingsJSON, err := app.setting.GetByPrefix("app") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Unmarshal settings | ||||||
|  | 	var settings map[string]any | ||||||
|  | 	if err := json.Unmarshal(settingsJSON, &settings); err != nil { | ||||||
|  | 		app.lo.Error("error unmarshalling settings", "err", err) | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", app.i18n.T("globals.terms.setting")), nil)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Filter to only include public fields needed for initial app load | ||||||
|  | 	publicSettings := map[string]any{ | ||||||
|  | 		"app.lang":        settings["app.lang"], | ||||||
|  | 		"app.favicon_url": settings["app.favicon_url"], | ||||||
|  | 		"app.logo_url":    settings["app.logo_url"], | ||||||
|  | 		"app.site_name":   settings["app.site_name"], | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Get all OIDC providers | ||||||
|  | 	oidcProviders, err := app.oidc.GetAll() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Filter for enabled providers and remove client_secret | ||||||
|  | 	enabledProviders := make([]map[string]any, 0) | ||||||
|  | 	for _, provider := range oidcProviders { | ||||||
|  | 		if provider.Enabled { | ||||||
|  | 			providerMap := map[string]any{ | ||||||
|  | 				"id":           provider.ID, | ||||||
|  | 				"name":         provider.Name, | ||||||
|  | 				"provider":     provider.Provider, | ||||||
|  | 				"provider_url": provider.ProviderURL, | ||||||
|  | 				"client_id":    provider.ClientID, | ||||||
|  | 				"logo_url":     provider.ProviderLogoURL, | ||||||
|  | 				"enabled":      provider.Enabled, | ||||||
|  | 				"redirect_uri": provider.RedirectURI, | ||||||
|  | 			} | ||||||
|  | 			enabledProviders = append(enabledProviders, providerMap) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Add SSO providers to the response | ||||||
|  | 	publicSettings["app.sso_providers"] = enabledProviders | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(publicSettings) | ||||||
|  | } | ||||||
							
								
								
									
										288
									
								
								cmd/contacts.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										288
									
								
								cmd/contacts.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,288 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"path/filepath" | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
|  | 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/stringutil" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/user/models" | ||||||
|  | 	"github.com/valyala/fasthttp" | ||||||
|  | 	"github.com/volatiletech/null/v9" | ||||||
|  | 	"github.com/zerodha/fastglue" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type createContactNoteReq struct { | ||||||
|  | 	Note string `json:"note"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type blockContactReq struct { | ||||||
|  | 	Enabled bool `json:"enabled"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGetContacts returns a list of contacts from the database. | ||||||
|  | func handleGetContacts(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app         = r.Context.(*App) | ||||||
|  | 		order       = string(r.RequestCtx.QueryArgs().Peek("order")) | ||||||
|  | 		orderBy     = string(r.RequestCtx.QueryArgs().Peek("order_by")) | ||||||
|  | 		filters     = string(r.RequestCtx.QueryArgs().Peek("filters")) | ||||||
|  | 		page, _     = strconv.Atoi(string(r.RequestCtx.QueryArgs().Peek("page"))) | ||||||
|  | 		pageSize, _ = strconv.Atoi(string(r.RequestCtx.QueryArgs().Peek("page_size"))) | ||||||
|  | 		total       = 0 | ||||||
|  | 	) | ||||||
|  | 	contacts, err := app.user.GetContacts(page, pageSize, order, orderBy, filters) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	if len(contacts) > 0 { | ||||||
|  | 		total = contacts[0].Total | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
|  | 		Results:    contacts, | ||||||
|  | 		Total:      total, | ||||||
|  | 		PerPage:    pageSize, | ||||||
|  | 		TotalPages: (total + pageSize - 1) / pageSize, | ||||||
|  | 		Page:       page, | ||||||
|  | 	}) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGetTags returns a contact from the database. | ||||||
|  | func handleGetContact(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	c, err := app.user.GetContact(id, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(c) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleUpdateContact updates a contact in the database. | ||||||
|  | func handleUpdateContact(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	contact, err := app.user.GetContact(id, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	form, err := r.RequestCtx.MultipartForm() | ||||||
|  | 	if err != nil { | ||||||
|  | 		app.lo.Error("error parsing form data", "error", err) | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.GeneralError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Parse form data | ||||||
|  | 	firstName := "" | ||||||
|  | 	if v, ok := form.Value["first_name"]; ok && len(v) > 0 { | ||||||
|  | 		firstName = string(v[0]) | ||||||
|  | 	} | ||||||
|  | 	lastName := "" | ||||||
|  | 	if v, ok := form.Value["last_name"]; ok && len(v) > 0 { | ||||||
|  | 		lastName = string(v[0]) | ||||||
|  | 	} | ||||||
|  | 	email := "" | ||||||
|  | 	if v, ok := form.Value["email"]; ok && len(v) > 0 { | ||||||
|  | 		email = strings.TrimSpace(string(v[0])) | ||||||
|  | 	} | ||||||
|  | 	phoneNumber := "" | ||||||
|  | 	if v, ok := form.Value["phone_number"]; ok && len(v) > 0 { | ||||||
|  | 		phoneNumber = string(v[0]) | ||||||
|  | 	} | ||||||
|  | 	phoneNumberCountryCode := "" | ||||||
|  | 	if v, ok := form.Value["phone_number_country_code"]; ok && len(v) > 0 { | ||||||
|  | 		phoneNumberCountryCode = string(v[0]) | ||||||
|  | 	} | ||||||
|  | 	avatarURL := "" | ||||||
|  | 	if v, ok := form.Value["avatar_url"]; ok && len(v) > 0 { | ||||||
|  | 		avatarURL = string(v[0]) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Set nulls to empty strings. | ||||||
|  | 	if avatarURL == "null" { | ||||||
|  | 		avatarURL = "" | ||||||
|  | 	} | ||||||
|  | 	if phoneNumberCountryCode == "null" { | ||||||
|  | 		phoneNumberCountryCode = "" | ||||||
|  | 	} | ||||||
|  | 	if phoneNumber == "null" { | ||||||
|  | 		phoneNumber = "" | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Validate mandatory fields. | ||||||
|  | 	if email == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "email"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	if !stringutil.ValidEmail(email) { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "email"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	if firstName == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "first_name"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Another contact with same new email? | ||||||
|  | 	existingContact, _ := app.user.GetContact(0, email) | ||||||
|  | 	if existingContact.ID > 0 && existingContact.ID != id { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("contact.alreadyExistsWithEmail"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	contactToUpdate := models.User{ | ||||||
|  | 		FirstName:              firstName, | ||||||
|  | 		LastName:               lastName, | ||||||
|  | 		Email:                  null.StringFrom(email), | ||||||
|  | 		AvatarURL:              null.NewString(avatarURL, avatarURL != ""), | ||||||
|  | 		PhoneNumber:            null.NewString(phoneNumber, phoneNumber != ""), | ||||||
|  | 		PhoneNumberCountryCode: null.NewString(phoneNumberCountryCode, phoneNumberCountryCode != ""), | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := app.user.UpdateContact(id, contactToUpdate); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Delete avatar? | ||||||
|  | 	if avatarURL == "" && contact.AvatarURL.Valid { | ||||||
|  | 		fileName := filepath.Base(contact.AvatarURL.String) | ||||||
|  | 		app.media.Delete(fileName) | ||||||
|  | 		contact.AvatarURL.Valid = false | ||||||
|  | 		contact.AvatarURL.String = "" | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Upload avatar? | ||||||
|  | 	files, ok := form.File["files"] | ||||||
|  | 	if ok && len(files) > 0 { | ||||||
|  | 		if err := uploadUserAvatar(r, contact, files); err != nil { | ||||||
|  | 			return sendErrorEnvelope(r, err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Refetch contact and return it | ||||||
|  | 	contact, err = app.user.GetContact(id, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(contact) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGetContactNotes returns all notes for a contact. | ||||||
|  | func handleGetContactNotes(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app          = r.Context.(*App) | ||||||
|  | 		contactID, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  | 	if contactID <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	notes, err := app.user.GetNotes(contactID) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(notes) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleCreateContactNote creates a note for a contact. | ||||||
|  | func handleCreateContactNote(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app          = r.Context.(*App) | ||||||
|  | 		contactID, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 		auser        = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		req          = createContactNoteReq{} | ||||||
|  | 	) | ||||||
|  | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
|  | 	} | ||||||
|  | 	if len(req.Note) == 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "note"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	n, err := app.user.CreateNote(contactID, auser.ID, req.Note) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	n, err = app.user.GetNote(n.ID) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(n) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleDeleteContactNote deletes a note for a contact. | ||||||
|  | func handleDeleteContactNote(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app          = r.Context.(*App) | ||||||
|  | 		contactID, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 		noteID, _    = strconv.Atoi(r.RequestCtx.UserValue("note_id").(string)) | ||||||
|  | 		auser        = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 	) | ||||||
|  | 	if contactID <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	if noteID <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`note_id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	agent, err := app.user.GetAgent(auser.ID, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Allow deletion of only own notes and not those created by others, but also allow `Admin` to delete any note. | ||||||
|  | 	if !agent.HasAdminRole() { | ||||||
|  | 		note, err := app.user.GetNote(noteID) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return sendErrorEnvelope(r, err) | ||||||
|  | 		} | ||||||
|  | 		if note.UserID != auser.ID { | ||||||
|  | 			return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.Ts("globals.messages.canOnlyDeleteOwn", "name", "{globals.terms.note}"), nil, envelope.InputError) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	app.lo.Info("deleting contact note", "note_id", noteID, "contact_id", contactID, "actor_id", auser.ID) | ||||||
|  |  | ||||||
|  | 	if err := app.user.DeleteNote(noteID, contactID); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleBlockContact blocks a contact. | ||||||
|  | func handleBlockContact(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app          = r.Context.(*App) | ||||||
|  | 		contactID, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 		auser        = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		req          = blockContactReq{} | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	if contactID <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	app.lo.Info("setting contact block status", "contact_id", contactID, "enabled", req.Enabled, "actor_id", auser.ID) | ||||||
|  |  | ||||||
|  | 	if err := app.user.ToggleEnabled(contactID, models.UserTypeContact, req.Enabled); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	contact, err := app.user.GetContact(contactID, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(contact) | ||||||
|  | } | ||||||
| @@ -1,9 +1,7 @@ | |||||||
| package main | package main | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" |  | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" |  | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | ||||||
| @@ -11,12 +9,49 @@ import ( | |||||||
| 	"github.com/abhinavxd/libredesk/internal/automation/models" | 	"github.com/abhinavxd/libredesk/internal/automation/models" | ||||||
| 	cmodels "github.com/abhinavxd/libredesk/internal/conversation/models" | 	cmodels "github.com/abhinavxd/libredesk/internal/conversation/models" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	medModels "github.com/abhinavxd/libredesk/internal/media/models" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/stringutil" | ||||||
| 	umodels "github.com/abhinavxd/libredesk/internal/user/models" | 	umodels "github.com/abhinavxd/libredesk/internal/user/models" | ||||||
|  | 	wmodels "github.com/abhinavxd/libredesk/internal/webhook/models" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/volatiletech/null/v9" | 	"github.com/volatiletech/null/v9" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | type assigneeChangeReq struct { | ||||||
|  | 	AssigneeID int `json:"assignee_id"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type teamAssigneeChangeReq struct { | ||||||
|  | 	AssigneeID int `json:"assignee_id"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type priorityUpdateReq struct { | ||||||
|  | 	Priority string `json:"priority"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type statusUpdateReq struct { | ||||||
|  | 	Status       string `json:"status"` | ||||||
|  | 	SnoozedUntil string `json:"snoozed_until,omitempty"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type tagsUpdateReq struct { | ||||||
|  | 	Tags []string `json:"tags"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type createConversationRequest struct { | ||||||
|  | 	InboxID         int    `json:"inbox_id"` | ||||||
|  | 	AssignedAgentID int    `json:"agent_id"` | ||||||
|  | 	AssignedTeamID  int    `json:"team_id"` | ||||||
|  | 	Email           string `json:"contact_email"` | ||||||
|  | 	FirstName       string `json:"first_name"` | ||||||
|  | 	LastName        string `json:"last_name"` | ||||||
|  | 	Subject         string `json:"subject"` | ||||||
|  | 	Content         string `json:"content"` | ||||||
|  | 	Attachments     []int  `json:"attachments"` | ||||||
|  | 	Initiator       string `json:"initiator"` // "contact" | "agent" | ||||||
|  | } | ||||||
|  |  | ||||||
| // handleGetAllConversations retrieves all conversations. | // handleGetAllConversations retrieves all conversations. | ||||||
| func handleGetAllConversations(r *fastglue.Request) error { | func handleGetAllConversations(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| @@ -38,13 +73,6 @@ func handleGetAllConversations(r *fastglue.Request) error { | |||||||
| 		total = conversations[0].Total | 		total = conversations[0].Total | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set deadlines for SLA if conversation has a policy |  | ||||||
| 	for i := range conversations { |  | ||||||
| 		if conversations[i].SLAPolicyID.Int != 0 { |  | ||||||
| 			setSLADeadlines(app, &conversations[i]) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(envelope.PageResults{ | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
| 		Results:    conversations, | 		Results:    conversations, | ||||||
| 		Total:      total, | 		Total:      total, | ||||||
| @@ -68,19 +96,12 @@ func handleGetAssignedConversations(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	conversations, err := app.conversation.GetAssignedConversationsList(user.ID, order, orderBy, filters, page, pageSize) | 	conversations, err := app.conversation.GetAssignedConversationsList(user.ID, order, orderBy, filters, page, pageSize) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, "") | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if len(conversations) > 0 { | 	if len(conversations) > 0 { | ||||||
| 		total = conversations[0].Total | 		total = conversations[0].Total | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set deadlines for SLA if conversation has a policy |  | ||||||
| 	for i := range conversations { |  | ||||||
| 		if conversations[i].SLAPolicyID.Int != 0 { |  | ||||||
| 			setSLADeadlines(app, &conversations[i]) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(envelope.PageResults{ | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
| 		Results:    conversations, | 		Results:    conversations, | ||||||
| 		Total:      total, | 		Total:      total, | ||||||
| @@ -104,19 +125,12 @@ func handleGetUnassignedConversations(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	conversations, err := app.conversation.GetUnassignedConversationsList(order, orderBy, filters, page, pageSize) | 	conversations, err := app.conversation.GetUnassignedConversationsList(order, orderBy, filters, page, pageSize) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, "") | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if len(conversations) > 0 { | 	if len(conversations) > 0 { | ||||||
| 		total = conversations[0].Total | 		total = conversations[0].Total | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set deadlines for SLA if conversation has a policy |  | ||||||
| 	for i := range conversations { |  | ||||||
| 		if conversations[i].SLAPolicyID.Int != 0 { |  | ||||||
| 			setSLADeadlines(app, &conversations[i]) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(envelope.PageResults{ | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
| 		Results:    conversations, | 		Results:    conversations, | ||||||
| 		Total:      total, | 		Total:      total, | ||||||
| @@ -139,7 +153,7 @@ func handleGetViewConversations(r *fastglue.Request) error { | |||||||
| 		total       = 0 | 		total       = 0 | ||||||
| 	) | 	) | ||||||
| 	if viewID < 1 { | 	if viewID < 1 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `view_id`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`view_id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Check if user has access to the view. | 	// Check if user has access to the view. | ||||||
| @@ -148,15 +162,15 @@ func handleGetViewConversations(r *fastglue.Request) error { | |||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if view.UserID != auser.ID { | 	if view.UserID != auser.ID { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, "You don't have access to this view.", nil, envelope.PermissionError) | 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.T("conversation.viewPermissionDenied"), nil, envelope.PermissionError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Prepare lists user has access to based on user permissions, internally this affects the SQL query. | 	// Prepare lists user has access to based on user permissions, internally this prepares the SQL query. | ||||||
| 	lists := []string{} | 	lists := []string{} | ||||||
| 	for _, perm := range user.Permissions { | 	for _, perm := range user.Permissions { | ||||||
| 		if perm == authzModels.PermConversationsReadAll { | 		if perm == authzModels.PermConversationsReadAll { | ||||||
| @@ -177,7 +191,7 @@ func handleGetViewConversations(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	// No lists found, user doesn't have access to any conversations. | 	// No lists found, user doesn't have access to any conversations. | ||||||
| 	if len(lists) == 0 { | 	if len(lists) == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, "Permission denied", nil, envelope.PermissionError) | 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.Ts("globals.messages.denied", "name", "{globals.terms.permission}"), nil, envelope.PermissionError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	conversations, err := app.conversation.GetViewConversationsList(user.ID, user.Teams.IDs(), lists, order, orderBy, string(view.Filters), page, pageSize) | 	conversations, err := app.conversation.GetViewConversationsList(user.ID, user.Teams.IDs(), lists, order, orderBy, string(view.Filters), page, pageSize) | ||||||
| @@ -188,13 +202,6 @@ func handleGetViewConversations(r *fastglue.Request) error { | |||||||
| 		total = conversations[0].Total | 		total = conversations[0].Total | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set deadlines for SLA if conversation has a policy |  | ||||||
| 	for i := range conversations { |  | ||||||
| 		if conversations[i].SLAPolicyID.Int != 0 { |  | ||||||
| 			setSLADeadlines(app, &conversations[i]) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(envelope.PageResults{ | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
| 		Results:    conversations, | 		Results:    conversations, | ||||||
| 		Total:      total, | 		Total:      total, | ||||||
| @@ -219,7 +226,7 @@ func handleGetTeamUnassignedConversations(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	teamID, _ := strconv.Atoi(teamIDStr) | 	teamID, _ := strconv.Atoi(teamIDStr) | ||||||
| 	if teamID < 1 { | 	if teamID < 1 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `team_id`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`team_id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Check if user belongs to the team. | 	// Check if user belongs to the team. | ||||||
| @@ -229,7 +236,7 @@ func handleGetTeamUnassignedConversations(r *fastglue.Request) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if !exists { | 	if !exists { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.PermissionError, "You're not a member of this team, Please refresh the page and try again.", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.PermissionError, app.i18n.T("conversation.notMemberOfTeam"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	conversations, err := app.conversation.GetTeamUnassignedConversationsList(teamID, order, orderBy, filters, page, pageSize) | 	conversations, err := app.conversation.GetTeamUnassignedConversationsList(teamID, order, orderBy, filters, page, pageSize) | ||||||
| @@ -240,13 +247,6 @@ func handleGetTeamUnassignedConversations(r *fastglue.Request) error { | |||||||
| 		total = conversations[0].Total | 		total = conversations[0].Total | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set deadlines for SLA if conversation has a policy |  | ||||||
| 	for i := range conversations { |  | ||||||
| 		if conversations[i].SLAPolicyID.Int != 0 { |  | ||||||
| 			setSLADeadlines(app, &conversations[i]) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(envelope.PageResults{ | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
| 		Results:    conversations, | 		Results:    conversations, | ||||||
| 		Total:      total, | 		Total:      total, | ||||||
| @@ -264,7 +264,7 @@ func handleGetConversation(r *fastglue.Request) error { | |||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -274,12 +274,8 @@ func handleGetConversation(r *fastglue.Request) error { | |||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if conv.SLAPolicyID.Int != 0 { | 	prev, _ := app.conversation.GetContactPreviousConversations(conv.ContactID, 10) | ||||||
| 		setSLADeadlines(app, conv) | 	conv.PreviousConversations = filterCurrentPreviousConv(prev, conv.UUID) | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	prev, _ := app.conversation.GetContactConversations(conv.ContactID) |  | ||||||
| 	conv.PreviousConversations = filterCurrentConv(prev, conv.UUID) |  | ||||||
| 	return r.SendEnvelope(conv) | 	return r.SendEnvelope(conv) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -290,7 +286,7 @@ func handleUpdateConversationAssigneeLastSeen(r *fastglue.Request) error { | |||||||
| 		uuid  = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -301,7 +297,7 @@ func handleUpdateConversationAssigneeLastSeen(r *fastglue.Request) error { | |||||||
| 	if err = app.conversation.UpdateConversationAssigneeLastSeen(uuid); err != nil { | 	if err = app.conversation.UpdateConversationAssigneeLastSeen(uuid); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Last seen updated successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetConversationParticipants retrieves participants of a conversation. | // handleGetConversationParticipants retrieves participants of a conversation. | ||||||
| @@ -311,7 +307,7 @@ func handleGetConversationParticipants(r *fastglue.Request) error { | |||||||
| 		uuid  = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -329,33 +325,37 @@ func handleGetConversationParticipants(r *fastglue.Request) error { | |||||||
| // handleUpdateUserAssignee updates the user assigned to a conversation. | // handleUpdateUserAssignee updates the user assigned to a conversation. | ||||||
| func handleUpdateUserAssignee(r *fastglue.Request) error { | func handleUpdateUserAssignee(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app        = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		uuid       = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser      = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		assigneeID = r.RequestCtx.PostArgs().GetUintOrZero("assignee_id") | 		req   = assigneeChangeReq{} | ||||||
| 	) | 	) | ||||||
| 	if assigneeID == 0 { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `assignee_id`", nil, envelope.InputError) | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		app.lo.Error("error decoding assignee change request", "error", err) | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	_, err = enforceConversationAccess(app, uuid, user) | 	conversation, err := enforceConversationAccess(app, uuid, user) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := app.conversation.UpdateConversationUserAssignee(uuid, assigneeID, user); err != nil { | 	// Already assigned? | ||||||
|  | 	if conversation.AssignedUserID.Int == req.AssigneeID { | ||||||
|  | 		return r.SendEnvelope(true) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := app.conversation.UpdateConversationUserAssignee(uuid, req.AssigneeID, user); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Evaluate automation rules. | 	return r.SendEnvelope(true) | ||||||
| 	app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationUserAssigned) |  | ||||||
|  |  | ||||||
| 	return r.SendEnvelope("User assigned successfully") |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateTeamAssignee updates the team assigned to a conversation. | // handleUpdateTeamAssignee updates the team assigned to a conversation. | ||||||
| @@ -364,13 +364,17 @@ func handleUpdateTeamAssignee(r *fastglue.Request) error { | |||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		uuid  = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		req   = teamAssigneeChangeReq{} | ||||||
| 	) | 	) | ||||||
| 	assigneeID, err := r.RequestCtx.PostArgs().GetUint("assignee_id") |  | ||||||
| 	if err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid assignee `id`.", nil, envelope.InputError) | 		app.lo.Error("error decoding team assignee change request", "error", err) | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	assigneeID := req.AssigneeID | ||||||
|  |  | ||||||
|  | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -384,89 +388,85 @@ func handleUpdateTeamAssignee(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// Already assigned? | ||||||
|  | 	if conversation.AssignedTeamID.Int == assigneeID { | ||||||
|  | 		return r.SendEnvelope(true) | ||||||
|  | 	} | ||||||
| 	if err := app.conversation.UpdateConversationTeamAssignee(uuid, assigneeID, user); err != nil { | 	if err := app.conversation.UpdateConversationTeamAssignee(uuid, assigneeID, user); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Evaluate automation rules on team assignment. | 	return r.SendEnvelope(true) | ||||||
| 	app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationTeamAssigned) |  | ||||||
|  |  | ||||||
| 	// Apply SLA policy if team has changed and the new team has an SLA policy. |  | ||||||
| 	if conversation.AssignedTeamID.Int != assigneeID && assigneeID != 0 { |  | ||||||
| 		team, err := app.team.Get(assigneeID) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return sendErrorEnvelope(r, err) |  | ||||||
| 		} |  | ||||||
| 		if team.SLAPolicyID.Int != 0 { |  | ||||||
| 			if err := app.conversation.ApplySLA(*conversation, team.SLAPolicyID.Int, user); err != nil { |  | ||||||
| 				return sendErrorEnvelope(r, err) |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	return r.SendEnvelope("Team assigned successfully") |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateConversationPriority updates the priority of a conversation. | // handleUpdateConversationPriority updates the priority of a conversation. | ||||||
| func handleUpdateConversationPriority(r *fastglue.Request) error { | func handleUpdateConversationPriority(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app      = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		uuid     = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser    = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		priority = string(r.RequestCtx.PostArgs().Peek("priority")) | 		req   = priorityUpdateReq{} | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
|  | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		app.lo.Error("error decoding priority update request", "error", err) | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	priority := req.Priority | ||||||
| 	if priority == "" { | 	if priority == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `priority`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`priority`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	conversation, err := app.conversation.GetConversation(0, uuid) |  | ||||||
|  | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	_, err = enforceConversationAccess(app, uuid, user) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	allowed, err := app.authz.EnforceConversationAccess(user, conversation) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
| 	if !allowed { |  | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.PermissionError, "Permission denied", nil)) |  | ||||||
| 	} |  | ||||||
| 	if err := app.conversation.UpdateConversationPriority(uuid, 0 /**priority_id**/, priority, user); err != nil { | 	if err := app.conversation.UpdateConversationPriority(uuid, 0 /**priority_id**/, priority, user); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Evaluate automation rules. | 	return r.SendEnvelope(true) | ||||||
| 	app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationPriorityChange) |  | ||||||
| 	return r.SendEnvelope("Priority updated successfully") |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateConversationStatus updates the status of a conversation. | // handleUpdateConversationStatus updates the status of a conversation. | ||||||
| func handleUpdateConversationStatus(r *fastglue.Request) error { | func handleUpdateConversationStatus(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app          = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		status       = string(r.RequestCtx.PostArgs().Peek("status")) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		snoozedUntil = string(r.RequestCtx.PostArgs().Peek("snoozed_until")) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		uuid         = r.RequestCtx.UserValue("uuid").(string) | 		req   = statusUpdateReq{} | ||||||
| 		auser        = r.RequestCtx.UserValue("user").(amodels.User) |  | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
|  | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		app.lo.Error("error decoding status update request", "error", err) | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	status := req.Status | ||||||
|  | 	snoozedUntil := req.SnoozedUntil | ||||||
|  |  | ||||||
| 	// Validate inputs | 	// Validate inputs | ||||||
| 	if status == "" { | 	if status == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `status`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`status`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if snoozedUntil == "" && status == cmodels.StatusSnoozed { | 	if snoozedUntil == "" && status == cmodels.StatusSnoozed { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `snoozed_until`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`snoozed_until`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if status == cmodels.StatusSnoozed { | 	if status == cmodels.StatusSnoozed { | ||||||
| 		_, err := time.ParseDuration(snoozedUntil) | 		_, err := time.ParseDuration(snoozedUntil) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `snoozed_until`", nil, envelope.InputError) | 			return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`snoozed_until`"), nil, envelope.InputError) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Enforce conversation access. | 	// Enforce conversation access. | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -477,7 +477,7 @@ func handleUpdateConversationStatus(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	// Make sure a user is assigned before resolving conversation. | 	// Make sure a user is assigned before resolving conversation. | ||||||
| 	if status == cmodels.StatusResolved && conversation.AssignedUserID.Int == 0 { | 	if status == cmodels.StatusResolved && conversation.AssignedUserID.Int == 0 { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, "Cannot resolve the conversation without an assigned user, Please assign a user before attempting to resolve.", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.T("conversation.resolveWithoutAssignee"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Update conversation status. | 	// Update conversation status. | ||||||
| @@ -485,9 +485,6 @@ func handleUpdateConversationStatus(r *fastglue.Request) error { | |||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Evaluate automation rules. |  | ||||||
| 	app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationStatusChange) |  | ||||||
|  |  | ||||||
| 	// If status is `Resolved`, send CSAT survey if enabled on inbox. | 	// If status is `Resolved`, send CSAT survey if enabled on inbox. | ||||||
| 	if status == cmodels.StatusResolved { | 	if status == cmodels.StatusResolved { | ||||||
| 		// Check if CSAT is enabled on the inbox and send CSAT survey message. | 		// Check if CSAT is enabled on the inbox and send CSAT survey message. | ||||||
| @@ -501,67 +498,98 @@ func handleUpdateConversationStatus(r *fastglue.Request) error { | |||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Status updated successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateConversationtags updates conversation tags. | // handleUpdateConversationtags updates conversation tags. | ||||||
| func handleUpdateConversationtags(r *fastglue.Request) error { | func handleUpdateConversationtags(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app      = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		tagNames = []string{} | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		tagJSON  = r.RequestCtx.PostArgs().Peek("tags") | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser    = r.RequestCtx.UserValue("user").(amodels.User) | 		req   = tagsUpdateReq{} | ||||||
| 		uuid     = r.RequestCtx.UserValue("uuid").(string) |  | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	if err := json.Unmarshal(tagJSON, &tagNames); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		app.lo.Error("error unmarshalling tags JSON", "error", err) | 		app.lo.Error("error decoding tags update request", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error unmarshalling tags JSON", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	conversation, err := app.conversation.GetConversation(0, uuid) |  | ||||||
|  | 	tagNames := req.Tags | ||||||
|  |  | ||||||
|  | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	_, err = enforceConversationAccess(app, uuid, user) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	if err := app.conversation.SetConversationTags(uuid, models.ActionSetTags, tagNames, user); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
| 	if allowed, err := app.authz.EnforceConversationAccess(user, conversation); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} else if !allowed { |  | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.PermissionError, "Permission denied", nil)) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if err := app.conversation.UpsertConversationTags(uuid, tagNames, user); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
| 	return r.SendEnvelope("Tags added successfully") |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDashboardCounts retrieves general dashboard counts for all users. | // handleUpdateConversationCustomAttributes updates custom attributes of a conversation. | ||||||
| func handleDashboardCounts(r *fastglue.Request) error { | func handleUpdateConversationCustomAttributes(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app        = r.Context.(*App) | ||||||
|  | 		attributes = map[string]any{} | ||||||
|  | 		auser      = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		uuid       = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 	) | 	) | ||||||
| 	counts, err := app.conversation.GetDashboardCounts(0, 0) | 	if err := r.Decode(&attributes, ""); err != nil { | ||||||
|  | 		app.lo.Error("error unmarshalling custom attributes JSON", "error", err) | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Enforce conversation access. | ||||||
|  | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(counts) | 	_, err = enforceConversationAccess(app, uuid, user) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Update custom attributes. | ||||||
|  | 	if err := app.conversation.UpdateConversationCustomAttributes(uuid, attributes); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDashboardCharts retrieves general dashboard chart data. | // handleUpdateContactCustomAttributes updates custom attributes of a contact. | ||||||
| func handleDashboardCharts(r *fastglue.Request) error { | func handleUpdateContactCustomAttributes(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app        = r.Context.(*App) | ||||||
|  | 		attributes = map[string]any{} | ||||||
|  | 		auser      = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		uuid       = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 	) | 	) | ||||||
| 	charts, err := app.conversation.GetDashboardChart(0, 0) | 	if err := r.Decode(&attributes, ""); err != nil { | ||||||
|  | 		app.lo.Error("error unmarshalling custom attributes JSON", "error", err) | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Enforce conversation access. | ||||||
|  | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(charts) | 	conversation, err := enforceConversationAccess(app, uuid, user) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	if err := app.user.UpdateCustomAttributes(conversation.ContactID, attributes); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	// Broadcast update. | ||||||
|  | 	app.conversation.BroadcastConversationUpdate(conversation.UUID, "contact.custom_attributes", attributes) | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // enforceConversationAccess fetches the conversation and checks if the user has access to it. | // enforceConversationAccess fetches the conversation and checks if the user has access to it. | ||||||
| @@ -572,7 +600,7 @@ func enforceConversationAccess(app *App, uuid string, user umodels.User) (*cmode | |||||||
| 	} | 	} | ||||||
| 	allowed, err := app.authz.EnforceConversationAccess(user, conversation) | 	allowed, err := app.authz.EnforceConversationAccess(user, conversation) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, envelope.NewError(envelope.GeneralError, "Error checking permissions", nil) | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	if !allowed { | 	if !allowed { | ||||||
| 		return nil, envelope.NewError(envelope.PermissionError, "Permission denied", nil) | 		return nil, envelope.NewError(envelope.PermissionError, "Permission denied", nil) | ||||||
| @@ -580,21 +608,6 @@ func enforceConversationAccess(app *App, uuid string, user umodels.User) (*cmode | |||||||
| 	return &conversation, nil | 	return &conversation, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // setSLADeadlines gets the latest SLA deadlines for a conversation and sets them. |  | ||||||
| func setSLADeadlines(app *App, conversation *cmodels.Conversation) error { |  | ||||||
| 	if conversation.ID < 1 { |  | ||||||
| 		return nil |  | ||||||
| 	} |  | ||||||
| 	first, resolution, err := app.sla.GetLatestDeadlines(conversation.ID) |  | ||||||
| 	if err != nil { |  | ||||||
| 		app.lo.Error("error getting SLA deadlines", "id", conversation.ID, "error", err) |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	conversation.FirstResponseDueAt = null.NewTime(first, first != time.Time{}) |  | ||||||
| 	conversation.ResolutionDueAt = null.NewTime(resolution, resolution != time.Time{}) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // handleRemoveUserAssignee removes the user assigned to a conversation. | // handleRemoveUserAssignee removes the user assigned to a conversation. | ||||||
| func handleRemoveUserAssignee(r *fastglue.Request) error { | func handleRemoveUserAssignee(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| @@ -602,7 +615,7 @@ func handleRemoveUserAssignee(r *fastglue.Request) error { | |||||||
| 		uuid  = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -610,7 +623,7 @@ func handleRemoveUserAssignee(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if err = app.conversation.RemoveConversationAssignee(uuid, "user"); err != nil { | 	if err = app.conversation.RemoveConversationAssignee(uuid, "user", user); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| @@ -623,7 +636,7 @@ func handleRemoveTeamAssignee(r *fastglue.Request) error { | |||||||
| 		uuid  = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -631,114 +644,155 @@ func handleRemoveTeamAssignee(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if err = app.conversation.RemoveConversationAssignee(uuid, "team"); err != nil { | 	if err = app.conversation.RemoveConversationAssignee(uuid, "team", user); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // filterCurrentConv removes the current conversation from the list of conversations. | // filterCurrentPreviousConv removes the current conversation from the list of previous conversations. | ||||||
| func filterCurrentConv(convs []cmodels.Conversation, uuid string) []cmodels.Conversation { | func filterCurrentPreviousConv(convs []cmodels.PreviousConversation, uuid string) []cmodels.PreviousConversation { | ||||||
| 	for i, c := range convs { | 	for i, c := range convs { | ||||||
| 		if c.UUID == uuid { | 		if c.UUID == uuid { | ||||||
| 			return append(convs[:i], convs[i+1:]...) | 			return append(convs[:i], convs[i+1:]...) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return []cmodels.Conversation{} | 	return []cmodels.PreviousConversation{} | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleCreateConversation creates a new conversation and sends a message to it. | // handleCreateConversation creates a new conversation and sends a message to it. | ||||||
| func handleCreateConversation(r *fastglue.Request) error { | func handleCreateConversation(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app             = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser           = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		inboxID         = r.RequestCtx.PostArgs().GetUintOrZero("inbox_id") | 		req   = createConversationRequest{} | ||||||
| 		assignedAgentID = r.RequestCtx.PostArgs().GetUintOrZero("agent_id") |  | ||||||
| 		assignedTeamID  = r.RequestCtx.PostArgs().GetUintOrZero("team_id") |  | ||||||
| 		email           = strings.TrimSpace(string(r.RequestCtx.PostArgs().Peek("contact_email"))) |  | ||||||
| 		firstName       = strings.TrimSpace(string(r.RequestCtx.PostArgs().Peek("first_name"))) |  | ||||||
| 		lastName        = strings.TrimSpace(string(r.RequestCtx.PostArgs().Peek("last_name"))) |  | ||||||
| 		subject         = strings.TrimSpace(string(r.RequestCtx.PostArgs().Peek("subject"))) |  | ||||||
| 		content         = string(r.RequestCtx.PostArgs().Peek("content")) |  | ||||||
| 	) | 	) | ||||||
| 	// Validate required fields |  | ||||||
| 	if inboxID <= 0 { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "inbox_id is required", nil, envelope.InputError) | 		app.lo.Error("error decoding create conversation request", "error", err) | ||||||
| 	} | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	if subject == "" { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "subject is required", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
| 	if content == "" { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "content is required", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
| 	if email == "" { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Contact email is required", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
| 	if firstName == "" { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "First name is required when creating a new contact", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	// Validate the request | ||||||
| 	if err != nil { | 	if err := validateCreateConversationRequest(req, app); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Check if inbox exists and is enabled. | 	to := []string{req.Email} | ||||||
| 	inbox, err := app.inbox.GetDBRecord(inboxID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if !inbox.Enabled { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "The chosen inbox is disabled", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// Find or create contact. | 	// Find or create contact. | ||||||
| 	contact := umodels.User{ | 	contact := umodels.User{ | ||||||
| 		Email:           null.StringFrom(email), | 		Email:           null.StringFrom(req.Email), | ||||||
| 		SourceChannelID: null.StringFrom(email), | 		SourceChannelID: null.StringFrom(req.Email), | ||||||
| 		FirstName:       firstName, | 		FirstName:       req.FirstName, | ||||||
| 		LastName:        lastName, | 		LastName:        req.LastName, | ||||||
| 		InboxID:         inboxID, | 		InboxID:         req.InboxID, | ||||||
| 	} | 	} | ||||||
| 	if err := app.user.CreateContact(&contact); err != nil { | 	if err := app.user.CreateContact(&contact); err != nil { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "Error creating contact", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorCreating", "name", "{globals.terms.contact}"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Create conversation | 	// Create conversation first. | ||||||
| 	conversationID, conversationUUID, err := app.conversation.CreateConversation( | 	conversationID, conversationUUID, err := app.conversation.CreateConversation( | ||||||
| 		contact.ID, | 		contact.ID, | ||||||
| 		contact.ContactChannelID, | 		contact.ContactChannelID, | ||||||
| 		inboxID, | 		req.InboxID, | ||||||
| 		"", /** last_message **/ | 		"",         /** last_message **/ | ||||||
| 		time.Now(), | 		time.Now(), /** last_message_at **/ | ||||||
| 		subject, | 		req.Subject, | ||||||
| 		true, /** append reference number to subject **/ | 		true, /** append reference number to subject? **/ | ||||||
| 	) | 	) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error creating conversation", "error", err) | 		app.lo.Error("error creating conversation", "error", err) | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "Error creating conversation", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorCreating", "name", "{globals.terms.conversation}"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Send reply to the created conversation. | 	// Get media for the attachment ids. | ||||||
| 	if err := app.conversation.SendReply(nil /**media**/, inboxID, auser.ID, conversationUUID, content, nil /**cc**/, nil /**bcc**/, map[string]any{} /**meta**/); err != nil { | 	var media = make([]medModels.Media, 0, len(req.Attachments)) | ||||||
| 		if err := app.conversation.DeleteConversation(conversationUUID); err != nil { | 	for _, id := range req.Attachments { | ||||||
| 			app.lo.Error("error deleting conversation", "error", err) | 		m, err := app.media.Get(id, "") | ||||||
|  | 		if err != nil { | ||||||
|  | 			app.lo.Error("error fetching media", "error", err) | ||||||
|  | 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.media}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "Error sending message", nil)) | 		media = append(media, m) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Send initial message based on the initiator of conversation. | ||||||
|  | 	switch req.Initiator { | ||||||
|  | 	case umodels.UserTypeAgent: | ||||||
|  | 		// Queue reply. | ||||||
|  | 		if _, err := app.conversation.QueueReply(media, req.InboxID, auser.ID /**sender_id**/, conversationUUID, req.Content, to, nil /**cc**/, nil /**bcc**/, map[string]any{} /**meta**/); err != nil { | ||||||
|  | 			// Delete the conversation if msg queue fails. | ||||||
|  | 			if err := app.conversation.DeleteConversation(conversationUUID); err != nil { | ||||||
|  | 				app.lo.Error("error deleting conversation", "error", err) | ||||||
|  | 			} | ||||||
|  | 			return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorSending", "name", "{globals.terms.message}"), nil)) | ||||||
|  | 		} | ||||||
|  | 	case umodels.UserTypeContact: | ||||||
|  | 		// Create contact message. | ||||||
|  | 		if _, err := app.conversation.CreateContactMessage(media, contact.ID, conversationUUID, req.Content, cmodels.ContentTypeHTML); err != nil { | ||||||
|  | 			// Delete the conversation if message creation fails. | ||||||
|  | 			if err := app.conversation.DeleteConversation(conversationUUID); err != nil { | ||||||
|  | 				app.lo.Error("error deleting conversation", "error", err) | ||||||
|  | 			} | ||||||
|  | 			return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorSending", "name", "{globals.terms.message}"), nil)) | ||||||
|  | 		} | ||||||
|  | 	default: | ||||||
|  | 		// Guard anyway. | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`initiator`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Assign the conversation to the agent or team. | 	// Assign the conversation to the agent or team. | ||||||
| 	if assignedAgentID > 0 { | 	if req.AssignedAgentID > 0 { | ||||||
| 		app.conversation.UpdateConversationUserAssignee(conversationUUID, assignedAgentID, user) | 		app.conversation.UpdateConversationUserAssignee(conversationUUID, req.AssignedAgentID, user) | ||||||
| 	} | 	} | ||||||
| 	if assignedTeamID > 0 { | 	if req.AssignedTeamID > 0 { | ||||||
| 		app.conversation.UpdateConversationTeamAssignee(conversationUUID, assignedTeamID, user) | 		app.conversation.UpdateConversationTeamAssignee(conversationUUID, req.AssignedTeamID, user) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Send the created conversation back to the client. | 	// Trigger webhook event for conversation created. | ||||||
| 	conversation, err := app.conversation.GetConversation(conversationID, "") | 	conversation, err := app.conversation.GetConversation(conversationID, "") | ||||||
| 	if err != nil { | 	if err == nil { | ||||||
| 		app.lo.Error("error fetching created conversation", "error", err) | 		app.webhook.TriggerEvent(wmodels.EventConversationCreated, conversation) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(conversation) | 	return r.SendEnvelope(conversation) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // validateCreateConversationRequest validates the create conversation request fields. | ||||||
|  | func validateCreateConversationRequest(req createConversationRequest, app *App) error { | ||||||
|  | 	if req.InboxID <= 0 { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.required", "name", "`inbox_id`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if req.Content == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.required", "name", "`content`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if req.Email == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.required", "name", "`contact_email`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if req.FirstName == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.required", "name", "`first_name`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if !stringutil.ValidEmail(req.Email) { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`contact_email`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if req.Initiator != umodels.UserTypeContact && req.Initiator != umodels.UserTypeAgent { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`initiator`"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Check if inbox exists and is enabled. | ||||||
|  | 	inbox, err := app.inbox.GetDBRecord(req.InboxID) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if !inbox.Enabled { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.disabled", "name", "inbox"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										29
									
								
								cmd/csat.go
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								cmd/csat.go
									
									
									
									
									
								
							| @@ -6,6 +6,10 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | const ( | ||||||
|  | 	maxCsatFeedbackLength = 1000 | ||||||
|  | ) | ||||||
|  |  | ||||||
| // handleShowCSAT renders the CSAT page for a given csat. | // handleShowCSAT renders the CSAT page for a given csat. | ||||||
| func handleShowCSAT(r *fastglue.Request) error { | func handleShowCSAT(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| @@ -17,7 +21,7 @@ func handleShowCSAT(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | ||||||
| 			"Data": map[string]interface{}{ | 			"Data": map[string]interface{}{ | ||||||
| 				"ErrorMessage": "Page not found", | 				"ErrorMessage": app.i18n.T("globals.messages.pageNotFound"), | ||||||
| 			}, | 			}, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| @@ -25,8 +29,8 @@ func handleShowCSAT(r *fastglue.Request) error { | |||||||
| 	if csat.ResponseTimestamp.Valid { | 	if csat.ResponseTimestamp.Valid { | ||||||
| 		return app.tmpl.RenderWebPage(r.RequestCtx, "info", map[string]interface{}{ | 		return app.tmpl.RenderWebPage(r.RequestCtx, "info", map[string]interface{}{ | ||||||
| 			"Data": map[string]interface{}{ | 			"Data": map[string]interface{}{ | ||||||
| 				"Title":   "Thank you!", | 				"Title":   app.i18n.T("globals.messages.thankYou"), | ||||||
| 				"Message": "We appreciate you taking the time to submit your feedback.", | 				"Message": app.i18n.T("csat.thankYouMessage"), | ||||||
| 			}, | 			}, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| @@ -35,14 +39,14 @@ func handleShowCSAT(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | ||||||
| 			"Data": map[string]interface{}{ | 			"Data": map[string]interface{}{ | ||||||
| 				"ErrorMessage": "Page not found", | 				"ErrorMessage": app.i18n.T("globals.messages.pageNotFound"), | ||||||
| 			}, | 			}, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return app.tmpl.RenderWebPage(r.RequestCtx, "csat", map[string]interface{}{ | 	return app.tmpl.RenderWebPage(r.RequestCtx, "csat", map[string]interface{}{ | ||||||
| 		"Data": map[string]interface{}{ | 		"Data": map[string]interface{}{ | ||||||
| 			"Title":    "Rate your interaction with us", | 			"Title": app.i18n.T("csat.pageTitle"), | ||||||
| 			"CSAT": map[string]interface{}{ | 			"CSAT": map[string]interface{}{ | ||||||
| 				"UUID": csat.UUID, | 				"UUID": csat.UUID, | ||||||
| 			}, | 			}, | ||||||
| @@ -67,7 +71,7 @@ func handleUpdateCSATResponse(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | ||||||
| 			"Data": map[string]interface{}{ | 			"Data": map[string]interface{}{ | ||||||
| 				"ErrorMessage": "Invalid `rating`", | 				"ErrorMessage": app.i18n.T("globals.messages.somethingWentWrong"), | ||||||
| 			}, | 			}, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| @@ -75,7 +79,7 @@ func handleUpdateCSATResponse(r *fastglue.Request) error { | |||||||
| 	if ratingI < 1 || ratingI > 5 { | 	if ratingI < 1 || ratingI > 5 { | ||||||
| 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | ||||||
| 			"Data": map[string]interface{}{ | 			"Data": map[string]interface{}{ | ||||||
| 				"ErrorMessage": "Invalid `rating`", | 				"ErrorMessage": app.i18n.T("globals.messages.somethingWentWrong"), | ||||||
| 			}, | 			}, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| @@ -83,11 +87,16 @@ func handleUpdateCSATResponse(r *fastglue.Request) error { | |||||||
| 	if uuid == "" { | 	if uuid == "" { | ||||||
| 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | ||||||
| 			"Data": map[string]interface{}{ | 			"Data": map[string]interface{}{ | ||||||
| 				"ErrorMessage": "Invalid `uuid`", | 				"ErrorMessage": app.i18n.T("globals.messages.somethingWentWrong"), | ||||||
| 			}, | 			}, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// Trim feedback if it exceeds max length | ||||||
|  | 	if len(feedback) > maxCsatFeedbackLength { | ||||||
|  | 		feedback = feedback[:maxCsatFeedbackLength] | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if err := app.csat.UpdateResponse(uuid, ratingI, feedback); err != nil { | 	if err := app.csat.UpdateResponse(uuid, ratingI, feedback); err != nil { | ||||||
| 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | 		return app.tmpl.RenderWebPage(r.RequestCtx, "error", map[string]interface{}{ | ||||||
| 			"Data": map[string]interface{}{ | 			"Data": map[string]interface{}{ | ||||||
| @@ -98,8 +107,8 @@ func handleUpdateCSATResponse(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	return app.tmpl.RenderWebPage(r.RequestCtx, "info", map[string]interface{}{ | 	return app.tmpl.RenderWebPage(r.RequestCtx, "info", map[string]interface{}{ | ||||||
| 		"Data": map[string]interface{}{ | 		"Data": map[string]interface{}{ | ||||||
| 			"Title":   "Thank you!", | 			"Title":   app.i18n.T("globals.messages.thankYou"), | ||||||
| 			"Message": "We appreciate you taking the time to submit your feedback.", | 			"Message": app.i18n.T("csat.thankYouMessage"), | ||||||
| 		}, | 		}, | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										139
									
								
								cmd/custom_attributes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										139
									
								
								cmd/custom_attributes.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,139 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"slices" | ||||||
|  | 	"strconv" | ||||||
|  |  | ||||||
|  | 	cmodels "github.com/abhinavxd/libredesk/internal/custom_attribute/models" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	"github.com/valyala/fasthttp" | ||||||
|  | 	"github.com/zerodha/fastglue" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	// disallowedKeys contains keys that are not allowed for custom attributes as they're the default fields. | ||||||
|  | 	disallowedKeys = []string{ | ||||||
|  | 		"contact_email", | ||||||
|  | 		"content", | ||||||
|  | 		"subject", | ||||||
|  | 		"status", | ||||||
|  | 		"priority", | ||||||
|  | 		"assigned_team", | ||||||
|  | 		"assigned_user", | ||||||
|  | 		"hours_since_created", | ||||||
|  | 		"hours_since_first_reply", | ||||||
|  | 		"hours_since_last_reply", | ||||||
|  | 		"hours_since_resolved", | ||||||
|  | 		"inbox", | ||||||
|  | 	} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // handleGetCustomAttribute retrieves a custom attribute by its ID. | ||||||
|  | func handleGetCustomAttribute(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app = r.Context.(*App) | ||||||
|  | 	) | ||||||
|  | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	if err != nil || id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	attribute, err := app.customAttribute.Get(id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(attribute) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGetCustomAttributes retrieves all custom attributes from the database. | ||||||
|  | func handleGetCustomAttributes(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app       = r.Context.(*App) | ||||||
|  | 		appliesTo = string(r.RequestCtx.QueryArgs().Peek("applies_to")) | ||||||
|  | 	) | ||||||
|  | 	attributes, err := app.customAttribute.GetAll(appliesTo) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(attributes) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleCreateCustomAttribute creates a new custom attribute in the database. | ||||||
|  | func handleCreateCustomAttribute(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app       = r.Context.(*App) | ||||||
|  | 		attribute = cmodels.CustomAttribute{} | ||||||
|  | 	) | ||||||
|  | 	if err := r.Decode(&attribute, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	if err := validateCustomAttribute(app, attribute); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	createdAttr, err := app.customAttribute.Create(attribute) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(createdAttr) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleUpdateCustomAttribute updates an existing custom attribute in the database. | ||||||
|  | func handleUpdateCustomAttribute(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app       = r.Context.(*App) | ||||||
|  | 		attribute = cmodels.CustomAttribute{} | ||||||
|  | 	) | ||||||
|  | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	if err != nil || id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	if err := r.Decode(&attribute, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	if err := validateCustomAttribute(app, attribute); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	updatedAttr, err := app.customAttribute.Update(id, attribute) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(updatedAttr) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleDeleteCustomAttribute deletes a custom attribute from the database. | ||||||
|  | func handleDeleteCustomAttribute(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app = r.Context.(*App) | ||||||
|  | 	) | ||||||
|  | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	if err != nil || id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	if err = app.customAttribute.Delete(id); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // validateCustomAttribute validates a custom attribute. | ||||||
|  | func validateCustomAttribute(app *App, attribute cmodels.CustomAttribute) error { | ||||||
|  | 	if attribute.Name == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if attribute.AppliesTo == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`applies_to`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if attribute.DataType == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`type`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if attribute.Description == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`description`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if attribute.Key == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`key`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if slices.Contains(disallowedKeys, attribute.Key) { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.T("admin.customAttributes.keyNotAllowed"), nil) | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
							
								
								
									
										142
									
								
								cmd/handlers.go
									
									
									
									
									
								
							
							
						
						
									
										142
									
								
								cmd/handlers.go
									
									
									
									
									
								
							| @@ -12,33 +12,33 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var ( |  | ||||||
| 	slaReqFields = map[string][2]int{"name": {1, 255}, "description": {1, 255}, "first_response_time": {1, 255}, "resolution_time": {1, 255}} |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| // initHandlers initializes the HTTP routes and handlers for the application. | // initHandlers initializes the HTTP routes and handlers for the application. | ||||||
| func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | ||||||
| 	// Authentication. | 	// Authentication. | ||||||
| 	g.POST("/api/v1/login", handleLogin) | 	g.POST("/api/v1/auth/login", handleLogin) | ||||||
| 	g.GET("/logout", handleLogout) | 	g.GET("/logout", auth(handleLogout)) | ||||||
| 	g.GET("/api/v1/oidc/{id}/login", handleOIDCLogin) | 	g.GET("/api/v1/oidc/{id}/login", handleOIDCLogin) | ||||||
| 	g.GET("/api/v1/oidc/{id}/finish", handleOIDCCallback) | 	g.GET("/api/v1/oidc/{id}/finish", handleOIDCCallback) | ||||||
|  |  | ||||||
|  | 	// i18n. | ||||||
|  | 	g.GET("/api/v1/lang/{lang}", handleGetI18nLang) | ||||||
|  |  | ||||||
|  | 	// Public config for app initialization. | ||||||
|  | 	g.GET("/api/v1/config", handleGetConfig) | ||||||
|  |  | ||||||
| 	// Media. | 	// Media. | ||||||
| 	g.GET("/uploads/{uuid}", auth(handleServeMedia)) | 	g.GET("/uploads/{uuid}", auth(handleServeMedia)) | ||||||
| 	g.POST("/api/v1/media", auth(handleMediaUpload)) | 	g.POST("/api/v1/media", auth(handleMediaUpload)) | ||||||
|  |  | ||||||
| 	// Settings. | 	// Settings. | ||||||
| 	g.GET("/api/v1/settings/general", handleGetGeneralSettings) | 	g.GET("/api/v1/settings/general", auth(handleGetGeneralSettings)) | ||||||
| 	g.PUT("/api/v1/settings/general", perm(handleUpdateGeneralSettings, "general_settings:manage")) | 	g.PUT("/api/v1/settings/general", perm(handleUpdateGeneralSettings, "general_settings:manage")) | ||||||
| 	g.GET("/api/v1/settings/notifications/email", perm(handleGetEmailNotificationSettings, "notification_settings:manage")) | 	g.GET("/api/v1/settings/notifications/email", perm(handleGetEmailNotificationSettings, "notification_settings:manage")) | ||||||
| 	g.PUT("/api/v1/settings/notifications/email", perm(handleUpdateEmailNotificationSettings, "notification_settings:manage")) | 	g.PUT("/api/v1/settings/notifications/email", perm(handleUpdateEmailNotificationSettings, "notification_settings:manage")) | ||||||
|  |  | ||||||
| 	// OpenID connect single sign-on. | 	// OpenID connect single sign-on. | ||||||
| 	g.GET("/api/v1/oidc/enabled", handleGetAllEnabledOIDC) |  | ||||||
| 	g.GET("/api/v1/oidc", perm(handleGetAllOIDC, "oidc:manage")) | 	g.GET("/api/v1/oidc", perm(handleGetAllOIDC, "oidc:manage")) | ||||||
| 	g.POST("/api/v1/oidc", perm(handleCreateOIDC, "oidc:manage")) | 	g.POST("/api/v1/oidc", perm(handleCreateOIDC, "oidc:manage")) | ||||||
| 	g.POST("/api/v1/oidc/test", perm(handleTestOIDC, "oidc:manage")) |  | ||||||
| 	g.GET("/api/v1/oidc/{id}", perm(handleGetOIDC, "oidc:manage")) | 	g.GET("/api/v1/oidc/{id}", perm(handleGetOIDC, "oidc:manage")) | ||||||
| 	g.PUT("/api/v1/oidc/{id}", perm(handleUpdateOIDC, "oidc:manage")) | 	g.PUT("/api/v1/oidc/{id}", perm(handleUpdateOIDC, "oidc:manage")) | ||||||
| 	g.DELETE("/api/v1/oidc/{id}", perm(handleDeleteOIDC, "oidc:manage")) | 	g.DELETE("/api/v1/oidc/{id}", perm(handleDeleteOIDC, "oidc:manage")) | ||||||
| @@ -64,11 +64,13 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | |||||||
| 	g.POST("/api/v1/conversations/{cuuid}/messages", perm(handleSendMessage, "messages:write")) | 	g.POST("/api/v1/conversations/{cuuid}/messages", perm(handleSendMessage, "messages:write")) | ||||||
| 	g.PUT("/api/v1/conversations/{cuuid}/messages/{uuid}/retry", perm(handleRetryMessage, "messages:write")) | 	g.PUT("/api/v1/conversations/{cuuid}/messages/{uuid}/retry", perm(handleRetryMessage, "messages:write")) | ||||||
| 	g.POST("/api/v1/conversations", perm(handleCreateConversation, "conversations:write")) | 	g.POST("/api/v1/conversations", perm(handleCreateConversation, "conversations:write")) | ||||||
|  | 	g.PUT("/api/v1/conversations/{uuid}/custom-attributes", auth(handleUpdateConversationCustomAttributes)) | ||||||
|  | 	g.PUT("/api/v1/conversations/{uuid}/contacts/custom-attributes", auth(handleUpdateContactCustomAttributes)) | ||||||
|  |  | ||||||
| 	// Search. | 	// Search. | ||||||
| 	g.GET("/api/v1/conversations/search", perm(handleSearchConversations, "conversations:read")) | 	g.GET("/api/v1/conversations/search", perm(handleSearchConversations, "conversations:read")) | ||||||
| 	g.GET("/api/v1/messages/search", perm(handleSearchMessages, "messages:read")) | 	g.GET("/api/v1/messages/search", perm(handleSearchMessages, "messages:read")) | ||||||
| 	g.GET("/api/v1/contacts/search", perm(handleSearchContacts, "conversations:write")) | 	g.GET("/api/v1/contacts/search", perm(handleSearchContacts, "contacts:read")) | ||||||
|  |  | ||||||
| 	// Views. | 	// Views. | ||||||
| 	g.GET("/api/v1/views/me", perm(handleGetUserViews, "view:manage")) | 	g.GET("/api/v1/views/me", perm(handleGetUserViews, "view:manage")) | ||||||
| @@ -83,7 +85,7 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | |||||||
| 	g.DELETE("/api/v1/statuses/{id}", perm(handleDeleteStatus, "status:manage")) | 	g.DELETE("/api/v1/statuses/{id}", perm(handleDeleteStatus, "status:manage")) | ||||||
| 	g.GET("/api/v1/priorities", auth(handleGetPriorities)) | 	g.GET("/api/v1/priorities", auth(handleGetPriorities)) | ||||||
|  |  | ||||||
| 	// Tag. | 	// Tags. | ||||||
| 	g.GET("/api/v1/tags", auth(handleGetTags)) | 	g.GET("/api/v1/tags", auth(handleGetTags)) | ||||||
| 	g.POST("/api/v1/tags", perm(handleCreateTag, "tags:manage")) | 	g.POST("/api/v1/tags", perm(handleCreateTag, "tags:manage")) | ||||||
| 	g.PUT("/api/v1/tags/{id}", perm(handleUpdateTag, "tags:manage")) | 	g.PUT("/api/v1/tags/{id}", perm(handleUpdateTag, "tags:manage")) | ||||||
| @@ -97,22 +99,36 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | |||||||
| 	g.DELETE("/api/v1/macros/{id}", perm(handleDeleteMacro, "macros:manage")) | 	g.DELETE("/api/v1/macros/{id}", perm(handleDeleteMacro, "macros:manage")) | ||||||
| 	g.POST("/api/v1/conversations/{uuid}/macros/{id}/apply", auth(handleApplyMacro)) | 	g.POST("/api/v1/conversations/{uuid}/macros/{id}/apply", auth(handleApplyMacro)) | ||||||
|  |  | ||||||
| 	// User. | 	// Agents. | ||||||
| 	g.GET("/api/v1/users/me", auth(handleGetCurrentUser)) | 	g.GET("/api/v1/agents/me", auth(handleGetCurrentAgent)) | ||||||
| 	g.PUT("/api/v1/users/me", auth(handleUpdateCurrentUser)) | 	g.PUT("/api/v1/agents/me", auth(handleUpdateCurrentAgent)) | ||||||
| 	g.GET("/api/v1/users/me/teams", auth(handleGetCurrentUserTeams)) | 	g.GET("/api/v1/agents/me/teams", auth(handleGetCurrentAgentTeams)) | ||||||
| 	g.PUT("/api/v1/users/me/availability", auth(handleUpdateUserAvailability)) | 	g.PUT("/api/v1/agents/me/availability", auth(handleUpdateAgentAvailability)) | ||||||
| 	g.DELETE("/api/v1/users/me/avatar", auth(handleDeleteAvatar)) | 	g.DELETE("/api/v1/agents/me/avatar", auth(handleDeleteCurrentAgentAvatar)) | ||||||
| 	g.GET("/api/v1/users/compact", auth(handleGetUsersCompact)) |  | ||||||
| 	g.GET("/api/v1/users", perm(handleGetUsers, "users:manage")) |  | ||||||
| 	g.GET("/api/v1/users/{id}", perm(handleGetUser, "users:manage")) |  | ||||||
| 	g.POST("/api/v1/users", perm(handleCreateUser, "users:manage")) |  | ||||||
| 	g.PUT("/api/v1/users/{id}", perm(handleUpdateUser, "users:manage")) |  | ||||||
| 	g.DELETE("/api/v1/users/{id}", perm(handleDeleteUser, "users:manage")) |  | ||||||
| 	g.POST("/api/v1/users/reset-password", tryAuth(handleResetPassword)) |  | ||||||
| 	g.POST("/api/v1/users/set-password", tryAuth(handleSetPassword)) |  | ||||||
|  |  | ||||||
| 	// Team. | 	g.GET("/api/v1/agents/compact", auth(handleGetAgentsCompact)) | ||||||
|  | 	g.GET("/api/v1/agents", perm(handleGetAgents, "users:manage")) | ||||||
|  | 	g.GET("/api/v1/agents/{id}", perm(handleGetAgent, "users:manage")) | ||||||
|  | 	g.POST("/api/v1/agents", perm(handleCreateAgent, "users:manage")) | ||||||
|  | 	g.PUT("/api/v1/agents/{id}", perm(handleUpdateAgent, "users:manage")) | ||||||
|  | 	g.DELETE("/api/v1/agents/{id}", perm(handleDeleteAgent, "users:manage")) | ||||||
|  | 	g.POST("/api/v1/agents/{id}/api-key", perm(handleGenerateAPIKey, "users:manage")) | ||||||
|  | 	g.DELETE("/api/v1/agents/{id}/api-key", perm(handleRevokeAPIKey, "users:manage")) | ||||||
|  | 	g.POST("/api/v1/agents/reset-password", tryAuth(handleResetPassword)) | ||||||
|  | 	g.POST("/api/v1/agents/set-password", tryAuth(handleSetPassword)) | ||||||
|  |  | ||||||
|  | 	// Contacts. | ||||||
|  | 	g.GET("/api/v1/contacts", perm(handleGetContacts, "contacts:read_all")) | ||||||
|  | 	g.GET("/api/v1/contacts/{id}", perm(handleGetContact, "contacts:read")) | ||||||
|  | 	g.PUT("/api/v1/contacts/{id}", perm(handleUpdateContact, "contacts:write")) | ||||||
|  | 	g.PUT("/api/v1/contacts/{id}/block", perm(handleBlockContact, "contacts:block")) | ||||||
|  |  | ||||||
|  | 	// Contact notes. | ||||||
|  | 	g.GET("/api/v1/contacts/{id}/notes", perm(handleGetContactNotes, "contact_notes:read")) | ||||||
|  | 	g.POST("/api/v1/contacts/{id}/notes", perm(handleCreateContactNote, "contact_notes:write")) | ||||||
|  | 	g.DELETE("/api/v1/contacts/{id}/notes/{note_id}", perm(handleDeleteContactNote, "contact_notes:delete")) | ||||||
|  |  | ||||||
|  | 	// Teams. | ||||||
| 	g.GET("/api/v1/teams/compact", auth(handleGetTeamsCompact)) | 	g.GET("/api/v1/teams/compact", auth(handleGetTeamsCompact)) | ||||||
| 	g.GET("/api/v1/teams", perm(handleGetTeams, "teams:manage")) | 	g.GET("/api/v1/teams", perm(handleGetTeams, "teams:manage")) | ||||||
| 	g.GET("/api/v1/teams/{id}", perm(handleGetTeam, "teams:manage")) | 	g.GET("/api/v1/teams/{id}", perm(handleGetTeam, "teams:manage")) | ||||||
| @@ -120,20 +136,17 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | |||||||
| 	g.PUT("/api/v1/teams/{id}", perm(handleUpdateTeam, "teams:manage")) | 	g.PUT("/api/v1/teams/{id}", perm(handleUpdateTeam, "teams:manage")) | ||||||
| 	g.DELETE("/api/v1/teams/{id}", perm(handleDeleteTeam, "teams:manage")) | 	g.DELETE("/api/v1/teams/{id}", perm(handleDeleteTeam, "teams:manage")) | ||||||
|  |  | ||||||
| 	// i18n. | 	// Automations. | ||||||
| 	g.GET("/api/v1/lang/{lang}", handleGetI18nLang) | 	g.GET("/api/v1/automations/rules", perm(handleGetAutomationRules, "automations:manage")) | ||||||
|  | 	g.GET("/api/v1/automations/rules/{id}", perm(handleGetAutomationRule, "automations:manage")) | ||||||
|  | 	g.POST("/api/v1/automations/rules", perm(handleCreateAutomationRule, "automations:manage")) | ||||||
|  | 	g.PUT("/api/v1/automations/rules/{id}/toggle", perm(handleToggleAutomationRule, "automations:manage")) | ||||||
|  | 	g.PUT("/api/v1/automations/rules/{id}", perm(handleUpdateAutomationRule, "automations:manage")) | ||||||
|  | 	g.PUT("/api/v1/automations/rules/weights", perm(handleUpdateAutomationRuleWeights, "automations:manage")) | ||||||
|  | 	g.PUT("/api/v1/automations/rules/execution-mode", perm(handleUpdateAutomationRuleExecutionMode, "automations:manage")) | ||||||
|  | 	g.DELETE("/api/v1/automations/rules/{id}", perm(handleDeleteAutomationRule, "automations:manage")) | ||||||
|  |  | ||||||
| 	// Automation. | 	// Inboxes. | ||||||
| 	g.GET("/api/v1/automation/rules", perm(handleGetAutomationRules, "automations:manage")) |  | ||||||
| 	g.GET("/api/v1/automation/rules/{id}", perm(handleGetAutomationRule, "automations:manage")) |  | ||||||
| 	g.POST("/api/v1/automation/rules", perm(handleCreateAutomationRule, "automations:manage")) |  | ||||||
| 	g.PUT("/api/v1/automation/rules/{id}/toggle", perm(handleToggleAutomationRule, "automations:manage")) |  | ||||||
| 	g.PUT("/api/v1/automation/rules/{id}", perm(handleUpdateAutomationRule, "automations:manage")) |  | ||||||
| 	g.PUT("/api/v1/automation/rules/weights", perm(handleUpdateAutomationRuleWeights, "automations:manage")) |  | ||||||
| 	g.PUT("/api/v1/automation/rules/execution-mode", perm(handleUpdateAutomationRuleExecutionMode, "automations:manage")) |  | ||||||
| 	g.DELETE("/api/v1/automation/rules/{id}", perm(handleDeleteAutomationRule, "automations:manage")) |  | ||||||
|  |  | ||||||
| 	// Inbox. |  | ||||||
| 	g.GET("/api/v1/inboxes", auth(handleGetInboxes)) | 	g.GET("/api/v1/inboxes", auth(handleGetInboxes)) | ||||||
| 	g.GET("/api/v1/inboxes/{id}", perm(handleGetInbox, "inboxes:manage")) | 	g.GET("/api/v1/inboxes/{id}", perm(handleGetInbox, "inboxes:manage")) | ||||||
| 	g.POST("/api/v1/inboxes", perm(handleCreateInbox, "inboxes:manage")) | 	g.POST("/api/v1/inboxes", perm(handleCreateInbox, "inboxes:manage")) | ||||||
| @@ -141,18 +154,28 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | |||||||
| 	g.PUT("/api/v1/inboxes/{id}", perm(handleUpdateInbox, "inboxes:manage")) | 	g.PUT("/api/v1/inboxes/{id}", perm(handleUpdateInbox, "inboxes:manage")) | ||||||
| 	g.DELETE("/api/v1/inboxes/{id}", perm(handleDeleteInbox, "inboxes:manage")) | 	g.DELETE("/api/v1/inboxes/{id}", perm(handleDeleteInbox, "inboxes:manage")) | ||||||
|  |  | ||||||
| 	// Role. | 	// Roles. | ||||||
| 	g.GET("/api/v1/roles", perm(handleGetRoles, "roles:manage")) | 	g.GET("/api/v1/roles", auth(handleGetRoles)) | ||||||
| 	g.GET("/api/v1/roles/{id}", perm(handleGetRole, "roles:manage")) | 	g.GET("/api/v1/roles/{id}", perm(handleGetRole, "roles:manage")) | ||||||
| 	g.POST("/api/v1/roles", perm(handleCreateRole, "roles:manage")) | 	g.POST("/api/v1/roles", perm(handleCreateRole, "roles:manage")) | ||||||
| 	g.PUT("/api/v1/roles/{id}", perm(handleUpdateRole, "roles:manage")) | 	g.PUT("/api/v1/roles/{id}", perm(handleUpdateRole, "roles:manage")) | ||||||
| 	g.DELETE("/api/v1/roles/{id}", perm(handleDeleteRole, "roles:manage")) | 	g.DELETE("/api/v1/roles/{id}", perm(handleDeleteRole, "roles:manage")) | ||||||
|  |  | ||||||
| 	// Dashboard. | 	// Webhooks. | ||||||
| 	g.GET("/api/v1/reports/overview/counts", perm(handleDashboardCounts, "reports:manage")) | 	g.GET("/api/v1/webhooks", perm(handleGetWebhooks, "webhooks:manage")) | ||||||
| 	g.GET("/api/v1/reports/overview/charts", perm(handleDashboardCharts, "reports:manage")) | 	g.GET("/api/v1/webhooks/{id}", perm(handleGetWebhook, "webhooks:manage")) | ||||||
|  | 	g.POST("/api/v1/webhooks", perm(handleCreateWebhook, "webhooks:manage")) | ||||||
|  | 	g.PUT("/api/v1/webhooks/{id}", perm(handleUpdateWebhook, "webhooks:manage")) | ||||||
|  | 	g.DELETE("/api/v1/webhooks/{id}", perm(handleDeleteWebhook, "webhooks:manage")) | ||||||
|  | 	g.PUT("/api/v1/webhooks/{id}/toggle", perm(handleToggleWebhook, "webhooks:manage")) | ||||||
|  | 	g.POST("/api/v1/webhooks/{id}/test", perm(handleTestWebhook, "webhooks:manage")) | ||||||
|  |  | ||||||
| 	// Template. | 	// Reports. | ||||||
|  | 	g.GET("/api/v1/reports/overview/sla", perm(handleOverviewSLA, "reports:manage")) | ||||||
|  | 	g.GET("/api/v1/reports/overview/counts", perm(handleOverviewCounts, "reports:manage")) | ||||||
|  | 	g.GET("/api/v1/reports/overview/charts", perm(handleOverviewCharts, "reports:manage")) | ||||||
|  |  | ||||||
|  | 	// Templates. | ||||||
| 	g.GET("/api/v1/templates", perm(handleGetTemplates, "templates:manage")) | 	g.GET("/api/v1/templates", perm(handleGetTemplates, "templates:manage")) | ||||||
| 	g.GET("/api/v1/templates/{id}", perm(handleGetTemplate, "templates:manage")) | 	g.GET("/api/v1/templates/{id}", perm(handleGetTemplate, "templates:manage")) | ||||||
| 	g.POST("/api/v1/templates", perm(handleCreateTemplate, "templates:manage")) | 	g.POST("/api/v1/templates", perm(handleCreateTemplate, "templates:manage")) | ||||||
| @@ -160,24 +183,34 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | |||||||
| 	g.DELETE("/api/v1/templates/{id}", perm(handleDeleteTemplate, "templates:manage")) | 	g.DELETE("/api/v1/templates/{id}", perm(handleDeleteTemplate, "templates:manage")) | ||||||
|  |  | ||||||
| 	// Business hours. | 	// Business hours. | ||||||
| 	g.GET("/api/v1/business-hours", perm(handleGetBusinessHours, "business_hours:manage")) | 	g.GET("/api/v1/business-hours", auth(handleGetBusinessHours)) | ||||||
| 	g.GET("/api/v1/business-hours/{id}", perm(handleGetBusinessHour, "business_hours:manage")) | 	g.GET("/api/v1/business-hours/{id}", perm(handleGetBusinessHour, "business_hours:manage")) | ||||||
| 	g.POST("/api/v1/business-hours", perm(handleCreateBusinessHours, "business_hours:manage")) | 	g.POST("/api/v1/business-hours", perm(handleCreateBusinessHours, "business_hours:manage")) | ||||||
| 	g.PUT("/api/v1/business-hours/{id}", perm(handleUpdateBusinessHours, "business_hours:manage")) | 	g.PUT("/api/v1/business-hours/{id}", perm(handleUpdateBusinessHours, "business_hours:manage")) | ||||||
| 	g.DELETE("/api/v1/business-hours/{id}", perm(handleDeleteBusinessHour, "business_hours:manage")) | 	g.DELETE("/api/v1/business-hours/{id}", perm(handleDeleteBusinessHour, "business_hours:manage")) | ||||||
|  |  | ||||||
| 	// SLA. | 	// SLAs. | ||||||
| 	g.GET("/api/v1/sla", perm(handleGetSLAs, "sla:manage")) | 	g.GET("/api/v1/sla", perm(handleGetSLAs, "sla:manage")) | ||||||
| 	g.GET("/api/v1/sla/{id}", perm(handleGetSLA, "sla:manage")) | 	g.GET("/api/v1/sla/{id}", perm(handleGetSLA, "sla:manage")) | ||||||
| 	g.POST("/api/v1/sla", perm(fastglue.ReqLenRangeParams(handleCreateSLA, slaReqFields), "sla:manage")) | 	g.POST("/api/v1/sla", perm(handleCreateSLA, "sla:manage")) | ||||||
| 	g.PUT("/api/v1/sla/{id}", perm(fastglue.ReqLenRangeParams(handleUpdateSLA, slaReqFields), "sla:manage")) | 	g.PUT("/api/v1/sla/{id}", perm(handleUpdateSLA, "sla:manage")) | ||||||
| 	g.DELETE("/api/v1/sla/{id}", perm(handleDeleteSLA, "sla:manage")) | 	g.DELETE("/api/v1/sla/{id}", perm(handleDeleteSLA, "sla:manage")) | ||||||
|  |  | ||||||
| 	// AI completion. | 	// AI completions. | ||||||
| 	g.GET("/api/v1/ai/prompts", auth(handleGetAIPrompts)) | 	g.GET("/api/v1/ai/prompts", auth(handleGetAIPrompts)) | ||||||
| 	g.POST("/api/v1/ai/completion", auth(handleAICompletion)) | 	g.POST("/api/v1/ai/completion", auth(handleAICompletion)) | ||||||
| 	g.PUT("/api/v1/ai/provider", perm(handleUpdateAIProvider, "ai:manage")) | 	g.PUT("/api/v1/ai/provider", perm(handleUpdateAIProvider, "ai:manage")) | ||||||
|  |  | ||||||
|  | 	// Custom attributes. | ||||||
|  | 	g.GET("/api/v1/custom-attributes", auth(handleGetCustomAttributes)) | ||||||
|  | 	g.POST("/api/v1/custom-attributes", perm(handleCreateCustomAttribute, "custom_attributes:manage")) | ||||||
|  | 	g.GET("/api/v1/custom-attributes/{id}", perm(handleGetCustomAttribute, "custom_attributes:manage")) | ||||||
|  | 	g.PUT("/api/v1/custom-attributes/{id}", perm(handleUpdateCustomAttribute, "custom_attributes:manage")) | ||||||
|  | 	g.DELETE("/api/v1/custom-attributes/{id}", perm(handleDeleteCustomAttribute, "custom_attributes:manage")) | ||||||
|  |  | ||||||
|  | 	// Actvity logs. | ||||||
|  | 	g.GET("/api/v1/activity-logs", perm(handleGetActivityLogs, "activity_logs:manage")) | ||||||
|  |  | ||||||
| 	// WebSocket. | 	// WebSocket. | ||||||
| 	g.GET("/ws", auth(func(r *fastglue.Request) error { | 	g.GET("/ws", auth(func(r *fastglue.Request) error { | ||||||
| 		return handleWS(r, hub) | 		return handleWS(r, hub) | ||||||
| @@ -189,6 +222,7 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) { | |||||||
| 	g.GET("/teams/{all:*}", authPage(serveIndexPage)) | 	g.GET("/teams/{all:*}", authPage(serveIndexPage)) | ||||||
| 	g.GET("/views/{all:*}", authPage(serveIndexPage)) | 	g.GET("/views/{all:*}", authPage(serveIndexPage)) | ||||||
| 	g.GET("/admin/{all:*}", authPage(serveIndexPage)) | 	g.GET("/admin/{all:*}", authPage(serveIndexPage)) | ||||||
|  | 	g.GET("/contacts/{all:*}", authPage(serveIndexPage)) | ||||||
| 	g.GET("/reports/{all:*}", authPage(serveIndexPage)) | 	g.GET("/reports/{all:*}", authPage(serveIndexPage)) | ||||||
| 	g.GET("/account/{all:*}", authPage(serveIndexPage)) | 	g.GET("/account/{all:*}", authPage(serveIndexPage)) | ||||||
| 	g.GET("/reset-password", notAuthPage(serveIndexPage)) | 	g.GET("/reset-password", notAuthPage(serveIndexPage)) | ||||||
| @@ -218,7 +252,7 @@ func serveIndexPage(r *fastglue.Request) error { | |||||||
| 	// Serve the index.html file from the embedded filesystem. | 	// Serve the index.html file from the embedded filesystem. | ||||||
| 	file, err := app.fs.Get(path.Join(frontendDir, "index.html")) | 	file, err := app.fs.Get(path.Join(frontendDir, "index.html")) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(http.StatusNotFound, "Page not found", nil, envelope.NotFoundError) | 		return r.SendErrorEnvelope(http.StatusNotFound, app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.file}"), nil, envelope.NotFoundError) | ||||||
| 	} | 	} | ||||||
| 	r.RequestCtx.Response.Header.Set("Content-Type", "text/html") | 	r.RequestCtx.Response.Header.Set("Content-Type", "text/html") | ||||||
| 	r.RequestCtx.SetBody(file.ReadBytes()) | 	r.RequestCtx.SetBody(file.ReadBytes()) | ||||||
| @@ -226,7 +260,7 @@ func serveIndexPage(r *fastglue.Request) error { | |||||||
| 	// Set CSRF cookie if not already set. | 	// Set CSRF cookie if not already set. | ||||||
| 	if err := app.auth.SetCSRFCookie(r); err != nil { | 	if err := app.auth.SetCSRFCookie(r); err != nil { | ||||||
| 		app.lo.Error("error setting csrf cookie", "error", err) | 		app.lo.Error("error setting csrf cookie", "error", err) | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.T("user.errorAcquiringSession"), nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorSaving", "name", "{globals.terms.session}"), nil)) | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| @@ -240,7 +274,7 @@ func serveStaticFiles(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	file, err := app.fs.Get(filePath) | 	file, err := app.fs.Get(filePath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(http.StatusNotFound, "File not found", nil, envelope.NotFoundError) | 		return r.SendErrorEnvelope(http.StatusNotFound, app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.file}"), nil, envelope.NotFoundError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set the appropriate Content-Type based on the file extension. | 	// Set the appropriate Content-Type based on the file extension. | ||||||
| @@ -265,7 +299,7 @@ func serveFrontendStaticFiles(r *fastglue.Request) error { | |||||||
| 	finalPath := filepath.Join(frontendDir, filePath) | 	finalPath := filepath.Join(frontendDir, filePath) | ||||||
| 	file, err := app.fs.Get(finalPath) | 	file, err := app.fs.Get(finalPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(http.StatusNotFound, "File not found", nil, envelope.NotFoundError) | 		return r.SendErrorEnvelope(http.StatusNotFound, app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.file}"), nil, envelope.NotFoundError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set the appropriate Content-Type based on the file extension. | 	// Set the appropriate Content-Type based on the file extension. | ||||||
|   | |||||||
| @@ -27,6 +27,7 @@ func handleGetI18nLang(r *fastglue.Request) error { | |||||||
| 	return r.SendBytes(http.StatusOK, "application/json", i.JSON()) | 	return r.SendBytes(http.StatusOK, "application/json", i.JSON()) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // loadI18nLang loads the i18n language pack for the given language code. | ||||||
| func loadI18nLang(lang string, fs stuffbin.FileSystem) (*i18n.I18n, error) { | func loadI18nLang(lang string, fs stuffbin.FileSystem) (*i18n.I18n, error) { | ||||||
| 	// Helper function to read and initialize i18n language. | 	// Helper function to read and initialize i18n language. | ||||||
| 	readLang := func(lang string) ([]byte, error) { | 	readLang := func(lang string) ([]byte, error) { | ||||||
|   | |||||||
							
								
								
									
										107
									
								
								cmd/inboxes.go
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								cmd/inboxes.go
									
									
									
									
									
								
							| @@ -1,6 +1,7 @@ | |||||||
| package main | package main | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"net/mail" | ||||||
| 	"strconv" | 	"strconv" | ||||||
|  |  | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
| @@ -9,15 +10,23 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | // handleGetInboxes returns all inboxes | ||||||
| func handleGetInboxes(r *fastglue.Request) error { | func handleGetInboxes(r *fastglue.Request) error { | ||||||
| 	var app = r.Context.(*App) | 	var app = r.Context.(*App) | ||||||
| 	inboxes, err := app.inbox.GetAll() | 	inboxes, err := app.inbox.GetAll() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	for i := range inboxes { | ||||||
|  | 		if err := inboxes[i].ClearPasswords(); err != nil { | ||||||
|  | 			app.lo.Error("error clearing inbox passwords from response", "error", err) | ||||||
|  | 			return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.inbox}"), nil) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| 	return r.SendEnvelope(inboxes) | 	return r.SendEnvelope(inboxes) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleGetInbox returns an inbox by ID | ||||||
| func handleGetInbox(r *fastglue.Request) error { | func handleGetInbox(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| @@ -25,33 +34,45 @@ func handleGetInbox(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	inbox, err := app.inbox.GetDBRecord(id) | 	inbox, err := app.inbox.GetDBRecord(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error fetching inbox", nil, envelope.GeneralError) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if err := inbox.ClearPasswords(); err != nil { | 	if err := inbox.ClearPasswords(); err != nil { | ||||||
| 		app.lo.Error("error clearing out passwords", "error", err) | 		app.lo.Error("error clearing inbox passwords from response", "error", err) | ||||||
| 		return envelope.NewError(envelope.GeneralError, "Error fetching inbox", nil) | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.inbox}"), nil) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(inbox) | 	return r.SendEnvelope(inbox) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleCreateInbox creates a new inbox | ||||||
| func handleCreateInbox(r *fastglue.Request) error { | func handleCreateInbox(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		inb = imodels.Inbox{} | 		inbox = imodels.Inbox{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&inb, "json"); err != nil { | 	if err := r.Decode(&inbox, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	err := app.inbox.Create(inb) |  | ||||||
|  | 	createdInbox, err := app.inbox.Create(inbox) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := reloadInboxes(app); err != nil { | 	if err := validateInbox(app, createdInbox); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error reloading inboxes", nil, envelope.GeneralError) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	if err := reloadInboxes(app); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.couldNotReload", "name", "{globals.terms.inbox}"), nil, envelope.GeneralError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Clear passwords before returning. | ||||||
|  | 	if err := createdInbox.ClearPasswords(); err != nil { | ||||||
|  | 		app.lo.Error("error clearing inbox passwords from response", "error", err) | ||||||
|  | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.inbox}"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(createdInbox) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateInbox updates an inbox | // handleUpdateInbox updates an inbox | ||||||
| @@ -63,24 +84,36 @@ func handleUpdateInbox(r *fastglue.Request) error { | |||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | ||||||
| 			"Invalid inbox `id`.", nil, envelope.InputError) | 			app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&inbox, "json"); err != nil { | 	if err := r.Decode(&inbox, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	err = app.inbox.Update(id, inbox) |  | ||||||
|  | 	if err := validateInbox(app, inbox); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	updatedInbox, err := app.inbox.Update(id, inbox) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Could not update inbox.", nil, envelope.GeneralError) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := reloadInboxes(app); err != nil { | 	if err := reloadInboxes(app); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error reloading inboxes, Please restart the app if the issue persists", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.couldNotReload", "name", "{globals.terms.inbox}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(inbox) | 	// Clear passwords before returning. | ||||||
|  | 	if err := updatedInbox.ClearPasswords(); err != nil { | ||||||
|  | 		app.lo.Error("error clearing inbox passwords from response", "error", err) | ||||||
|  | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.inbox}"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(updatedInbox) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleToggleInbox toggles an inbox | ||||||
| func handleToggleInbox(r *fastglue.Request) error { | func handleToggleInbox(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| @@ -88,20 +121,28 @@ func handleToggleInbox(r *fastglue.Request) error { | |||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | ||||||
| 			"Invalid inbox `id`.", nil, envelope.InputError) | 			app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.inbox.Toggle(id); err != nil { | 	toggledInbox, err := app.inbox.Toggle(id) | ||||||
|  | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := reloadInboxes(app); err != nil { | 	if err := reloadInboxes(app); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error reloading inboxes", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.couldNotReload", "name", "{globals.terms.inbox}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	// Clear passwords before returning | ||||||
|  | 	if err := toggledInbox.ClearPasswords(); err != nil { | ||||||
|  | 		app.lo.Error("error clearing inbox passwords from response", "error", err) | ||||||
|  | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.inbox}"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(toggledInbox) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleDeleteInbox deletes an inbox | ||||||
| func handleDeleteInbox(r *fastglue.Request) error { | func handleDeleteInbox(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| @@ -109,12 +150,28 @@ func handleDeleteInbox(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	err := app.inbox.SoftDelete(id) | 	err := app.inbox.SoftDelete(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Could not update inbox.", nil, envelope.GeneralError) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := reloadInboxes(app); err != nil { | 	if err := reloadInboxes(app); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error reloading inboxes", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.couldNotReload", "name", "{globals.terms.inbox}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // validateInbox validates the inbox | ||||||
|  | func validateInbox(app *App, inbox imodels.Inbox) error { | ||||||
|  | 	// Validate from address. | ||||||
|  | 	if _, err := mail.ParseAddress(inbox.From); err != nil { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalidFromAddress"), nil) | ||||||
|  | 	} | ||||||
|  | 	if len(inbox.Config) == 0 { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "config"), nil) | ||||||
|  | 	} | ||||||
|  | 	if inbox.Name == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "name"), nil) | ||||||
|  | 	} | ||||||
|  | 	if inbox.Channel == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "channel"), nil) | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										223
									
								
								cmd/init.go
									
									
									
									
									
								
							
							
						
						
									
										223
									
								
								cmd/init.go
									
									
									
									
									
								
							| @@ -12,6 +12,7 @@ import ( | |||||||
|  |  | ||||||
| 	"html/template" | 	"html/template" | ||||||
|  |  | ||||||
|  | 	activitylog "github.com/abhinavxd/libredesk/internal/activity_log" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/ai" | 	"github.com/abhinavxd/libredesk/internal/ai" | ||||||
| 	auth_ "github.com/abhinavxd/libredesk/internal/auth" | 	auth_ "github.com/abhinavxd/libredesk/internal/auth" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/authz" | 	"github.com/abhinavxd/libredesk/internal/authz" | ||||||
| @@ -23,6 +24,7 @@ import ( | |||||||
| 	"github.com/abhinavxd/libredesk/internal/conversation/priority" | 	"github.com/abhinavxd/libredesk/internal/conversation/priority" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/conversation/status" | 	"github.com/abhinavxd/libredesk/internal/conversation/status" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/csat" | 	"github.com/abhinavxd/libredesk/internal/csat" | ||||||
|  | 	customAttribute "github.com/abhinavxd/libredesk/internal/custom_attribute" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/inbox" | 	"github.com/abhinavxd/libredesk/internal/inbox" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/inbox/channel/email" | 	"github.com/abhinavxd/libredesk/internal/inbox/channel/email" | ||||||
| 	imodels "github.com/abhinavxd/libredesk/internal/inbox/models" | 	imodels "github.com/abhinavxd/libredesk/internal/inbox/models" | ||||||
| @@ -33,6 +35,7 @@ import ( | |||||||
| 	notifier "github.com/abhinavxd/libredesk/internal/notification" | 	notifier "github.com/abhinavxd/libredesk/internal/notification" | ||||||
| 	emailnotifier "github.com/abhinavxd/libredesk/internal/notification/providers/email" | 	emailnotifier "github.com/abhinavxd/libredesk/internal/notification/providers/email" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/oidc" | 	"github.com/abhinavxd/libredesk/internal/oidc" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/report" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/role" | 	"github.com/abhinavxd/libredesk/internal/role" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/search" | 	"github.com/abhinavxd/libredesk/internal/search" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/setting" | 	"github.com/abhinavxd/libredesk/internal/setting" | ||||||
| @@ -42,6 +45,7 @@ import ( | |||||||
| 	tmpl "github.com/abhinavxd/libredesk/internal/template" | 	tmpl "github.com/abhinavxd/libredesk/internal/template" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/user" | 	"github.com/abhinavxd/libredesk/internal/user" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/view" | 	"github.com/abhinavxd/libredesk/internal/view" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/webhook" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/ws" | 	"github.com/abhinavxd/libredesk/internal/ws" | ||||||
| 	"github.com/jmoiron/sqlx" | 	"github.com/jmoiron/sqlx" | ||||||
| 	"github.com/knadh/go-i18n" | 	"github.com/knadh/go-i18n" | ||||||
| @@ -217,8 +221,9 @@ func initConversations( | |||||||
| 	csat *csat.Manager, | 	csat *csat.Manager, | ||||||
| 	automationEngine *automation.Engine, | 	automationEngine *automation.Engine, | ||||||
| 	template *tmpl.Manager, | 	template *tmpl.Manager, | ||||||
|  | 	webhook *webhook.Manager, | ||||||
| ) *conversation.Manager { | ) *conversation.Manager { | ||||||
| 	c, err := conversation.New(hub, i18n, notif, sla, status, priority, inboxStore, userStore, teamStore, mediaStore, settings, csat, automationEngine, template, conversation.Opts{ | 	c, err := conversation.New(hub, i18n, notif, sla, status, priority, inboxStore, userStore, teamStore, mediaStore, settings, csat, automationEngine, template, webhook, conversation.Opts{ | ||||||
| 		DB:                       db, | 		DB:                       db, | ||||||
| 		Lo:                       initLogger("conversation_manager"), | 		Lo:                       initLogger("conversation_manager"), | ||||||
| 		OutgoingMessageQueueSize: ko.MustInt("message.outgoing_queue_size"), | 		OutgoingMessageQueueSize: ko.MustInt("message.outgoing_queue_size"), | ||||||
| @@ -231,11 +236,12 @@ func initConversations( | |||||||
| } | } | ||||||
|  |  | ||||||
| // initTag inits tag manager. | // initTag inits tag manager. | ||||||
| func initTag(db *sqlx.DB) *tag.Manager { | func initTag(db *sqlx.DB, i18n *i18n.I18n) *tag.Manager { | ||||||
| 	var lo = initLogger("tag_manager") | 	var lo = initLogger("tag_manager") | ||||||
| 	mgr, err := tag.New(tag.Opts{ | 	mgr, err := tag.New(tag.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing tags: %v", err) | 		log.Fatalf("error initializing tags: %v", err) | ||||||
| @@ -244,11 +250,12 @@ func initTag(db *sqlx.DB) *tag.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initViews inits view manager. | // initViews inits view manager. | ||||||
| func initView(db *sqlx.DB) *view.Manager { | func initView(db *sqlx.DB, i18n *i18n.I18n) *view.Manager { | ||||||
| 	var lo = initLogger("view_manager") | 	var lo = initLogger("view_manager") | ||||||
| 	m, err := view.New(view.Opts{ | 	m, err := view.New(view.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing view manager: %v", err) | 		log.Fatalf("error initializing view manager: %v", err) | ||||||
| @@ -257,11 +264,12 @@ func initView(db *sqlx.DB) *view.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initMacro inits macro manager. | // initMacro inits macro manager. | ||||||
| func initMacro(db *sqlx.DB) *macro.Manager { | func initMacro(db *sqlx.DB, i18n *i18n.I18n) *macro.Manager { | ||||||
| 	var lo = initLogger("macro") | 	var lo = initLogger("macro") | ||||||
| 	m, err := macro.New(macro.Opts{ | 	m, err := macro.New(macro.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing macro manager: %v", err) | 		log.Fatalf("error initializing macro manager: %v", err) | ||||||
| @@ -270,11 +278,12 @@ func initMacro(db *sqlx.DB) *macro.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initBusinessHours inits business hours manager. | // initBusinessHours inits business hours manager. | ||||||
| func initBusinessHours(db *sqlx.DB) *businesshours.Manager { | func initBusinessHours(db *sqlx.DB, i18n *i18n.I18n) *businesshours.Manager { | ||||||
| 	var lo = initLogger("business-hours") | 	var lo = initLogger("business-hours") | ||||||
| 	m, err := businesshours.New(businesshours.Opts{ | 	m, err := businesshours.New(businesshours.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing business hours manager: %v", err) | 		log.Fatalf("error initializing business hours manager: %v", err) | ||||||
| @@ -283,12 +292,13 @@ func initBusinessHours(db *sqlx.DB) *businesshours.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initSLA inits SLA manager. | // initSLA inits SLA manager. | ||||||
| func initSLA(db *sqlx.DB, teamManager *team.Manager, settings *setting.Manager, businessHours *businesshours.Manager) *sla.Manager { | func initSLA(db *sqlx.DB, teamManager *team.Manager, settings *setting.Manager, businessHours *businesshours.Manager, notifier *notifier.Service, template *tmpl.Manager, userManager *user.Manager, i18n *i18n.I18n) *sla.Manager { | ||||||
| 	var lo = initLogger("sla") | 	var lo = initLogger("sla") | ||||||
| 	m, err := sla.New(sla.Opts{ | 	m, err := sla.New(sla.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
| 	}, teamManager, settings, businessHours) | 		I18n: i18n, | ||||||
|  | 	}, teamManager, settings, businessHours, notifier, template, userManager) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing SLA manager: %v", err) | 		log.Fatalf("error initializing SLA manager: %v", err) | ||||||
| 	} | 	} | ||||||
| @@ -296,11 +306,12 @@ func initSLA(db *sqlx.DB, teamManager *team.Manager, settings *setting.Manager, | |||||||
| } | } | ||||||
|  |  | ||||||
| // initCSAT inits CSAT manager. | // initCSAT inits CSAT manager. | ||||||
| func initCSAT(db *sqlx.DB) *csat.Manager { | func initCSAT(db *sqlx.DB, i18n *i18n.I18n) *csat.Manager { | ||||||
| 	var lo = initLogger("csat") | 	var lo = initLogger("csat") | ||||||
| 	m, err := csat.New(csat.Opts{ | 	m, err := csat.New(csat.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing CSAT manager: %v", err) | 		log.Fatalf("error initializing CSAT manager: %v", err) | ||||||
| @@ -314,10 +325,10 @@ func initWS(user *user.Manager) *ws.Hub { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initTemplates inits template manager. | // initTemplates inits template manager. | ||||||
| func initTemplate(db *sqlx.DB, fs stuffbin.FileSystem, consts *constants) *tmpl.Manager { | func initTemplate(db *sqlx.DB, fs stuffbin.FileSystem, consts *constants, i18n *i18n.I18n) *tmpl.Manager { | ||||||
| 	var ( | 	var ( | ||||||
| 		lo      = initLogger("template") | 		lo      = initLogger("template") | ||||||
| 		funcMap = getTmplFuncs(consts) | 		funcMap = getTmplFuncs(consts, i18n) | ||||||
| 	) | 	) | ||||||
| 	tpls, err := stuffbin.ParseTemplatesGlob(funcMap, fs, "/static/email-templates/*.html") | 	tpls, err := stuffbin.ParseTemplatesGlob(funcMap, fs, "/static/email-templates/*.html") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -327,7 +338,7 @@ func initTemplate(db *sqlx.DB, fs stuffbin.FileSystem, consts *constants) *tmpl. | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error parsing web templates: %v", err) | 		log.Fatalf("error parsing web templates: %v", err) | ||||||
| 	} | 	} | ||||||
| 	m, err := tmpl.New(lo, db, webTpls, tpls, funcMap) | 	m, err := tmpl.New(lo, db, webTpls, tpls, funcMap, i18n) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing template manager: %v", err) | 		log.Fatalf("error initializing template manager: %v", err) | ||||||
| 	} | 	} | ||||||
| @@ -335,7 +346,7 @@ func initTemplate(db *sqlx.DB, fs stuffbin.FileSystem, consts *constants) *tmpl. | |||||||
| } | } | ||||||
|  |  | ||||||
| // getTmplFuncs returns the template functions. | // getTmplFuncs returns the template functions. | ||||||
| func getTmplFuncs(consts *constants) template.FuncMap { | func getTmplFuncs(consts *constants, i18n *i18n.I18n) template.FuncMap { | ||||||
| 	return template.FuncMap{ | 	return template.FuncMap{ | ||||||
| 		"RootURL": func() string { | 		"RootURL": func() string { | ||||||
| 			return consts.AppBaseURL | 			return consts.AppBaseURL | ||||||
| @@ -355,6 +366,9 @@ func getTmplFuncs(consts *constants) template.FuncMap { | |||||||
| 		"SiteName": func() string { | 		"SiteName": func() string { | ||||||
| 			return consts.SiteName | 			return consts.SiteName | ||||||
| 		}, | 		}, | ||||||
|  | 		"L": func() interface{} { | ||||||
|  | 			return i18n | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -371,7 +385,10 @@ func reloadSettings(app *App) error { | |||||||
| 		app.lo.Error("error unmarshalling settings from DB", "error", err) | 		app.lo.Error("error unmarshalling settings from DB", "error", err) | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	if err := ko.Load(confmap.Provider(out, "."), nil); err != nil { | 	app.Lock() | ||||||
|  | 	err = ko.Load(confmap.Provider(out, "."), nil) | ||||||
|  | 	app.Unlock() | ||||||
|  | 	if err != nil { | ||||||
| 		app.lo.Error("error loading settings into koanf", "error", err) | 		app.lo.Error("error loading settings into koanf", "error", err) | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -383,7 +400,7 @@ func reloadSettings(app *App) error { | |||||||
| // reloadTemplates reloads the templates from the filesystem. | // reloadTemplates reloads the templates from the filesystem. | ||||||
| func reloadTemplates(app *App) error { | func reloadTemplates(app *App) error { | ||||||
| 	app.lo.Info("reloading templates") | 	app.lo.Info("reloading templates") | ||||||
| 	funcMap := getTmplFuncs(app.consts.Load().(*constants)) | 	funcMap := getTmplFuncs(app.consts.Load().(*constants), app.i18n) | ||||||
| 	tpls, err := stuffbin.ParseTemplatesGlob(funcMap, app.fs, "/static/email-templates/*.html") | 	tpls, err := stuffbin.ParseTemplatesGlob(funcMap, app.fs, "/static/email-templates/*.html") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error parsing email templates", "error", err) | 		app.lo.Error("error parsing email templates", "error", err) | ||||||
| @@ -398,11 +415,12 @@ func reloadTemplates(app *App) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initTeam inits team manager. | // initTeam inits team manager. | ||||||
| func initTeam(db *sqlx.DB) *team.Manager { | func initTeam(db *sqlx.DB, i18n *i18n.I18n) *team.Manager { | ||||||
| 	var lo = initLogger("team-manager") | 	var lo = initLogger("team-manager") | ||||||
| 	mgr, err := team.New(team.Opts{ | 	mgr, err := team.New(team.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing team manager: %v", err) | 		log.Fatalf("error initializing team manager: %v", err) | ||||||
| @@ -411,7 +429,7 @@ func initTeam(db *sqlx.DB) *team.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initMedia inits media manager. | // initMedia inits media manager. | ||||||
| func initMedia(db *sqlx.DB) *media.Manager { | func initMedia(db *sqlx.DB, i18n *i18n.I18n) *media.Manager { | ||||||
| 	var ( | 	var ( | ||||||
| 		store      media.Store | 		store      media.Store | ||||||
| 		err        error | 		err        error | ||||||
| @@ -452,6 +470,7 @@ func initMedia(db *sqlx.DB) *media.Manager { | |||||||
| 		Store: store, | 		Store: store, | ||||||
| 		Lo:    lo, | 		Lo:    lo, | ||||||
| 		DB:    db, | 		DB:    db, | ||||||
|  | 		I18n:  i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing media: %v", err) | 		log.Fatalf("error initializing media: %v", err) | ||||||
| @@ -460,9 +479,9 @@ func initMedia(db *sqlx.DB) *media.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initInbox initializes the inbox manager without registering inboxes. | // initInbox initializes the inbox manager without registering inboxes. | ||||||
| func initInbox(db *sqlx.DB) *inbox.Manager { | func initInbox(db *sqlx.DB, i18n *i18n.I18n) *inbox.Manager { | ||||||
| 	var lo = initLogger("inbox-manager") | 	var lo = initLogger("inbox-manager") | ||||||
| 	mgr, err := inbox.New(lo, db) | 	mgr, err := inbox.New(lo, db, i18n) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing inbox manager: %v", err) | 		log.Fatalf("error initializing inbox manager: %v", err) | ||||||
| 	} | 	} | ||||||
| @@ -470,11 +489,12 @@ func initInbox(db *sqlx.DB) *inbox.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initAutomationEngine initializes the automation engine. | // initAutomationEngine initializes the automation engine. | ||||||
| func initAutomationEngine(db *sqlx.DB) *automation.Engine { | func initAutomationEngine(db *sqlx.DB, i18n *i18n.I18n) *automation.Engine { | ||||||
| 	var lo = initLogger("automation_engine") | 	var lo = initLogger("automation_engine") | ||||||
| 	engine, err := automation.New(automation.Opts{ | 	engine, err := automation.New(automation.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing automation engine: %v", err) | 		log.Fatalf("error initializing automation engine: %v", err) | ||||||
| @@ -496,13 +516,13 @@ func initAutoAssigner(teamManager *team.Manager, userManager *user.Manager, conv | |||||||
| } | } | ||||||
|  |  | ||||||
| // initNotifier initializes the notifier service with available providers. | // initNotifier initializes the notifier service with available providers. | ||||||
| func initNotifier(userStore notifier.UserStore) *notifier.Service { | func initNotifier() *notifier.Service { | ||||||
| 	smtpCfg := email.SMTPConfig{} | 	smtpCfg := email.SMTPConfig{} | ||||||
| 	if err := ko.UnmarshalWithConf("notification.email", &smtpCfg, koanf.UnmarshalConf{Tag: "json"}); err != nil { | 	if err := ko.UnmarshalWithConf("notification.email", &smtpCfg, koanf.UnmarshalConf{Tag: "json"}); err != nil { | ||||||
| 		log.Fatalf("error unmarshalling email notification provider config: %v", err) | 		log.Fatalf("error unmarshalling email notification provider config: %v", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	emailNotifier, err := emailnotifier.New([]email.SMTPConfig{smtpCfg}, userStore, emailnotifier.Opts{ | 	emailNotifier, err := emailnotifier.New([]email.SMTPConfig{smtpCfg}, emailnotifier.Opts{ | ||||||
| 		Lo:        initLogger("email-notifier"), | 		Lo:        initLogger("email-notifier"), | ||||||
| 		FromEmail: ko.String("notification.email.email_address"), | 		FromEmail: ko.String("notification.email.email_address"), | ||||||
| 	}) | 	}) | ||||||
| @@ -518,7 +538,7 @@ func initNotifier(userStore notifier.UserStore) *notifier.Service { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initEmailInbox initializes the email inbox. | // initEmailInbox initializes the email inbox. | ||||||
| func initEmailInbox(inboxRecord imodels.Inbox, store inbox.MessageStore) (inbox.Inbox, error) { | func initEmailInbox(inboxRecord imodels.Inbox, msgStore inbox.MessageStore, usrStore inbox.UserStore) (inbox.Inbox, error) { | ||||||
| 	var config email.Config | 	var config email.Config | ||||||
|  |  | ||||||
| 	// Load JSON data into Koanf. | 	// Load JSON data into Koanf. | ||||||
| @@ -544,7 +564,7 @@ func initEmailInbox(inboxRecord imodels.Inbox, store inbox.MessageStore) (inbox. | |||||||
| 		log.Printf("WARNING: No `from` email address set for `%s` inbox: Name: `%s`", inboxRecord.Channel, inboxRecord.Name) | 		log.Printf("WARNING: No `from` email address set for `%s` inbox: Name: `%s`", inboxRecord.Channel, inboxRecord.Name) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	inbox, err := email.New(store, email.Opts{ | 	inbox, err := email.New(msgStore, usrStore, email.Opts{ | ||||||
| 		ID:     inboxRecord.ID, | 		ID:     inboxRecord.ID, | ||||||
| 		Config: config, | 		Config: config, | ||||||
| 		Lo:     initLogger("email_inbox"), | 		Lo:     initLogger("email_inbox"), | ||||||
| @@ -560,10 +580,10 @@ func initEmailInbox(inboxRecord imodels.Inbox, store inbox.MessageStore) (inbox. | |||||||
| } | } | ||||||
|  |  | ||||||
| // initializeInboxes handles inbox initialization. | // initializeInboxes handles inbox initialization. | ||||||
| func initializeInboxes(inboxR imodels.Inbox, store inbox.MessageStore) (inbox.Inbox, error) { | func initializeInboxes(inboxR imodels.Inbox, msgStore inbox.MessageStore, usrStore inbox.UserStore) (inbox.Inbox, error) { | ||||||
| 	switch inboxR.Channel { | 	switch inboxR.Channel { | ||||||
| 	case "email": | 	case "email": | ||||||
| 		return initEmailInbox(inboxR, store) | 		return initEmailInbox(inboxR, msgStore, usrStore) | ||||||
| 	default: | 	default: | ||||||
| 		return nil, fmt.Errorf("unknown inbox channel: %s", inboxR.Channel) | 		return nil, fmt.Errorf("unknown inbox channel: %s", inboxR.Channel) | ||||||
| 	} | 	} | ||||||
| @@ -576,8 +596,9 @@ func reloadInboxes(app *App) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| // startInboxes registers the active inboxes and starts receiver for each. | // startInboxes registers the active inboxes and starts receiver for each. | ||||||
| func startInboxes(ctx context.Context, mgr *inbox.Manager, store inbox.MessageStore) { | func startInboxes(ctx context.Context, mgr *inbox.Manager, msgStore inbox.MessageStore, usrStore inbox.UserStore) { | ||||||
| 	mgr.SetMessageStore(store) | 	mgr.SetMessageStore(msgStore) | ||||||
|  | 	mgr.SetUserStore(usrStore) | ||||||
|  |  | ||||||
| 	if err := mgr.InitInboxes(initializeInboxes); err != nil { | 	if err := mgr.InitInboxes(initializeInboxes); err != nil { | ||||||
| 		log.Fatalf("error initializing inboxes: %v", err) | 		log.Fatalf("error initializing inboxes: %v", err) | ||||||
| @@ -589,8 +610,8 @@ func startInboxes(ctx context.Context, mgr *inbox.Manager, store inbox.MessageSt | |||||||
| } | } | ||||||
|  |  | ||||||
| // initAuthz initializes authorization enforcer. | // initAuthz initializes authorization enforcer. | ||||||
| func initAuthz() *authz.Enforcer { | func initAuthz(i18n *i18n.I18n) *authz.Enforcer { | ||||||
| 	enforcer, err := authz.NewEnforcer(initLogger("authz")) | 	enforcer, err := authz.NewEnforcer(initLogger("authz"), i18n) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing authz: %v", err) | 		log.Fatalf("error initializing authz: %v", err) | ||||||
| 	} | 	} | ||||||
| @@ -598,7 +619,7 @@ func initAuthz() *authz.Enforcer { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initAuth initializes the authentication manager. | // initAuth initializes the authentication manager. | ||||||
| func initAuth(o *oidc.Manager, rd *redis.Client) *auth_.Auth { | func initAuth(o *oidc.Manager, rd *redis.Client, i18n *i18n.I18n) *auth_.Auth { | ||||||
| 	lo := initLogger("auth") | 	lo := initLogger("auth") | ||||||
|  |  | ||||||
| 	providers, err := buildProviders(o) | 	providers, err := buildProviders(o) | ||||||
| @@ -606,7 +627,8 @@ func initAuth(o *oidc.Manager, rd *redis.Client) *auth_.Auth { | |||||||
| 		log.Fatalf("error initializing auth: %v", err) | 		log.Fatalf("error initializing auth: %v", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	auth, err := auth_.New(auth_.Config{Providers: providers}, rd, lo) | 	secure := !ko.Bool("app.server.disable_secure_cookies") | ||||||
|  | 	auth, err := auth_.New(auth_.Config{Providers: providers, SecureCookies: secure}, i18n, rd, lo) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing auth: %v", err) | 		log.Fatalf("error initializing auth: %v", err) | ||||||
| 	} | 	} | ||||||
| @@ -653,11 +675,12 @@ func buildProviders(o *oidc.Manager) ([]auth_.Provider, error) { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initOIDC initializes open id connect config manager. | // initOIDC initializes open id connect config manager. | ||||||
| func initOIDC(db *sqlx.DB, settings *setting.Manager) *oidc.Manager { | func initOIDC(db *sqlx.DB, settings *setting.Manager, i18n *i18n.I18n) *oidc.Manager { | ||||||
| 	lo := initLogger("oidc") | 	lo := initLogger("oidc") | ||||||
| 	o, err := oidc.New(oidc.Opts{ | 	o, err := oidc.New(oidc.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}, settings) | 	}, settings) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing oidc: %v", err) | 		log.Fatalf("error initializing oidc: %v", err) | ||||||
| @@ -667,9 +690,11 @@ func initOIDC(db *sqlx.DB, settings *setting.Manager) *oidc.Manager { | |||||||
|  |  | ||||||
| // initI18n inits i18n. | // initI18n inits i18n. | ||||||
| func initI18n(fs stuffbin.FileSystem) *i18n.I18n { | func initI18n(fs stuffbin.FileSystem) *i18n.I18n { | ||||||
| 	file, err := fs.Get("i18n/" + cmp.Or(ko.String("app.lang"), defLang) + ".json") | 	fileName := cmp.Or(ko.String("app.lang"), defLang) | ||||||
|  | 	log.Printf("loading i18n language file: %s", fileName) | ||||||
|  | 	file, err := fs.Get("i18n/" + fileName + ".json") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error reading i18n language file") | 		log.Fatalf("error reading i18n language file `%s` : %v", fileName, err) | ||||||
| 	} | 	} | ||||||
| 	i18n, err := i18n.New(file.ReadBytes()) | 	i18n, err := i18n.New(file.ReadBytes()) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -713,11 +738,12 @@ func initDB() *sqlx.DB { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initRedis inits role manager. | // initRedis inits role manager. | ||||||
| func initRole(db *sqlx.DB) *role.Manager { | func initRole(db *sqlx.DB, i18n *i18n.I18n) *role.Manager { | ||||||
| 	var lo = initLogger("role_manager") | 	var lo = initLogger("role_manager") | ||||||
| 	r, err := role.New(role.Opts{ | 	r, err := role.New(role.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing role manager: %v", err) | 		log.Fatalf("error initializing role manager: %v", err) | ||||||
| @@ -726,10 +752,11 @@ func initRole(db *sqlx.DB) *role.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initStatus inits conversation status manager. | // initStatus inits conversation status manager. | ||||||
| func initStatus(db *sqlx.DB) *status.Manager { | func initStatus(db *sqlx.DB, i18n *i18n.I18n) *status.Manager { | ||||||
| 	manager, err := status.New(status.Opts{ | 	manager, err := status.New(status.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: initLogger("status-manager"), | 		Lo:   initLogger("status-manager"), | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing status manager: %v", err) | 		log.Fatalf("error initializing status manager: %v", err) | ||||||
| @@ -738,10 +765,11 @@ func initStatus(db *sqlx.DB) *status.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initPriority inits conversation priority manager. | // initPriority inits conversation priority manager. | ||||||
| func initPriority(db *sqlx.DB) *priority.Manager { | func initPriority(db *sqlx.DB, i18n *i18n.I18n) *priority.Manager { | ||||||
| 	manager, err := priority.New(priority.Opts{ | 	manager, err := priority.New(priority.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: initLogger("priority-manager"), | 		Lo:   initLogger("priority-manager"), | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing priority manager: %v", err) | 		log.Fatalf("error initializing priority manager: %v", err) | ||||||
| @@ -750,11 +778,12 @@ func initPriority(db *sqlx.DB) *priority.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initAI inits AI manager. | // initAI inits AI manager. | ||||||
| func initAI(db *sqlx.DB) *ai.Manager { | func initAI(db *sqlx.DB, i18n *i18n.I18n) *ai.Manager { | ||||||
| 	lo := initLogger("ai") | 	lo := initLogger("ai") | ||||||
| 	m, err := ai.New(ai.Opts{ | 	m, err := ai.New(ai.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing AI manager: %v", err) | 		log.Fatalf("error initializing AI manager: %v", err) | ||||||
| @@ -763,11 +792,12 @@ func initAI(db *sqlx.DB) *ai.Manager { | |||||||
| } | } | ||||||
|  |  | ||||||
| // initSearch inits search manager. | // initSearch inits search manager. | ||||||
| func initSearch(db *sqlx.DB) *search.Manager { | func initSearch(db *sqlx.DB, i18n *i18n.I18n) *search.Manager { | ||||||
| 	lo := initLogger("search") | 	lo := initLogger("search") | ||||||
| 	m, err := search.New(search.Opts{ | 	m, err := search.New(search.Opts{ | ||||||
| 		DB: db, | 		DB:   db, | ||||||
| 		Lo: lo, | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error initializing search manager: %v", err) | 		log.Fatalf("error initializing search manager: %v", err) | ||||||
| @@ -775,6 +805,65 @@ func initSearch(db *sqlx.DB) *search.Manager { | |||||||
| 	return m | 	return m | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // initCustomAttribute inits custom attribute manager. | ||||||
|  | func initCustomAttribute(db *sqlx.DB, i18n *i18n.I18n) *customAttribute.Manager { | ||||||
|  | 	lo := initLogger("custom-attribute") | ||||||
|  | 	m, err := customAttribute.New(customAttribute.Opts{ | ||||||
|  | 		DB:   db, | ||||||
|  | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatalf("error initializing custom attribute manager: %v", err) | ||||||
|  | 	} | ||||||
|  | 	return m | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // initActivityLog inits activity log manager. | ||||||
|  | func initActivityLog(db *sqlx.DB, i18n *i18n.I18n) *activitylog.Manager { | ||||||
|  | 	lo := initLogger("activity-log") | ||||||
|  | 	m, err := activitylog.New(activitylog.Opts{ | ||||||
|  | 		DB:   db, | ||||||
|  | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatalf("error initializing activity log manager: %v", err) | ||||||
|  | 	} | ||||||
|  | 	return m | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // initReport inits report manager. | ||||||
|  | func initReport(db *sqlx.DB, i18n *i18n.I18n) *report.Manager { | ||||||
|  | 	lo := initLogger("report") | ||||||
|  | 	m, err := report.New(report.Opts{ | ||||||
|  | 		DB:   db, | ||||||
|  | 		Lo:   lo, | ||||||
|  | 		I18n: i18n, | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatalf("error initializing report manager: %v", err) | ||||||
|  | 	} | ||||||
|  | 	return m | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // initWebhook inits webhook manager. | ||||||
|  | func initWebhook(db *sqlx.DB, i18n *i18n.I18n) *webhook.Manager { | ||||||
|  | 	var lo = initLogger("webhook") | ||||||
|  | 	m, err := webhook.New(webhook.Opts{ | ||||||
|  | 		DB:        db, | ||||||
|  | 		Lo:        lo, | ||||||
|  | 		I18n:      i18n, | ||||||
|  | 		Workers:   ko.MustInt("webhook.workers"), | ||||||
|  | 		QueueSize: ko.MustInt("webhook.queue_size"), | ||||||
|  | 		Timeout:   ko.MustDuration("webhook.timeout"), | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatalf("error initializing webhook manager: %v", err) | ||||||
|  | 	} | ||||||
|  | 	return m | ||||||
|  | } | ||||||
|  |  | ||||||
| // initLogger initializes a logf logger. | // initLogger initializes a logf logger. | ||||||
| func initLogger(src string) *logf.Logger { | func initLogger(src string) *logf.Logger { | ||||||
| 	lvl, env := ko.MustString("app.log_level"), ko.MustString("app.env") | 	lvl, env := ko.MustString("app.log_level"), ko.MustString("app.env") | ||||||
|   | |||||||
| @@ -25,8 +25,8 @@ func install(ctx context.Context, db *sqlx.DB, fs stuffbin.FileSystem, idempoten | |||||||
|  |  | ||||||
| 	// Make sure the system user password is strong enough. | 	// Make sure the system user password is strong enough. | ||||||
| 	password := os.Getenv("LIBREDESK_SYSTEM_USER_PASSWORD") | 	password := os.Getenv("LIBREDESK_SYSTEM_USER_PASSWORD") | ||||||
| 	if password != "" && !user.IsStrongSystemUserPassword(password) && !schemaInstalled { | 	if password != "" && !user.IsStrongPassword(password) && !schemaInstalled { | ||||||
| 		log.Fatalf("system user password is not strong, %s", user.SystemUserPasswordHint) | 		log.Fatalf("system user password is not strong, %s", user.PasswordHint) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if !idempotentInstall { | 	if !idempotentInstall { | ||||||
| @@ -49,11 +49,10 @@ func install(ctx context.Context, db *sqlx.DB, fs stuffbin.FileSystem, idempoten | |||||||
| 			os.Exit(0) | 			os.Exit(0) | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
|  | 		log.Println("installing database schema...") | ||||||
| 		time.Sleep(5 * time.Second) | 		time.Sleep(5 * time.Second) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	log.Println("installing database schema...") |  | ||||||
|  |  | ||||||
| 	// Install schema. | 	// Install schema. | ||||||
| 	if err := installSchema(db, fs); err != nil { | 	if err := installSchema(db, fs); err != nil { | ||||||
| 		log.Fatalf("error installing schema: %v", err) | 		log.Fatalf("error installing schema: %v", err) | ||||||
|   | |||||||
							
								
								
									
										63
									
								
								cmd/login.go
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								cmd/login.go
									
									
									
									
									
								
							| @@ -3,33 +3,44 @@ package main | |||||||
| import ( | import ( | ||||||
| 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
| 	umodels "github.com/abhinavxd/libredesk/internal/user/models" | 	realip "github.com/ferluci/fast-realip" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // handleLogin logs a user in. | type loginRequest struct { | ||||||
|  | 	Email    string `json:"email"` | ||||||
|  | 	Password string `json:"password"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleLogin logs in the user and returns the user. | ||||||
| func handleLogin(r *fastglue.Request) error { | func handleLogin(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app      = r.Context.(*App) | 		app      = r.Context.(*App) | ||||||
| 		email    = string(r.RequestCtx.PostArgs().Peek("email")) | 		ip       = realip.FromRequest(r.RequestCtx) | ||||||
| 		password = r.RequestCtx.PostArgs().Peek("password") | 		loginReq loginRequest | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.VerifyPassword(email, password) |  | ||||||
|  | 	// Decode JSON request. | ||||||
|  | 	if err := r.Decode(&loginReq, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if loginReq.Email == "" || loginReq.Password == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("globals.messages.badRequest"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Verify email and password. | ||||||
|  | 	user, err := app.user.VerifyPassword(loginReq.Email, []byte(loginReq.Password)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// Check if user is enabled. | ||||||
| 	if !user.Enabled { | 	if !user.Enabled { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "Your account is disabled, please contact administrator", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.T("user.accountDisabled"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set user availability status to online. |  | ||||||
| 	if err := app.user.UpdateAvailability(user.ID, umodels.Online); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
| 	user.AvailabilityStatus = umodels.Online |  | ||||||
|  |  | ||||||
| 	if err := app.auth.SaveSession(amodels.User{ | 	if err := app.auth.SaveSession(amodels.User{ | ||||||
| 		ID:        user.ID, | 		ID:        user.ID, | ||||||
| 		Email:     user.Email.String, | 		Email:     user.Email.String, | ||||||
| @@ -37,25 +48,43 @@ func handleLogin(r *fastglue.Request) error { | |||||||
| 		LastName:  user.LastName, | 		LastName:  user.LastName, | ||||||
| 	}, r); err != nil { | 	}, r); err != nil { | ||||||
| 		app.lo.Error("error saving session", "error", err) | 		app.lo.Error("error saving session", "error", err) | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.T("user.errorAcquiringSession"), nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorSaving", "name", "{globals.terms.session}"), nil)) | ||||||
| 	} | 	} | ||||||
| 	// Set CSRF cookie if not already set. | 	// Set CSRF cookie if not already set. | ||||||
| 	if err := app.auth.SetCSRFCookie(r); err != nil { | 	if err := app.auth.SetCSRFCookie(r); err != nil { | ||||||
| 		app.lo.Error("error setting csrf cookie", "error", err) | 		app.lo.Error("error setting csrf cookie", "error", err) | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.T("user.errorAcquiringSession"), nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorSaving", "name", "{globals.terms.session}"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// Update last login time. | ||||||
|  | 	if err := app.user.UpdateLastLoginAt(user.ID); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Insert activity log. | ||||||
|  | 	if err := app.activityLog.Login(user.ID, user.Email.String, ip); err != nil { | ||||||
|  | 		app.lo.Error("error creating login activity log", "error", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(user) | 	return r.SendEnvelope(user) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleLogout logs out the user and redirects to the dashboard. | // handleLogout logs out the user and redirects to the dashboard. | ||||||
| func handleLogout(r *fastglue.Request) error { | func handleLogout(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
|  | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		ip    = realip.FromRequest(r.RequestCtx) | ||||||
| 	) | 	) | ||||||
| 	if err := app.auth.DestroySession(r); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.T("user.errorAcquiringSession"), nil)) | 	// Insert activity log. | ||||||
|  | 	if err := app.activityLog.Logout(auser.ID, auser.Email, ip); err != nil { | ||||||
|  | 		app.lo.Error("error creating logout activity log", "error", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if err := app.auth.DestroySession(r); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorDestroying", "name", "{globals.terms.session}"), nil)) | ||||||
|  | 	} | ||||||
| 	// Add no-cache headers. | 	// Add no-cache headers. | ||||||
| 	r.RequestCtx.Response.Header.Add("Cache-Control", | 	r.RequestCtx.Response.Header.Add("Cache-Control", | ||||||
| 		"no-store, no-cache, must-revalidate, post-check=0, pre-check=0") | 		"no-store, no-cache, must-revalidate, post-check=0, pre-check=0") | ||||||
|   | |||||||
							
								
								
									
										69
									
								
								cmd/macro.go
									
									
									
									
									
								
							
							
						
						
									
										69
									
								
								cmd/macro.go
									
									
									
									
									
								
							| @@ -2,7 +2,6 @@ package main | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" |  | ||||||
| 	"slices" | 	"slices" | ||||||
| 	"strconv" | 	"strconv" | ||||||
|  |  | ||||||
| @@ -24,14 +23,14 @@ func handleGetMacros(r *fastglue.Request) error { | |||||||
| 	for i, m := range macros { | 	for i, m := range macros { | ||||||
| 		var actions []autoModels.RuleAction | 		var actions []autoModels.RuleAction | ||||||
| 		if err := json.Unmarshal(m.Actions, &actions); err != nil { | 		if err := json.Unmarshal(m.Actions, &actions); err != nil { | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error unmarshalling macro actions", nil, envelope.GeneralError) | 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.macroAction}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 		// Set display values for actions as the value field can contain DB IDs | 		// Set display values for actions as the value field can contain DB IDs | ||||||
| 		if err := setDisplayValues(app, actions); err != nil { | 		if err := setDisplayValues(app, actions); err != nil { | ||||||
| 			app.lo.Warn("error setting display values", "error", err) | 			app.lo.Warn("error setting display values", "error", err) | ||||||
| 		} | 		} | ||||||
| 		if macros[i].Actions, err = json.Marshal(actions); err != nil { | 		if macros[i].Actions, err = json.Marshal(actions); err != nil { | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "marshal failed", nil, envelope.GeneralError) | 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.macroAction}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(macros) | 	return r.SendEnvelope(macros) | ||||||
| @@ -44,8 +43,7 @@ func handleGetMacro(r *fastglue.Request) error { | |||||||
| 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	) | 	) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid macro `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	macro, err := app.macro.Get(id) | 	macro, err := app.macro.Get(id) | ||||||
| @@ -55,14 +53,14 @@ func handleGetMacro(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	var actions []autoModels.RuleAction | 	var actions []autoModels.RuleAction | ||||||
| 	if err := json.Unmarshal(macro.Actions, &actions); err != nil { | 	if err := json.Unmarshal(macro.Actions, &actions); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error unmarshalling macro actions", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.macroAction}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
| 	// Set display values for actions as the value field can contain DB IDs | 	// Set display values for actions as the value field can contain DB IDs | ||||||
| 	if err := setDisplayValues(app, actions); err != nil { | 	if err := setDisplayValues(app, actions); err != nil { | ||||||
| 		app.lo.Warn("error setting display values", "error", err) | 		app.lo.Warn("error setting display values", "error", err) | ||||||
| 	} | 	} | ||||||
| 	if macro.Actions, err = json.Marshal(actions); err != nil { | 	if macro.Actions, err = json.Marshal(actions); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "marshal failed", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.macroAction}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(macro) | 	return r.SendEnvelope(macro) | ||||||
| @@ -76,19 +74,19 @@ func handleCreateMacro(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	if err := r.Decode(¯o, "json"); err != nil { | 	if err := r.Decode(¯o, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := validateMacro(macro); err != nil { | 	if err := validateMacro(app, macro); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err := app.macro.Create(macro.Name, macro.MessageContent, macro.UserID, macro.TeamID, macro.Visibility, macro.Actions) | 	createdMacro, err := app.macro.Create(macro.Name, macro.MessageContent, macro.UserID, macro.TeamID, macro.Visibility, macro.VisibleWhen, macro.Actions) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(macro) | 	return r.SendEnvelope(createdMacro) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateMacro updates a macro. | // handleUpdateMacro updates a macro. | ||||||
| @@ -108,32 +106,29 @@ func handleUpdateMacro(r *fastglue.Request) error { | |||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := validateMacro(macro); err != nil { | 	if err := validateMacro(app, macro); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.macro.Update(id, macro.Name, macro.MessageContent, macro.UserID, macro.TeamID, macro.Visibility, macro.Actions); err != nil { | 	updatedMacro, err := app.macro.Update(id, macro.Name, macro.MessageContent, macro.UserID, macro.TeamID, macro.Visibility, macro.VisibleWhen, macro.Actions) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(macro) | 	return r.SendEnvelope(updatedMacro) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteMacro deletes macro. | // handleDeleteMacro deletes macro. | ||||||
| func handleDeleteMacro(r *fastglue.Request) error { | func handleDeleteMacro(r *fastglue.Request) error { | ||||||
| 	var app = r.Context.(*App) | 	var app = r.Context.(*App) | ||||||
|  |  | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid macro `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := app.macro.Delete(id); err != nil { | 	if err := app.macro.Delete(id); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
| 	return r.SendEnvelope("Macro deleted successfully") |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleApplyMacro applies macro actions to a conversation. | // handleApplyMacro applies macro actions to a conversation. | ||||||
| @@ -145,7 +140,7 @@ func handleApplyMacro(r *fastglue.Request) error { | |||||||
| 		id, _            = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 		id, _            = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 		incomingActions  = []autoModels.RuleAction{} | 		incomingActions  = []autoModels.RuleAction{} | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -156,7 +151,7 @@ func handleApplyMacro(r *fastglue.Request) error { | |||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if allowed, err := app.authz.EnforceConversationAccess(user, conversation); err != nil || !allowed { | 	if allowed, err := app.authz.EnforceConversationAccess(user, conversation); err != nil || !allowed { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.PermissionError, "Permission denied", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.PermissionError, app.i18n.Ts("globals.messages.denied", "name", "{globals.terms.permission}"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	macro, err := app.macro.Get(id) | 	macro, err := app.macro.Get(id) | ||||||
| @@ -167,7 +162,7 @@ func handleApplyMacro(r *fastglue.Request) error { | |||||||
| 	// Decode incoming actions. | 	// Decode incoming actions. | ||||||
| 	if err := r.Decode(&incomingActions, "json"); err != nil { | 	if err := r.Decode(&incomingActions, "json"); err != nil { | ||||||
| 		app.lo.Error("error unmashalling incoming actions", "error", err) | 		app.lo.Error("error unmashalling incoming actions", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Failed to decode incoming actions", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.macroAction}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Make sure no duplicate action types are present. | 	// Make sure no duplicate action types are present. | ||||||
| @@ -175,7 +170,7 @@ func handleApplyMacro(r *fastglue.Request) error { | |||||||
| 	for _, act := range incomingActions { | 	for _, act := range incomingActions { | ||||||
| 		if actionTypes[act.Type] { | 		if actionTypes[act.Type] { | ||||||
| 			app.lo.Warn("duplicate action types found in macro apply apply request", "action", act.Type, "user_id", user.ID) | 			app.lo.Warn("duplicate action types found in macro apply apply request", "action", act.Type, "user_id", user.ID) | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Duplicate actions are not allowed", nil, envelope.InputError) | 			return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("macro.duplicateActionsNotAllowed"), nil, envelope.InputError) | ||||||
| 		} | 		} | ||||||
| 		actionTypes[act.Type] = true | 		actionTypes[act.Type] = true | ||||||
| 	} | 	} | ||||||
| @@ -184,11 +179,11 @@ func handleApplyMacro(r *fastglue.Request) error { | |||||||
| 	for _, act := range incomingActions { | 	for _, act := range incomingActions { | ||||||
| 		if !isMacroActionAllowed(act.Type) { | 		if !isMacroActionAllowed(act.Type) { | ||||||
| 			app.lo.Warn("action not allowed in macro", "action", act.Type, "user_id", user.ID) | 			app.lo.Warn("action not allowed in macro", "action", act.Type, "user_id", user.ID) | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusForbidden, "Action not allowed in macro", nil, envelope.PermissionError) | 			return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.Ts("macro.actionNotAllowed", "name", act.Type), nil, envelope.PermissionError) | ||||||
| 		} | 		} | ||||||
| 		if !hasActionPermission(act.Type, user.Permissions) { | 		if !hasActionPermission(act.Type, user.Permissions) { | ||||||
| 			app.lo.Warn("no permission to execute macro action", "action", act.Type, "user_id", user.ID) | 			app.lo.Warn("no permission to execute macro action", "action", act.Type, "user_id", user.ID) | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusForbidden, "No permission to execute this macro", nil, envelope.PermissionError) | 			return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.T("macro.permissionDenied"), nil, envelope.PermissionError) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -201,7 +196,7 @@ func handleApplyMacro(r *fastglue.Request) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if successCount == 0 { | 	if successCount == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Failed to apply macro", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("macro.couldNotApply"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Increment usage count. | 	// Increment usage count. | ||||||
| @@ -209,12 +204,12 @@ func handleApplyMacro(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	if successCount < len(incomingActions) { | 	if successCount < len(incomingActions) { | ||||||
| 		return r.SendJSON(fasthttp.StatusMultiStatus, map[string]interface{}{ | 		return r.SendJSON(fasthttp.StatusMultiStatus, map[string]interface{}{ | ||||||
| 			"message": fmt.Sprintf("Macro executed with errors. %d actions succeeded out of %d", successCount, len(incomingActions)), | 			"message": app.i18n.T("macro.partiallyApplied"), | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendJSON(fasthttp.StatusOK, map[string]interface{}{ | 	return r.SendJSON(fasthttp.StatusOK, map[string]interface{}{ | ||||||
| 		"message": "Macro applied successfully", | 		"message": app.i18n.T("macro.applied"), | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -239,7 +234,7 @@ func setDisplayValues(app *App, actions []autoModels.RuleAction) error { | |||||||
| 			return t.Name, nil | 			return t.Name, nil | ||||||
| 		}, | 		}, | ||||||
| 		autoModels.ActionAssignUser: func(id int) (string, error) { | 		autoModels.ActionAssignUser: func(id int) (string, error) { | ||||||
| 			u, err := app.user.GetAgent(id) | 			u, err := app.user.GetAgent(id, "") | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				app.lo.Warn("user not found for macro action", "user_id", id) | 				app.lo.Warn("user not found for macro action", "user_id", id) | ||||||
| 				return "", err | 				return "", err | ||||||
| @@ -276,18 +271,22 @@ func setDisplayValues(app *App, actions []autoModels.RuleAction) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| // validateMacro validates an incoming macro. | // validateMacro validates an incoming macro. | ||||||
| func validateMacro(macro models.Macro) error { | func validateMacro(app *App, macro models.Macro) error { | ||||||
| 	if macro.Name == "" { | 	if macro.Name == "" { | ||||||
| 		return envelope.NewError(envelope.InputError, "Empty macro `name`", nil) | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if len(macro.VisibleWhen) == 0 { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`visible_when`"), nil) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	var act []autoModels.RuleAction | 	var act []autoModels.RuleAction | ||||||
| 	if err := json.Unmarshal(macro.Actions, &act); err != nil { | 	if err := json.Unmarshal(macro.Actions, &act); err != nil { | ||||||
| 		return envelope.NewError(envelope.InputError, "Could not parse macro actions", nil) | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.macroAction}"), nil) | ||||||
| 	} | 	} | ||||||
| 	for _, a := range act { | 	for _, a := range act { | ||||||
| 		if len(a.Value) == 0 { | 		if len(a.Value) == 0 { | ||||||
| 			return envelope.NewError(envelope.InputError, fmt.Sprintf("Empty value for action: %s", a.Type), nil) | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", a.Type), nil) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| @@ -298,7 +297,7 @@ func isMacroActionAllowed(action string) bool { | |||||||
| 	switch action { | 	switch action { | ||||||
| 	case autoModels.ActionSendPrivateNote, autoModels.ActionReply: | 	case autoModels.ActionSendPrivateNote, autoModels.ActionReply: | ||||||
| 		return false | 		return false | ||||||
| 	case autoModels.ActionAssignTeam, autoModels.ActionAssignUser, autoModels.ActionSetStatus, autoModels.ActionSetPriority, autoModels.ActionSetTags: | 	case autoModels.ActionAssignTeam, autoModels.ActionAssignUser, autoModels.ActionSetStatus, autoModels.ActionSetPriority, autoModels.ActionAddTags, autoModels.ActionSetTags, autoModels.ActionRemoveTags: | ||||||
| 		return true | 		return true | ||||||
| 	default: | 	default: | ||||||
| 		return false | 		return false | ||||||
|   | |||||||
							
								
								
									
										174
									
								
								cmd/main.go
									
									
									
									
									
								
							
							
						
						
									
										174
									
								
								cmd/main.go
									
									
									
									
									
								
							| @@ -11,14 +11,19 @@ import ( | |||||||
| 	"syscall" | 	"syscall" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
|  | 	_ "time/tzdata" | ||||||
|  |  | ||||||
|  | 	activitylog "github.com/abhinavxd/libredesk/internal/activity_log" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/ai" | 	"github.com/abhinavxd/libredesk/internal/ai" | ||||||
| 	auth_ "github.com/abhinavxd/libredesk/internal/auth" | 	auth_ "github.com/abhinavxd/libredesk/internal/auth" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/authz" | 	"github.com/abhinavxd/libredesk/internal/authz" | ||||||
| 	businesshours "github.com/abhinavxd/libredesk/internal/business_hours" | 	businesshours "github.com/abhinavxd/libredesk/internal/business_hours" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/colorlog" | 	"github.com/abhinavxd/libredesk/internal/colorlog" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/csat" | 	"github.com/abhinavxd/libredesk/internal/csat" | ||||||
|  | 	customAttribute "github.com/abhinavxd/libredesk/internal/custom_attribute" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/macro" | 	"github.com/abhinavxd/libredesk/internal/macro" | ||||||
| 	notifier "github.com/abhinavxd/libredesk/internal/notification" | 	notifier "github.com/abhinavxd/libredesk/internal/notification" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/report" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/search" | 	"github.com/abhinavxd/libredesk/internal/search" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/sla" | 	"github.com/abhinavxd/libredesk/internal/sla" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/view" | 	"github.com/abhinavxd/libredesk/internal/view" | ||||||
| @@ -36,6 +41,7 @@ import ( | |||||||
| 	"github.com/abhinavxd/libredesk/internal/team" | 	"github.com/abhinavxd/libredesk/internal/team" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/template" | 	"github.com/abhinavxd/libredesk/internal/template" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/user" | 	"github.com/abhinavxd/libredesk/internal/user" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/webhook" | ||||||
| 	"github.com/knadh/go-i18n" | 	"github.com/knadh/go-i18n" | ||||||
| 	"github.com/knadh/koanf/v2" | 	"github.com/knadh/koanf/v2" | ||||||
| 	"github.com/knadh/stuffbin" | 	"github.com/knadh/stuffbin" | ||||||
| @@ -57,36 +63,42 @@ var ( | |||||||
|  |  | ||||||
| // App is the global app context which is passed and injected in the http handlers. | // App is the global app context which is passed and injected in the http handlers. | ||||||
| type App struct { | type App struct { | ||||||
| 	fs            stuffbin.FileSystem | 	fs              stuffbin.FileSystem | ||||||
| 	consts        atomic.Value | 	consts          atomic.Value | ||||||
| 	auth          *auth_.Auth | 	auth            *auth_.Auth | ||||||
| 	authz         *authz.Enforcer | 	authz           *authz.Enforcer | ||||||
| 	i18n          *i18n.I18n | 	i18n            *i18n.I18n | ||||||
| 	lo            *logf.Logger | 	lo              *logf.Logger | ||||||
| 	oidc          *oidc.Manager | 	oidc            *oidc.Manager | ||||||
| 	media         *media.Manager | 	media           *media.Manager | ||||||
| 	setting       *setting.Manager | 	setting         *setting.Manager | ||||||
| 	role          *role.Manager | 	role            *role.Manager | ||||||
| 	user          *user.Manager | 	user            *user.Manager | ||||||
| 	team          *team.Manager | 	team            *team.Manager | ||||||
| 	status        *status.Manager | 	status          *status.Manager | ||||||
| 	priority      *priority.Manager | 	priority        *priority.Manager | ||||||
| 	tag           *tag.Manager | 	tag             *tag.Manager | ||||||
| 	inbox         *inbox.Manager | 	inbox           *inbox.Manager | ||||||
| 	tmpl          *template.Manager | 	tmpl            *template.Manager | ||||||
| 	macro         *macro.Manager | 	macro           *macro.Manager | ||||||
| 	conversation  *conversation.Manager | 	conversation    *conversation.Manager | ||||||
| 	automation    *automation.Engine | 	automation      *automation.Engine | ||||||
| 	businessHours *businesshours.Manager | 	businessHours   *businesshours.Manager | ||||||
| 	sla           *sla.Manager | 	sla             *sla.Manager | ||||||
| 	csat          *csat.Manager | 	csat            *csat.Manager | ||||||
| 	view          *view.Manager | 	view            *view.Manager | ||||||
| 	ai            *ai.Manager | 	ai              *ai.Manager | ||||||
| 	search        *search.Manager | 	search          *search.Manager | ||||||
| 	notifier      *notifier.Service | 	activityLog     *activitylog.Manager | ||||||
|  | 	notifier        *notifier.Service | ||||||
|  | 	customAttribute *customAttribute.Manager | ||||||
|  | 	report          *report.Manager | ||||||
|  | 	webhook         *webhook.Manager | ||||||
|  |  | ||||||
| 	// Global state that stores data on an available app update. | 	// Global state that stores data on an available app update. | ||||||
| 	update *AppUpdate | 	update *AppUpdate | ||||||
|  | 	// Flag to indicate if app restart is required for settings to take effect. | ||||||
|  | 	restartRequired bool | ||||||
| 	sync.Mutex | 	sync.Mutex | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -106,7 +118,6 @@ func main() { | |||||||
|  |  | ||||||
| 	// Build string injected at build time. | 	// Build string injected at build time. | ||||||
| 	colorlog.Green("Build: %s", buildString) | 	colorlog.Green("Build: %s", buildString) | ||||||
| 	colorlog.Green("Version: %s", versionString) |  | ||||||
|  |  | ||||||
| 	// Load the config files into Koanf. | 	// Load the config files into Koanf. | ||||||
| 	initConfig(ko) | 	initConfig(ko) | ||||||
| @@ -152,76 +163,93 @@ func main() { | |||||||
| 	settings := initSettings(db) | 	settings := initSettings(db) | ||||||
| 	loadSettings(settings) | 	loadSettings(settings) | ||||||
|  |  | ||||||
|  | 	// Fallback for config typo. Logs a warning but continues to work with the incorrect key. | ||||||
|  | 	// Uses 'message.message_outgoing_scan_interval' (correct key) as default key, falls back to the common typo. | ||||||
|  | 	msgOutgoingScanIntervalKey := "message.message_outgoing_scan_interval" | ||||||
|  | 	if ko.String(msgOutgoingScanIntervalKey) == "" { | ||||||
|  | 		if ko.String("message.message_outoing_scan_interval") != "" { | ||||||
|  | 			colorlog.Red("WARNING: typo in config key 'message.message_outoing_scan_interval' detected. Use 'message.message_outgoing_scan_interval' instead in your config.toml file. Support for this incorrect key will be removed in a future release.") | ||||||
|  | 			msgOutgoingScanIntervalKey = "message.message_outoing_scan_interval" | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	var ( | 	var ( | ||||||
| 		autoAssignInterval          = ko.MustDuration("autoassigner.autoassign_interval") | 		autoAssignInterval          = ko.MustDuration("autoassigner.autoassign_interval") | ||||||
| 		unsnoozeInterval            = ko.MustDuration("conversation.unsnooze_interval") | 		unsnoozeInterval            = ko.MustDuration("conversation.unsnooze_interval") | ||||||
| 		automationWorkers           = ko.MustInt("automation.worker_count") | 		automationWorkers           = ko.MustInt("automation.worker_count") | ||||||
| 		messageOutgoingQWorkers     = ko.MustDuration("message.outgoing_queue_workers") | 		messageOutgoingQWorkers     = ko.MustDuration("message.outgoing_queue_workers") | ||||||
| 		messageIncomingQWorkers     = ko.MustDuration("message.incoming_queue_workers") | 		messageIncomingQWorkers     = ko.MustDuration("message.incoming_queue_workers") | ||||||
| 		messageOutgoingScanInterval = ko.MustDuration("message.message_outoing_scan_interval") | 		messageOutgoingScanInterval = ko.MustDuration(msgOutgoingScanIntervalKey) | ||||||
| 		slaEvaluationInterval       = ko.MustDuration("sla.evaluation_interval") | 		slaEvaluationInterval       = ko.MustDuration("sla.evaluation_interval") | ||||||
| 		lo                          = initLogger(appName) | 		lo                          = initLogger(appName) | ||||||
| 		rdb                         = initRedis() | 		rdb                         = initRedis() | ||||||
| 		constants                   = initConstants() | 		constants                   = initConstants() | ||||||
| 		i18n                        = initI18n(fs) | 		i18n                        = initI18n(fs) | ||||||
| 		csat                        = initCSAT(db) | 		csat                        = initCSAT(db, i18n) | ||||||
| 		oidc                        = initOIDC(db, settings) | 		oidc                        = initOIDC(db, settings, i18n) | ||||||
| 		status                      = initStatus(db) | 		status                      = initStatus(db, i18n) | ||||||
| 		priority                    = initPriority(db) | 		priority                    = initPriority(db, i18n) | ||||||
| 		auth                        = initAuth(oidc, rdb) | 		auth                        = initAuth(oidc, rdb, i18n) | ||||||
| 		template                    = initTemplate(db, fs, constants) | 		template                    = initTemplate(db, fs, constants, i18n) | ||||||
| 		media                       = initMedia(db) | 		media                       = initMedia(db, i18n) | ||||||
| 		inbox                       = initInbox(db) | 		inbox                       = initInbox(db, i18n) | ||||||
| 		team                        = initTeam(db) | 		team                        = initTeam(db, i18n) | ||||||
| 		businessHours               = initBusinessHours(db) | 		businessHours               = initBusinessHours(db, i18n) | ||||||
|  | 		webhook                     = initWebhook(db, i18n) | ||||||
| 		user                        = initUser(i18n, db) | 		user                        = initUser(i18n, db) | ||||||
| 		wsHub                       = initWS(user) | 		wsHub                       = initWS(user) | ||||||
| 		notifier                    = initNotifier(user) | 		notifier                    = initNotifier() | ||||||
| 		automation                  = initAutomationEngine(db) | 		automation                  = initAutomationEngine(db, i18n) | ||||||
| 		sla                         = initSLA(db, team, settings, businessHours) | 		sla                         = initSLA(db, team, settings, businessHours, notifier, template, user, i18n) | ||||||
| 		conversation                = initConversations(i18n, sla, status, priority, wsHub, notifier, db, inbox, user, team, media, settings, csat, automation, template) | 		conversation                = initConversations(i18n, sla, status, priority, wsHub, notifier, db, inbox, user, team, media, settings, csat, automation, template, webhook) | ||||||
| 		autoassigner                = initAutoAssigner(team, user, conversation) | 		autoassigner                = initAutoAssigner(team, user, conversation) | ||||||
| 	) | 	) | ||||||
| 	automation.SetConversationStore(conversation) | 	automation.SetConversationStore(conversation) | ||||||
|  |  | ||||||
| 	startInboxes(ctx, inbox, conversation) | 	startInboxes(ctx, inbox, conversation, user) | ||||||
| 	go automation.Run(ctx, automationWorkers) | 	go automation.Run(ctx, automationWorkers) | ||||||
| 	go autoassigner.Run(ctx, autoAssignInterval) | 	go autoassigner.Run(ctx, autoAssignInterval) | ||||||
| 	go conversation.Run(ctx, messageIncomingQWorkers, messageOutgoingQWorkers, messageOutgoingScanInterval) | 	go conversation.Run(ctx, messageIncomingQWorkers, messageOutgoingQWorkers, messageOutgoingScanInterval) | ||||||
| 	go conversation.RunUnsnoozer(ctx, unsnoozeInterval) | 	go conversation.RunUnsnoozer(ctx, unsnoozeInterval) | ||||||
|  | 	go webhook.Run(ctx) | ||||||
| 	go notifier.Run(ctx) | 	go notifier.Run(ctx) | ||||||
| 	go sla.Run(ctx, slaEvaluationInterval) | 	go sla.Run(ctx, slaEvaluationInterval) | ||||||
|  | 	go sla.SendNotifications(ctx) | ||||||
| 	go media.DeleteUnlinkedMedia(ctx) | 	go media.DeleteUnlinkedMedia(ctx) | ||||||
| 	go user.MonitorAgentAvailability(ctx) | 	go user.MonitorAgentAvailability(ctx) | ||||||
|  |  | ||||||
| 	var app = &App{ | 	var app = &App{ | ||||||
| 		lo:            lo, | 		lo:              lo, | ||||||
| 		fs:            fs, | 		fs:              fs, | ||||||
| 		sla:           sla, | 		sla:             sla, | ||||||
| 		oidc:          oidc, | 		oidc:            oidc, | ||||||
| 		i18n:          i18n, | 		i18n:            i18n, | ||||||
| 		auth:          auth, | 		auth:            auth, | ||||||
| 		media:         media, | 		media:           media, | ||||||
| 		setting:       settings, | 		setting:         settings, | ||||||
| 		inbox:         inbox, | 		inbox:           inbox, | ||||||
| 		user:          user, | 		user:            user, | ||||||
| 		team:          team, | 		team:            team, | ||||||
| 		status:        status, | 		status:          status, | ||||||
| 		priority:      priority, | 		priority:        priority, | ||||||
| 		tmpl:          template, | 		tmpl:            template, | ||||||
| 		notifier:      notifier, | 		notifier:        notifier, | ||||||
| 		consts:        atomic.Value{}, | 		consts:          atomic.Value{}, | ||||||
| 		conversation:  conversation, | 		conversation:    conversation, | ||||||
| 		automation:    automation, | 		automation:      automation, | ||||||
| 		businessHours: businessHours, | 		businessHours:   businessHours, | ||||||
| 		authz:         initAuthz(), | 		activityLog:     initActivityLog(db, i18n), | ||||||
| 		view:          initView(db), | 		customAttribute: initCustomAttribute(db, i18n), | ||||||
| 		csat:          initCSAT(db), | 		authz:           initAuthz(i18n), | ||||||
| 		search:        initSearch(db), | 		view:            initView(db, i18n), | ||||||
| 		role:          initRole(db), | 		report:          initReport(db, i18n), | ||||||
| 		tag:           initTag(db), | 		csat:            initCSAT(db, i18n), | ||||||
| 		macro:         initMacro(db), | 		search:          initSearch(db, i18n), | ||||||
| 		ai:            initAI(db), | 		role:            initRole(db, i18n), | ||||||
|  | 		tag:             initTag(db, i18n), | ||||||
|  | 		macro:           initMacro(db, i18n), | ||||||
|  | 		ai:              initAI(db, i18n), | ||||||
|  | 		webhook:         webhook, | ||||||
| 	} | 	} | ||||||
| 	app.consts.Store(constants) | 	app.consts.Store(constants) | ||||||
|  |  | ||||||
| @@ -235,7 +263,7 @@ func main() { | |||||||
| 		WriteTimeout:         ko.MustDuration("app.server.write_timeout"), | 		WriteTimeout:         ko.MustDuration("app.server.write_timeout"), | ||||||
| 		MaxRequestBodySize:   ko.MustInt("app.server.max_body_size"), | 		MaxRequestBodySize:   ko.MustInt("app.server.max_body_size"), | ||||||
| 		MaxKeepaliveDuration: ko.MustDuration("app.server.keepalive_timeout"), | 		MaxKeepaliveDuration: ko.MustDuration("app.server.keepalive_timeout"), | ||||||
| 		ReadBufferSize:       ko.MustInt("app.server.max_body_size"), | 		ReadBufferSize:       ko.Int("app.server.read_buffer_size"), | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	go func() { | 	go func() { | ||||||
| @@ -265,6 +293,8 @@ func main() { | |||||||
| 	autoassigner.Close() | 	autoassigner.Close() | ||||||
| 	colorlog.Red("Shutting down notifier...") | 	colorlog.Red("Shutting down notifier...") | ||||||
| 	notifier.Close() | 	notifier.Close() | ||||||
|  | 	colorlog.Red("Shutting down webhook...") | ||||||
|  | 	webhook.Close() | ||||||
| 	colorlog.Red("Shutting down conversation...") | 	colorlog.Red("Shutting down conversation...") | ||||||
| 	conversation.Close() | 	conversation.Close() | ||||||
| 	colorlog.Red("Shutting down SLA...") | 	colorlog.Red("Shutting down SLA...") | ||||||
|   | |||||||
							
								
								
									
										30
									
								
								cmd/media.go
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								cmd/media.go
									
									
									
									
									
								
							| @@ -24,6 +24,7 @@ const ( | |||||||
| 	thumbPrefix = "thumb_" | 	thumbPrefix = "thumb_" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | // handleMediaUpload handles media uploads. | ||||||
| func handleMediaUpload(r *fastglue.Request) error { | func handleMediaUpload(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app     = r.Context.(*App) | 		app     = r.Context.(*App) | ||||||
| @@ -33,19 +34,19 @@ func handleMediaUpload(r *fastglue.Request) error { | |||||||
| 	form, err := r.RequestCtx.MultipartForm() | 	form, err := r.RequestCtx.MultipartForm() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error parsing form data.", "error", err) | 		app.lo.Error("error parsing form data.", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error parsing data", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	files, ok := form.File["files"] | 	files, ok := form.File["files"] | ||||||
| 	if !ok || len(files) == 0 { | 	if !ok || len(files) == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "File not found", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.file}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	fileHeader := files[0] | 	fileHeader := files[0] | ||||||
| 	file, err := fileHeader.Open() | 	file, err := fileHeader.Open() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error reading uploaded file", "error", err) | 		app.lo.Error("error reading uploaded file", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error reading file", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorReading", "name", "{globals.terms.file}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
| 	defer file.Close() | 	defer file.Close() | ||||||
|  |  | ||||||
| @@ -74,15 +75,15 @@ func handleMediaUpload(r *fastglue.Request) error { | |||||||
| 	if bytesToMegabytes(srcFileSize) > float64(consts.MaxFileUploadSizeMB) { | 	if bytesToMegabytes(srcFileSize) > float64(consts.MaxFileUploadSizeMB) { | ||||||
| 		app.lo.Error("error: uploaded file size is larger than max allowed", "size", bytesToMegabytes(srcFileSize), "max_allowed", consts.MaxFileUploadSizeMB) | 		app.lo.Error("error: uploaded file size is larger than max allowed", "size", bytesToMegabytes(srcFileSize), "max_allowed", consts.MaxFileUploadSizeMB) | ||||||
| 		return r.SendErrorEnvelope( | 		return r.SendErrorEnvelope( | ||||||
| 			http.StatusRequestEntityTooLarge, | 			fasthttp.StatusRequestEntityTooLarge, | ||||||
| 			fmt.Sprintf("File size is too large. Please upload file lesser than %d MB", consts.MaxFileUploadSizeMB), | 			app.i18n.Ts("media.fileSizeTooLarge", "size", fmt.Sprintf("%dMB", consts.MaxFileUploadSizeMB)), | ||||||
| 			nil, | 			nil, | ||||||
| 			envelope.GeneralError, | 			envelope.GeneralError, | ||||||
| 		) | 		) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if !slices.Contains(consts.AllowedUploadFileExtensions, "*") && !slices.Contains(consts.AllowedUploadFileExtensions, srcExt) { | 	if !slices.Contains(consts.AllowedUploadFileExtensions, "*") && !slices.Contains(consts.AllowedUploadFileExtensions, srcExt) { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "File type not allowed", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("media.fileTypeNotAllowed"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Delete files on any error. | 	// Delete files on any error. | ||||||
| @@ -102,11 +103,10 @@ func handleMediaUpload(r *fastglue.Request) error { | |||||||
| 		thumbFile, err := image.CreateThumb(image.DefThumbSize, file) | 		thumbFile, err := image.CreateThumb(image.DefThumbSize, file) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			app.lo.Error("error creating thumb image", "error", err) | 			app.lo.Error("error creating thumb image", "error", err) | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error creating image thumbnail", nil, envelope.GeneralError) | 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorCreating", "name", "{globals.terms.thumbnail}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 		thumbName, err = app.media.Upload(thumbName, srcContentType, thumbFile) | 		thumbName, err = app.media.Upload(thumbName, srcContentType, thumbFile) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			app.lo.Error("error uploading thumbnail", "error", err) |  | ||||||
| 			return sendErrorEnvelope(r, err) | 			return sendErrorEnvelope(r, err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| @@ -116,7 +116,7 @@ func handleMediaUpload(r *fastglue.Request) error { | |||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			cleanUp = true | 			cleanUp = true | ||||||
| 			app.lo.Error("error getting image dimensions", "error", err) | 			app.lo.Error("error getting image dimensions", "error", err) | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error uploading file", nil, envelope.GeneralError) | 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorUploading", "name", "{globals.terms.media}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 		meta, _ = json.Marshal(map[string]interface{}{ | 		meta, _ = json.Marshal(map[string]interface{}{ | ||||||
| 			"width":  width, | 			"width":  width, | ||||||
| @@ -129,7 +129,7 @@ func handleMediaUpload(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		cleanUp = true | 		cleanUp = true | ||||||
| 		app.lo.Error("error uploading file", "error", err) | 		app.lo.Error("error uploading file", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error uploading file", nil, envelope.GeneralError) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Insert in DB. | 	// Insert in DB. | ||||||
| @@ -137,7 +137,7 @@ func handleMediaUpload(r *fastglue.Request) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		cleanUp = true | 		cleanUp = true | ||||||
| 		app.lo.Error("error inserting metadata into database", "error", err) | 		app.lo.Error("error inserting metadata into database", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error inserting media", nil, envelope.GeneralError) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(media) | 	return r.SendEnvelope(media) | ||||||
| } | } | ||||||
| @@ -150,13 +150,13 @@ func handleServeMedia(r *fastglue.Request) error { | |||||||
| 		uuid  = r.RequestCtx.UserValue("uuid").(string) | 		uuid  = r.RequestCtx.UserValue("uuid").(string) | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Fetch media from DB. | 	// Fetch media from DB. | ||||||
| 	media, err := app.media.GetByUUID(strings.TrimPrefix(uuid, thumbPrefix)) | 	media, err := app.media.Get(0, strings.TrimPrefix(uuid, thumbPrefix)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -164,7 +164,6 @@ func handleServeMedia(r *fastglue.Request) error { | |||||||
| 	// Check if the user has permission to access the linked model. | 	// Check if the user has permission to access the linked model. | ||||||
| 	allowed, err := app.authz.EnforceMediaAccess(user, media.Model.String) | 	allowed, err := app.authz.EnforceMediaAccess(user, media.Model.String) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error checking media permission", "error", err, "model", media.Model.String, "model_id", media.ModelID) |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -181,7 +180,7 @@ func handleServeMedia(r *fastglue.Request) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if !allowed { | 	if !allowed { | ||||||
| 		return r.SendErrorEnvelope(http.StatusUnauthorized, "Permission denied", nil, envelope.PermissionError) | 		return r.SendErrorEnvelope(http.StatusUnauthorized, app.i18n.Ts("globals.messages.denied", "name", "{globals.terms.permission}"), nil, envelope.UnauthorizedError) | ||||||
| 	} | 	} | ||||||
| 	consts := app.consts.Load().(*constants) | 	consts := app.consts.Load().(*constants) | ||||||
| 	switch consts.UploadProvider { | 	switch consts.UploadProvider { | ||||||
| @@ -193,6 +192,7 @@ func handleServeMedia(r *fastglue.Request) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // bytesToMegabytes converts bytes to megabytes. | ||||||
| func bytesToMegabytes(bytes int64) float64 { | func bytesToMegabytes(bytes int64) float64 { | ||||||
| 	return float64(bytes) / 1024 / 1024 | 	return float64(bytes) / 1024 / 1024 | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,11 +2,14 @@ package main | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"strconv" | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
| 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/automation/models" | 	authzModels "github.com/abhinavxd/libredesk/internal/authz/models" | ||||||
|  | 	cmodels "github.com/abhinavxd/libredesk/internal/conversation/models" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
| 	medModels "github.com/abhinavxd/libredesk/internal/media/models" | 	medModels "github.com/abhinavxd/libredesk/internal/media/models" | ||||||
|  | 	umodels "github.com/abhinavxd/libredesk/internal/user/models" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
| @@ -15,8 +18,10 @@ type messageReq struct { | |||||||
| 	Attachments []int    `json:"attachments"` | 	Attachments []int    `json:"attachments"` | ||||||
| 	Message     string   `json:"message"` | 	Message     string   `json:"message"` | ||||||
| 	Private     bool     `json:"private"` | 	Private     bool     `json:"private"` | ||||||
|  | 	To          []string `json:"to"` | ||||||
| 	CC          []string `json:"cc"` | 	CC          []string `json:"cc"` | ||||||
| 	BCC         []string `json:"bcc"` | 	BCC         []string `json:"bcc"` | ||||||
|  | 	SenderType  string   `json:"sender_type"` | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetMessages returns messages for a conversation. | // handleGetMessages returns messages for a conversation. | ||||||
| @@ -30,7 +35,7 @@ func handleGetMessages(r *fastglue.Request) error { | |||||||
| 		total       = 0 | 		total       = 0 | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -48,11 +53,14 @@ func handleGetMessages(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	for i := range messages { | 	for i := range messages { | ||||||
| 		total = messages[i].Total | 		total = messages[i].Total | ||||||
|  | 		// Populate attachment URLs | ||||||
| 		for j := range messages[i].Attachments { | 		for j := range messages[i].Attachments { | ||||||
| 			messages[i].Attachments[j].URL = app.media.GetURL(messages[i].Attachments[j].UUID) | 			messages[i].Attachments[j].URL = app.media.GetURL(messages[i].Attachments[j].UUID) | ||||||
| 		} | 		} | ||||||
|  | 		// Redact CSAT survey link | ||||||
| 		messages[i].CensorCSATContent() | 		messages[i].CensorCSATContent() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(envelope.PageResults{ | 	return r.SendEnvelope(envelope.PageResults{ | ||||||
| 		Total:      total, | 		Total:      total, | ||||||
| 		Results:    messages, | 		Results:    messages, | ||||||
| @@ -70,7 +78,7 @@ func handleGetMessage(r *fastglue.Request) error { | |||||||
| 		cuuid = r.RequestCtx.UserValue("cuuid").(string) | 		cuuid = r.RequestCtx.UserValue("cuuid").(string) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -96,7 +104,7 @@ func handleGetMessage(r *fastglue.Request) error { | |||||||
| 	return r.SendEnvelope(message) | 	return r.SendEnvelope(message) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleRetryMessage changes message status so it can be retried for sending. | // handleRetryMessage changes message status to `pending`, so it's enqueued for sending. | ||||||
| func handleRetryMessage(r *fastglue.Request) error { | func handleRetryMessage(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| @@ -105,7 +113,7 @@ func handleRetryMessage(r *fastglue.Request) error { | |||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -116,8 +124,7 @@ func handleRetryMessage(r *fastglue.Request) error { | |||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err = app.conversation.MarkMessageAsPending(uuid) | 	if err = app.conversation.MarkMessageAsPending(uuid); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| @@ -129,16 +136,15 @@ func handleSendMessage(r *fastglue.Request) error { | |||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		cuuid = r.RequestCtx.UserValue("cuuid").(string) | 		cuuid = r.RequestCtx.UserValue("cuuid").(string) | ||||||
| 		media = []medModels.Media{} |  | ||||||
| 		req   = messageReq{} | 		req   = messageReq{} | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Check permission | 	// Check access to conversation. | ||||||
| 	conv, err := enforceConversationAccess(app, cuuid, user) | 	conv, err := enforceConversationAccess(app, cuuid, user) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| @@ -146,34 +152,66 @@ func handleSendMessage(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		app.lo.Error("error unmarshalling message request", "error", err) | 		app.lo.Error("error unmarshalling message request", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "error decoding request", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if req.SenderType != umodels.UserTypeAgent && req.SenderType != umodels.UserTypeContact { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`sender_type`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Contacts cannot send private messages | ||||||
|  | 	if req.SenderType == umodels.UserTypeContact && req.Private { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("globals.messages.badRequest"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Check if user has permission to send messages as contact | ||||||
|  | 	if req.SenderType == umodels.UserTypeContact { | ||||||
|  | 		parts := strings.Split(authzModels.PermMessagesWriteAsContact, ":") | ||||||
|  | 		if len(parts) != 2 { | ||||||
|  | 			return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorChecking", "name", "{globals.terms.permission}"), nil)) | ||||||
|  | 		} | ||||||
|  | 		ok, err := app.authz.Enforce(user, parts[0], parts[1]) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorChecking", "name", "{globals.terms.permission}"), nil)) | ||||||
|  | 		} | ||||||
|  | 		if !ok { | ||||||
|  | 			return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.Ts("globals.messages.denied", "name", "{globals.terms.permission}"), nil, envelope.PermissionError) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Get media for all attachments. | ||||||
|  | 	var media = make([]medModels.Media, 0, len(req.Attachments)) | ||||||
| 	for _, id := range req.Attachments { | 	for _, id := range req.Attachments { | ||||||
| 		m, err := app.media.Get(id) | 		m, err := app.media.Get(id, "") | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			app.lo.Error("error fetching media", "error", err) | 			app.lo.Error("error fetching media", "error", err) | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error fetching media", nil, envelope.GeneralError) | 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.media}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 		media = append(media, m) | 		media = append(media, m) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if req.Private { | 	// Create contact message. | ||||||
| 		if err := app.conversation.SendPrivateNote(media, user.ID, cuuid, req.Message); err != nil { | 	if req.SenderType == umodels.UserTypeContact { | ||||||
|  | 		message, err := app.conversation.CreateContactMessage(media, int(conv.ContactID), cuuid, req.Message, cmodels.ContentTypeHTML) | ||||||
|  | 		if err != nil { | ||||||
| 			return sendErrorEnvelope(r, err) | 			return sendErrorEnvelope(r, err) | ||||||
| 		} | 		} | ||||||
| 	} else { | 		return r.SendEnvelope(message) | ||||||
| 		if err := app.conversation.SendReply(media, conv.InboxID, user.ID, cuuid, req.Message, req.CC, req.BCC, map[string]any{} /**meta**/); err != nil { |  | ||||||
| 			return sendErrorEnvelope(r, err) |  | ||||||
| 		} |  | ||||||
| 		// Evaluate automation rules. |  | ||||||
| 		app.automation.EvaluateConversationUpdateRules(cuuid, models.EventConversationMessageOutgoing) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Reopen if snoozed/closed/resolved regardless of automation rules - this is the default behavior | 	// Send private note. | ||||||
| 	if err := app.conversation.ReOpenConversation(cuuid, user); err != nil { | 	if req.Private { | ||||||
|  | 		message, err := app.conversation.SendPrivateNote(media, user.ID, cuuid, req.Message) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return sendErrorEnvelope(r, err) | ||||||
|  | 		} | ||||||
|  | 		return r.SendEnvelope(message) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Queue reply. | ||||||
|  | 	message, err := app.conversation.QueueReply(media, conv.InboxID, user.ID, cuuid, req.Message, req.To, req.CC, req.BCC, map[string]any{} /**meta**/) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	return r.SendEnvelope(message) | ||||||
| 	return r.SendEnvelope("Message sent successfully") |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,30 +6,80 @@ import ( | |||||||
|  |  | ||||||
| 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | 	amodels "github.com/abhinavxd/libredesk/internal/auth/models" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/user/models" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| 	"github.com/zerodha/simplesessions/v3" | 	"github.com/zerodha/simplesessions/v3" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // tryAuth is a middleware that attempts to authenticate the user and add them to the context | // authenticateUser handles both API key and session-based authentication | ||||||
| // but doesn't enforce authentication. Handlers can check if user exists in context optionally. | // Returns the authenticated user or an error | ||||||
|  | // For session-based auth, CSRF is checked for POST/PUT/DELETE requests | ||||||
|  | func authenticateUser(r *fastglue.Request, app *App) (models.User, error) { | ||||||
|  | 	var user models.User | ||||||
|  |  | ||||||
|  | 	// Check for Authorization header first (API key authentication) | ||||||
|  | 	apiKey, apiSecret, err := r.ParseAuthHeader(fastglue.AuthBasic | fastglue.AuthToken) | ||||||
|  | 	if err == nil && len(apiKey) > 0 && len(apiSecret) > 0 { | ||||||
|  | 		user, err = app.user.ValidateAPIKey(string(apiKey), string(apiSecret)) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return user, err | ||||||
|  | 		} | ||||||
|  | 		return user, nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Session-based authentication - Check CSRF first. | ||||||
|  | 	method := string(r.RequestCtx.Method()) | ||||||
|  | 	if method == "POST" || method == "PUT" || method == "DELETE" { | ||||||
|  | 		cookieToken := string(r.RequestCtx.Request.Header.Cookie("csrf_token")) | ||||||
|  | 		hdrToken := string(r.RequestCtx.Request.Header.Peek("X-CSRFTOKEN")) | ||||||
|  |  | ||||||
|  | 		// Match CSRF token from cookie and header. | ||||||
|  | 		if cookieToken == "" || hdrToken == "" || cookieToken != hdrToken { | ||||||
|  | 			app.lo.Error("csrf token mismatch", "method", method, "cookie_token", cookieToken, "header_token", hdrToken) | ||||||
|  | 			return user, envelope.NewError(envelope.PermissionError, app.i18n.T("auth.csrfTokenMismatch"), nil) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Validate session and fetch user. | ||||||
|  | 	sessUser, err := app.auth.ValidateSession(r) | ||||||
|  | 	if err != nil || sessUser.ID <= 0 { | ||||||
|  | 		app.lo.Error("error validating session", "error", err) | ||||||
|  | 		return user, envelope.NewError(envelope.GeneralError, app.i18n.T("auth.invalidOrExpiredSession"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Get agent user from cache or load it. | ||||||
|  | 	user, err = app.user.GetAgentCachedOrLoad(sessUser.ID) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return user, err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Destroy session if user is disabled. | ||||||
|  | 	if !user.Enabled { | ||||||
|  | 		if err := app.auth.DestroySession(r); err != nil { | ||||||
|  | 			app.lo.Error("error destroying session", "error", err) | ||||||
|  | 		} | ||||||
|  | 		return user, envelope.NewError(envelope.PermissionError, app.i18n.T("user.accountDisabled"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return user, nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // tryAuth attempts to authenticate the user and add them to the context but doesn't enforce authentication. | ||||||
|  | // Handlers can check if user exists in context optionally. | ||||||
|  | // Supports both API key authentication (Authorization header) and session-based authentication. | ||||||
| func tryAuth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler { | func tryAuth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler { | ||||||
| 	return func(r *fastglue.Request) error { | 	return func(r *fastglue.Request) error { | ||||||
| 		app := r.Context.(*App) | 		app := r.Context.(*App) | ||||||
|  |  | ||||||
| 		// Try to validate session without returning error. | 		// Try to authenticate user using shared authentication logic, but don't return errors | ||||||
| 		userSession, err := app.auth.ValidateSession(r) | 		user, err := authenticateUser(r, app) | ||||||
| 		if err != nil || userSession.ID <= 0 { |  | ||||||
| 			return handler(r) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Try to get user. |  | ||||||
| 		user, err := app.user.GetAgent(userSession.ID) |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
|  | 			// Authentication failed, but this is optional, so continue without user | ||||||
| 			return handler(r) | 			return handler(r) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Set user in context if found. | 		// Set user in context if authentication succeeded. | ||||||
| 		r.RequestCtx.SetUserValue("user", amodels.User{ | 		r.RequestCtx.SetUserValue("user", amodels.User{ | ||||||
| 			ID:        user.ID, | 			ID:        user.ID, | ||||||
| 			Email:     user.Email.String, | 			Email:     user.Email.String, | ||||||
| @@ -41,23 +91,25 @@ func tryAuth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // auth makes sure the user is logged in. | // auth validates the session or API key and adds the user to the request context. | ||||||
|  | // Supports both API key authentication (Authorization header) and session-based authentication. | ||||||
| func auth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler { | func auth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler { | ||||||
| 	return func(r *fastglue.Request) error { | 	return func(r *fastglue.Request) error { | ||||||
| 		var app = r.Context.(*App) | 		var app = r.Context.(*App) | ||||||
|  |  | ||||||
| 		// Validate session and fetch user. | 		// Authenticate user using shared authentication logic | ||||||
| 		userSession, err := app.auth.ValidateSession(r) | 		user, err := authenticateUser(r, app) | ||||||
| 		if err != nil || userSession.ID <= 0 { | 		if err != nil { | ||||||
| 			app.lo.Error("error validating session", "error", err) | 			if envErr, ok := err.(envelope.Error); ok { | ||||||
| 			return r.SendErrorEnvelope(http.StatusUnauthorized, "Invalid or expired session", nil, envelope.PermissionError) | 				if envErr.ErrorType == envelope.PermissionError { | ||||||
|  | 					return r.SendErrorEnvelope(http.StatusForbidden, envErr.Message, nil, envelope.PermissionError) | ||||||
|  | 				} | ||||||
|  | 				return r.SendErrorEnvelope(http.StatusUnauthorized, envErr.Message, nil, envelope.GeneralError) | ||||||
|  | 			} | ||||||
|  | 			return sendErrorEnvelope(r, err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Set user in the request context. | 		// Set user in the request context. | ||||||
| 		user, err := app.user.GetAgent(userSession.ID) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return sendErrorEnvelope(r, err) |  | ||||||
| 		} |  | ||||||
| 		r.RequestCtx.SetUserValue("user", amodels.User{ | 		r.RequestCtx.SetUserValue("user", amodels.User{ | ||||||
| 			ID:        user.ID, | 			ID:        user.ID, | ||||||
| 			Email:     user.Email.String, | 			Email:     user.Email.String, | ||||||
| @@ -69,53 +121,36 @@ func auth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // perm does session validation, CSRF, and permission enforcement. | // perm checks if the user has the required permission to access the endpoint. | ||||||
|  | // Supports both API key authentication (Authorization header) and session-based authentication. | ||||||
| func perm(handler fastglue.FastRequestHandler, perm string) fastglue.FastRequestHandler { | func perm(handler fastglue.FastRequestHandler, perm string) fastglue.FastRequestHandler { | ||||||
| 	return func(r *fastglue.Request) error { | 	return func(r *fastglue.Request) error { | ||||||
| 		var ( | 		var app = r.Context.(*App) | ||||||
| 			app         = r.Context.(*App) |  | ||||||
| 			cookieToken = string(r.RequestCtx.Request.Header.Cookie("csrf_token")) |  | ||||||
| 			hdrToken    = string(r.RequestCtx.Request.Header.Peek("X-CSRFTOKEN")) |  | ||||||
| 		) |  | ||||||
|  |  | ||||||
| 		if cookieToken == "" || hdrToken == "" || cookieToken != hdrToken { | 		// Authenticate user using shared authentication logic | ||||||
| 			app.lo.Error("csrf token mismatch", "cookie_token", cookieToken, "header_token", hdrToken) | 		user, err := authenticateUser(r, app) | ||||||
| 			return r.SendErrorEnvelope(http.StatusForbidden, "Invalid CSRF token", nil, envelope.PermissionError) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Validate session and fetch user. |  | ||||||
| 		sessUser, err := app.auth.ValidateSession(r) |  | ||||||
| 		if err != nil || sessUser.ID <= 0 { |  | ||||||
| 			app.lo.Error("error validating session", "error", err) |  | ||||||
| 			return r.SendErrorEnvelope(http.StatusUnauthorized, "Invalid or expired session", nil, envelope.PermissionError) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Get user from DB. |  | ||||||
| 		user, err := app.user.GetAgent(sessUser.ID) |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return sendErrorEnvelope(r, err) | 			if envErr, ok := err.(envelope.Error); ok { | ||||||
| 		} | 				if envErr.ErrorType == envelope.PermissionError { | ||||||
|  | 					return r.SendErrorEnvelope(http.StatusForbidden, envErr.Message, nil, envelope.PermissionError) | ||||||
| 		// Destroy session if user is disabled. | 				} | ||||||
| 		if !user.Enabled { | 				return r.SendErrorEnvelope(http.StatusUnauthorized, envErr.Message, nil, envelope.GeneralError) | ||||||
| 			if err := app.auth.DestroySession(r); err != nil { |  | ||||||
| 				app.lo.Error("error destroying session", "error", err) |  | ||||||
| 			} | 			} | ||||||
| 			return r.SendErrorEnvelope(http.StatusUnauthorized, "User account disabled", nil, envelope.PermissionError) | 			return sendErrorEnvelope(r, err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Split the permission string into object and action and enforce it. | 		// Split the permission string into object and action and enforce it. | ||||||
| 		parts := strings.Split(perm, ":") | 		parts := strings.Split(perm, ":") | ||||||
| 		if len(parts) != 2 { | 		if len(parts) != 2 { | ||||||
| 			return r.SendErrorEnvelope(http.StatusInternalServerError, "Invalid permission format", nil, envelope.GeneralError) | 			return r.SendErrorEnvelope(http.StatusInternalServerError, app.i18n.Ts("globals.messages.invalid", "name", "{globals.terms.permission}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 		object, action := parts[0], parts[1] | 		object, action := parts[0], parts[1] | ||||||
| 		ok, err := app.authz.Enforce(user, object, action) | 		ok, err := app.authz.Enforce(user, object, action) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return r.SendErrorEnvelope(http.StatusInternalServerError, "Error checking permissions", nil, envelope.GeneralError) | 			return r.SendErrorEnvelope(http.StatusInternalServerError, app.i18n.Ts("globals.messages.errorChecking", "name", "{globals.terms.permission}"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
| 		if !ok { | 		if !ok { | ||||||
| 			return r.SendErrorEnvelope(http.StatusForbidden, "Permission denied", nil, envelope.PermissionError) | 			return r.SendErrorEnvelope(http.StatusForbidden, app.i18n.Ts("globals.messages.denied", "name", "{globals.terms.permission}"), nil, envelope.PermissionError) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Set user in the request context. | 		// Set user in the request context. | ||||||
| @@ -141,7 +176,7 @@ func authPage(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler { | |||||||
| 			// Session is not valid, destroy it and redirect to login. | 			// Session is not valid, destroy it and redirect to login. | ||||||
| 			if err != simplesessions.ErrInvalidSession { | 			if err != simplesessions.ErrInvalidSession { | ||||||
| 				app.lo.Error("error validating session", "error", err) | 				app.lo.Error("error validating session", "error", err) | ||||||
| 				return r.SendErrorEnvelope(http.StatusUnauthorized, "Error validating session", nil, envelope.PermissionError) | 				return r.SendErrorEnvelope(http.StatusUnauthorized, app.i18n.Ts("globals.messages.errorValidating", "name", "{globals.terms.session}"), nil, envelope.GeneralError) | ||||||
| 			} | 			} | ||||||
| 			if err := app.auth.DestroySession(r); err != nil { | 			if err := app.auth.DestroySession(r); err != nil { | ||||||
| 				app.lo.Error("error destroying session", "error", err) | 				app.lo.Error("error destroying session", "error", err) | ||||||
| @@ -172,7 +207,7 @@ func notAuthPage(handler fastglue.FastRequestHandler) fastglue.FastRequestHandle | |||||||
| 		user, err := app.auth.ValidateSession(r) | 		user, err := app.auth.ValidateSession(r) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			app.lo.Error("error validating session", "error", err) | 			app.lo.Error("error validating session", "error", err) | ||||||
| 			return r.SendErrorEnvelope(http.StatusUnauthorized, "Invalid or expired session", nil, envelope.PermissionError) | 			return r.SendErrorEnvelope(http.StatusUnauthorized, app.i18n.T("auth.invalidOrExpiredSessionClearCookie"), nil, envelope.GeneralError) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if user.ID != 0 { | 		if user.ID != 0 { | ||||||
|   | |||||||
							
								
								
									
										68
									
								
								cmd/oidc.go
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								cmd/oidc.go
									
									
									
									
									
								
							| @@ -11,16 +11,6 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // handleGetAllEnabledOIDC returns all enabled OIDC records |  | ||||||
| func handleGetAllEnabledOIDC(r *fastglue.Request) error { |  | ||||||
| 	app := r.Context.(*App) |  | ||||||
| 	out, err := app.oidc.GetAllEnabled() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
| 	return r.SendEnvelope(out) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // handleGetAllOIDC returns all OIDC records | // handleGetAllOIDC returns all OIDC records | ||||||
| func handleGetAllOIDC(r *fastglue.Request) error { | func handleGetAllOIDC(r *fastglue.Request) error { | ||||||
| 	app := r.Context.(*App) | 	app := r.Context.(*App) | ||||||
| @@ -41,7 +31,7 @@ func handleGetOIDC(r *fastglue.Request) error { | |||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id <= 0 { | 	if err != nil || id <= 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | ||||||
| 			"Invalid OIDC `id`", nil, envelope.InputError) | 			app.i18n.Ts("globals.messages.invalid", "name", "OIDC `id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	o, err := app.oidc.Get(id, false) | 	o, err := app.oidc.Get(id, false) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -50,18 +40,6 @@ func handleGetOIDC(r *fastglue.Request) error { | |||||||
| 	return r.SendEnvelope(o) | 	return r.SendEnvelope(o) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleTestOIDC tests an OIDC provider URL by doing a discovery on the provider URL. |  | ||||||
| func handleTestOIDC(r *fastglue.Request) error { |  | ||||||
| 	var ( |  | ||||||
| 		app         = r.Context.(*App) |  | ||||||
| 		providerURL = string(r.RequestCtx.PostArgs().Peek("provider_url")) |  | ||||||
| 	) |  | ||||||
| 	if err := app.auth.TestProvider(providerURL); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
| 	return r.SendEnvelope("OIDC provider discovered successfully") |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // handleCreateOIDC creates a new OIDC record. | // handleCreateOIDC creates a new OIDC record. | ||||||
| func handleCreateOIDC(r *fastglue.Request) error { | func handleCreateOIDC(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| @@ -69,18 +47,28 @@ func handleCreateOIDC(r *fastglue.Request) error { | |||||||
| 		req = models.OIDC{} | 		req = models.OIDC{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Bad request", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := app.oidc.Create(req); err != nil { | 	// Test OIDC provider URL by performing a discovery. | ||||||
|  | 	if err := app.auth.TestProvider(req.ProviderURL); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	createdOIDC, err := app.oidc.Create(req) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Reload the auth manager to update the OIDC providers. | 	// Reload the auth manager to update the OIDC providers. | ||||||
| 	if err := reloadAuth(app); err != nil { | 	if err := reloadAuth(app); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.couldNotReload", "name", "OIDC"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("OIDC created successfully") |  | ||||||
|  | 	// Clear client secret before returning | ||||||
|  | 	createdOIDC.ClientSecret = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(createdOIDC) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateOIDC updates an OIDC record. | // handleUpdateOIDC updates an OIDC record. | ||||||
| @@ -91,23 +79,32 @@ func handleUpdateOIDC(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "OIDC `id`"), nil, envelope.InputError) | ||||||
| 			"Invalid oidc `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Bad request", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.oidc.Update(id, req); err != nil { | 	// Test OIDC provider URL by performing a discovery. | ||||||
|  | 	if err := app.auth.TestProvider(req.ProviderURL); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	updatedOIDC, err := app.oidc.Update(id, req) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Reload the auth manager to update the OIDC providers. | 	// Reload the auth manager to update the OIDC providers. | ||||||
| 	if err := reloadAuth(app); err != nil { | 	if err := reloadAuth(app); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.couldNotReload", "name", "OIDC"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("OIDC updated successfully") |  | ||||||
|  | 	// Clear client secret before returning | ||||||
|  | 	updatedOIDC.ClientSecret = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(updatedOIDC) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteOIDC deletes an OIDC record. | // handleDeleteOIDC deletes an OIDC record. | ||||||
| @@ -115,11 +112,10 @@ func handleDeleteOIDC(r *fastglue.Request) error { | |||||||
| 	var app = r.Context.(*App) | 	var app = r.Context.(*App) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "OIDC `id`"), nil, envelope.InputError) | ||||||
| 			"Invalid oidc `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
| 	if err = app.oidc.Delete(id); err != nil { | 	if err = app.oidc.Delete(id); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("OIDC deleted successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										45
									
								
								cmd/report.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								cmd/report.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"strconv" | ||||||
|  |  | ||||||
|  | 	"github.com/zerodha/fastglue" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // handleOverviewCounts retrieves general dashboard counts for all users. | ||||||
|  | func handleOverviewCounts(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app = r.Context.(*App) | ||||||
|  | 	) | ||||||
|  | 	counts, err := app.report.GetOverViewCounts() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(counts) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleOverviewCharts retrieves general dashboard chart data. | ||||||
|  | func handleOverviewCharts(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app     = r.Context.(*App) | ||||||
|  | 		days, _ = strconv.Atoi(string(r.RequestCtx.QueryArgs().Peek("days"))) | ||||||
|  | 	) | ||||||
|  | 	charts, err := app.report.GetOverviewChart(days) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(charts) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleOverviewSLA retrieves SLA data for the dashboard. | ||||||
|  | func handleOverviewSLA(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app     = r.Context.(*App) | ||||||
|  | 		days, _ = strconv.Atoi(string(r.RequestCtx.QueryArgs().Peek("days"))) | ||||||
|  | 	) | ||||||
|  | 	sla, err := app.report.GetOverviewSLA(days) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(sla) | ||||||
|  | } | ||||||
							
								
								
									
										20
									
								
								cmd/roles.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								cmd/roles.go
									
									
									
									
									
								
							| @@ -14,11 +14,11 @@ func handleGetRoles(r *fastglue.Request) error { | |||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 	) | 	) | ||||||
| 	agents, err := app.role.GetAll() | 	roles, err := app.role.GetAll() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(agents) | 	return r.SendEnvelope(roles) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetRole returns a single role | // handleGetRole returns a single role | ||||||
| @@ -43,7 +43,7 @@ func handleDeleteRole(r *fastglue.Request) error { | |||||||
| 	if err := app.role.Delete(id); err != nil { | 	if err := app.role.Delete(id); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Role deleted successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleCreateRole creates a new role | // handleCreateRole creates a new role | ||||||
| @@ -53,12 +53,13 @@ func handleCreateRole(r *fastglue.Request) error { | |||||||
| 		req = models.Role{} | 		req = models.Role{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err := app.role.Create(req); err != nil { | 	createdRole, err := app.role.Create(req) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Role created successfully") | 	return r.SendEnvelope(createdRole) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateRole updates a role | // handleUpdateRole updates a role | ||||||
| @@ -69,10 +70,11 @@ func handleUpdateRole(r *fastglue.Request) error { | |||||||
| 		req   = models.Role{} | 		req   = models.Role{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err := app.role.Update(id, req);err != nil { | 	updatedRole, err := app.role.Update(id, req) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Role updated successfully") | 	return r.SendEnvelope(updatedRole) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,6 +1,8 @@ | |||||||
| package main | package main | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"fmt" | ||||||
|  |  | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
| @@ -11,52 +13,45 @@ const ( | |||||||
|  |  | ||||||
| // handleSearchConversations searches conversations based on the query. | // handleSearchConversations searches conversations based on the query. | ||||||
| func handleSearchConversations(r *fastglue.Request) error { | func handleSearchConversations(r *fastglue.Request) error { | ||||||
| 	var ( | 	app := r.Context.(*App) | ||||||
| 		app = r.Context.(*App) | 	wrapper := func(query string) (interface{}, error) { | ||||||
| 		q   = string(r.RequestCtx.QueryArgs().Peek("query")) | 		return app.search.Conversations(query) | ||||||
| 	) |  | ||||||
|  |  | ||||||
| 	if len(q) < minSearchQueryLength { |  | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, "Query length should be at least 3 characters", nil)) |  | ||||||
| 	} | 	} | ||||||
|  | 	return handleSearch(r, wrapper) | ||||||
| 	conversations, err := app.search.Conversations(q) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
| 	return r.SendEnvelope(conversations) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleSearchMessages searches messages based on the query. | // handleSearchMessages searches messages based on the query. | ||||||
| func handleSearchMessages(r *fastglue.Request) error { | func handleSearchMessages(r *fastglue.Request) error { | ||||||
| 	var ( | 	app := r.Context.(*App) | ||||||
| 		app = r.Context.(*App) | 	wrapper := func(query string) (interface{}, error) { | ||||||
| 		q   = string(r.RequestCtx.QueryArgs().Peek("query")) | 		return app.search.Messages(query) | ||||||
| 	) |  | ||||||
|  |  | ||||||
| 	if len(q) < minSearchQueryLength { |  | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, "Query length should be at least 3 characters", nil)) |  | ||||||
| 	} | 	} | ||||||
|  | 	return handleSearch(r, wrapper) | ||||||
| 	messages, err := app.search.Messages(q) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
| 	return r.SendEnvelope(messages) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleSearchContacts searches contacts based on the query. | // handleSearchContacts searches contacts based on the query. | ||||||
| func handleSearchContacts(r *fastglue.Request) error { | func handleSearchContacts(r *fastglue.Request) error { | ||||||
|  | 	app := r.Context.(*App) | ||||||
|  | 	wrapper := func(query string) (interface{}, error) { | ||||||
|  | 		return app.search.Contacts(query) | ||||||
|  | 	} | ||||||
|  | 	return handleSearch(r, wrapper) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleSearch searches for the given query using the provided search function. | ||||||
|  | func handleSearch(r *fastglue.Request, searchFunc func(string) (interface{}, error)) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		q   = string(r.RequestCtx.QueryArgs().Peek("query")) | 		q   = string(r.RequestCtx.QueryArgs().Peek("query")) | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	if len(q) < minSearchQueryLength { | 	if len(q) < minSearchQueryLength { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, "Query length should be at least 3 characters", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("search.minQueryLength", "length", fmt.Sprintf("%d", minSearchQueryLength)), nil)) | ||||||
| 	} | 	} | ||||||
| 	contacts, err := app.search.Contacts(q) |  | ||||||
|  | 	results, err := searchFunc(q) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(contacts) | 	return r.SendEnvelope(results) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // handleGetGeneralSettings fetches general settings. | // handleGetGeneralSettings fetches general settings, this endpoint is not behind auth as it has no sensitive data and is required for the app to function. | ||||||
| func handleGetGeneralSettings(r *fastglue.Request) error { | func handleGetGeneralSettings(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| @@ -25,12 +25,14 @@ func handleGetGeneralSettings(r *fastglue.Request) error { | |||||||
| 	var settings map[string]interface{} | 	var settings map[string]interface{} | ||||||
| 	if err := json.Unmarshal(out, &settings); err != nil { | 	if err := json.Unmarshal(out, &settings); err != nil { | ||||||
| 		app.lo.Error("error unmarshalling settings", "err", err) | 		app.lo.Error("error unmarshalling settings", "err", err) | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "Error fetching settings", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", app.i18n.T("globals.terms.setting")), nil)) | ||||||
| 	} | 	} | ||||||
| 	// Set the app.update to the settings, adding `app` prefix to the key to match the settings structure in db. | 	// Set the app.update to the settings, adding `app` prefix to the key to match the settings structure in db. | ||||||
| 	settings["app.update"] = app.update | 	settings["app.update"] = app.update | ||||||
| 	// Set app version. | 	// Set app version. | ||||||
| 	settings["app.version"] = versionString | 	settings["app.version"] = versionString | ||||||
|  | 	// Set restart required flag. | ||||||
|  | 	settings["app.restart_required"] = app.restartRequired | ||||||
| 	return r.SendEnvelope(settings) | 	return r.SendEnvelope(settings) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -42,20 +44,39 @@ func handleUpdateGeneralSettings(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Bad request", nil, "") | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("globals.messages.badRequest"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// Get current language before update. | ||||||
|  | 	app.Lock() | ||||||
|  | 	oldLang := ko.String("app.lang") | ||||||
|  | 	app.Unlock() | ||||||
|  |  | ||||||
|  | 	// Remove any trailing slash `/` from the root url. | ||||||
|  | 	req.RootURL = strings.TrimRight(req.RootURL, "/") | ||||||
|  |  | ||||||
| 	if err := app.setting.Update(req); err != nil { | 	if err := app.setting.Update(req); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	// Reload the settings and templates. | 	// Reload the settings and templates. | ||||||
| 	if err := reloadSettings(app); err != nil { | 	if err := reloadSettings(app); err != nil { | ||||||
| 		return envelope.NewError(envelope.GeneralError, "Could not reload settings, Please restart the app.", nil) | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.couldNotReload", "name", app.i18n.T("globals.terms.setting")), nil) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// Check if language changed and reload i18n if needed. | ||||||
|  | 	app.Lock() | ||||||
|  | 	newLang := ko.String("app.lang") | ||||||
|  | 	if oldLang != newLang { | ||||||
|  | 		app.lo.Info("language changed, reloading i18n", "old_lang", oldLang, "new_lang", newLang) | ||||||
|  | 		app.i18n = initI18n(app.fs) | ||||||
|  | 		app.lo.Info("reloaded i18n", "old_lang", oldLang, "new_lang", newLang) | ||||||
|  | 	} | ||||||
|  | 	app.Unlock() | ||||||
|  |  | ||||||
| 	if err := reloadTemplates(app); err != nil { | 	if err := reloadTemplates(app); err != nil { | ||||||
| 		return envelope.NewError(envelope.GeneralError, "Could not reload settings, Please restart the app.", nil) | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.couldNotReload", "name", app.i18n.T("globals.terms.setting")), nil) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Settings updated successfully") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetEmailNotificationSettings fetches email notification settings. | // handleGetEmailNotificationSettings fetches email notification settings. | ||||||
| @@ -72,7 +93,7 @@ func handleGetEmailNotificationSettings(r *fastglue.Request) error { | |||||||
|  |  | ||||||
| 	// Unmarshal and filter out password. | 	// Unmarshal and filter out password. | ||||||
| 	if err := json.Unmarshal(out, ¬if); err != nil { | 	if err := json.Unmarshal(out, ¬if); err != nil { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "Error fetching settings", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorFetching", "name", app.i18n.T("globals.terms.setting")), nil)) | ||||||
| 	} | 	} | ||||||
| 	if notif.Password != "" { | 	if notif.Password != "" { | ||||||
| 		notif.Password = strings.Repeat(stringutil.PasswordDummy, 10) | 		notif.Password = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
| @@ -89,7 +110,7 @@ func handleUpdateEmailNotificationSettings(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Bad request", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("globals.messages.badRequest"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	out, err := app.setting.GetByPrefix("notification.email") | 	out, err := app.setting.GetByPrefix("notification.email") | ||||||
| @@ -98,14 +119,15 @@ func handleUpdateEmailNotificationSettings(r *fastglue.Request) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := json.Unmarshal(out, &cur); err != nil { | 	if err := json.Unmarshal(out, &cur); err != nil { | ||||||
| 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "Error updating settings", nil)) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorUpdating", "name", app.i18n.T("globals.terms.setting")), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Make sure it's a valid from email address. | 	// Make sure it's a valid from email address. | ||||||
| 	if _, err := mail.ParseAddress(req.EmailAddress); err != nil { | 	if _, err := mail.ParseAddress(req.EmailAddress); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid from email address format", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("globals.messages.invalidFromAddress"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// If empty then retain previous password. | ||||||
| 	if req.Password == "" { | 	if req.Password == "" { | ||||||
| 		req.Password = cur.Password | 		req.Password = cur.Password | ||||||
| 	} | 	} | ||||||
| @@ -114,6 +136,10 @@ func handleUpdateEmailNotificationSettings(r *fastglue.Request) error { | |||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// No reload implemented, so user has to restart the app. | 	// Email notification settings require app restart to take effect. | ||||||
| 	return r.SendEnvelope("Settings updated successfully, Please restart the app for changes to take effect.") | 	app.Lock() | ||||||
|  | 	app.restartRequired = true | ||||||
|  | 	app.Unlock() | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										158
									
								
								cmd/sla.go
									
									
									
									
									
								
							
							
						
						
									
										158
									
								
								cmd/sla.go
									
									
									
									
									
								
							| @@ -5,10 +5,12 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	smodels "github.com/abhinavxd/libredesk/internal/sla/models" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | // handleGetSLAs returns all SLAs. | ||||||
| func handleGetSLAs(r *fastglue.Request) error { | func handleGetSLAs(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| @@ -20,50 +22,82 @@ func handleGetSLAs(r *fastglue.Request) error { | |||||||
| 	return r.SendEnvelope(slas) | 	return r.SendEnvelope(slas) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleGetSLA returns the SLA with the given ID. | ||||||
| func handleGetSLA(r *fastglue.Request) error { | func handleGetSLA(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid SLA `id`.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	sla, err := app.sla.Get(id) | 	sla, err := app.sla.Get(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, "") | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(sla) | 	return r.SendEnvelope(sla) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleCreateSLA creates a new SLA. | ||||||
| func handleCreateSLA(r *fastglue.Request) error { | func handleCreateSLA(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app           = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		name          = string(r.RequestCtx.PostArgs().Peek("name")) | 		sla smodels.SLAPolicy | ||||||
| 		desc          = string(r.RequestCtx.PostArgs().Peek("description")) |  | ||||||
| 		firstRespTime = string(r.RequestCtx.PostArgs().Peek("first_response_time")) |  | ||||||
| 		resTime       = string(r.RequestCtx.PostArgs().Peek("resolution_time")) |  | ||||||
| 	) | 	) | ||||||
| 	// Validate time duration strings |  | ||||||
| 	if _, err := time.ParseDuration(firstRespTime); err != nil { | 	if err := r.Decode(&sla, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `first_response_time` duration.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if _, err := time.ParseDuration(resTime); err != nil { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `resolution_time` duration.", nil, envelope.InputError) | 	if err := validateSLA(app, &sla); err != nil { | ||||||
| 	} |  | ||||||
| 	if err := app.sla.Create(name, desc, firstRespTime, resTime); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("SLA created successfully.") |  | ||||||
|  | 	createdSLA, err := app.sla.Create(sla.Name, sla.Description, sla.FirstResponseTime, sla.ResolutionTime, sla.NextResponseTime, sla.Notifications) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(createdSLA) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleUpdateSLA updates the SLA with the given ID. | ||||||
|  | func handleUpdateSLA(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app = r.Context.(*App) | ||||||
|  | 		sla smodels.SLAPolicy | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	if err != nil || id == 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := r.Decode(&sla, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := validateSLA(app, &sla); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	updatedSLA, err := app.sla.Update(id, sla.Name, sla.Description, sla.FirstResponseTime, sla.ResolutionTime, sla.NextResponseTime, sla.Notifications) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(updatedSLA) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleDeleteSLA deletes the SLA with the given ID. | ||||||
| func handleDeleteSLA(r *fastglue.Request) error { | func handleDeleteSLA(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid SLA `id`.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.sla.Delete(id); err != nil { | 	if err = app.sla.Delete(id); err != nil { | ||||||
| @@ -73,31 +107,83 @@ func handleDeleteSLA(r *fastglue.Request) error { | |||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| func handleUpdateSLA(r *fastglue.Request) error { | // validateSLA validates the SLA policy and returns an envelope.Error if any validation fails. | ||||||
| 	var ( | func validateSLA(app *App, sla *smodels.SLAPolicy) error { | ||||||
| 		app           = r.Context.(*App) | 	if sla.Name == "" { | ||||||
| 		name          = string(r.RequestCtx.PostArgs().Peek("name")) | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil) | ||||||
| 		desc          = string(r.RequestCtx.PostArgs().Peek("description")) |  | ||||||
| 		firstRespTime = string(r.RequestCtx.PostArgs().Peek("first_response_time")) |  | ||||||
| 		resTime       = string(r.RequestCtx.PostArgs().Peek("resolution_time")) |  | ||||||
| 	) |  | ||||||
|  |  | ||||||
| 	// Validate time duration strings |  | ||||||
| 	if _, err := time.ParseDuration(firstRespTime); err != nil { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `first_response_time` duration.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
| 	if _, err := time.ParseDuration(resTime); err != nil { | 	if sla.FirstResponseTime.String == "" && sla.NextResponseTime.String == "" && sla.ResolutionTime.String == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `resolution_time` duration.", nil, envelope.InputError) | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "At least one of `first_response_time`, `next_response_time`, or `resolution_time` must be provided."), nil) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	// Validate notifications if any. | ||||||
| 	if err != nil || id == 0 { | 	for _, n := range sla.Notifications { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid SLA `id`.", nil, envelope.InputError) | 		if n.Type == "" { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`type`"), nil) | ||||||
|  | 		} | ||||||
|  | 		if n.TimeDelayType == "" { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`time_delay_type`"), nil) | ||||||
|  | 		} | ||||||
|  | 		if n.Metric == "" { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`metric`"), nil) | ||||||
|  | 		} | ||||||
|  | 		if n.TimeDelayType != "immediately" { | ||||||
|  | 			if n.TimeDelay == "" { | ||||||
|  | 				return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`time_delay`"), nil) | ||||||
|  | 			} | ||||||
|  | 			// Validate time delay duration. | ||||||
|  | 			td, err := time.ParseDuration(n.TimeDelay) | ||||||
|  | 			if err != nil { | ||||||
|  | 				return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`time_delay`"), nil) | ||||||
|  | 			} | ||||||
|  | 			if td.Minutes() < 1 { | ||||||
|  | 				return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`time_delay`"), nil) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		if len(n.Recipients) == 0 { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`recipients`"), nil) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := app.sla.Update(id, name, desc, firstRespTime, resTime); err != nil { | 	// Validate first response time duration string if not empty. | ||||||
| 		return sendErrorEnvelope(r, err) | 	if sla.FirstResponseTime.String != "" { | ||||||
|  | 		frt, err := time.ParseDuration(sla.FirstResponseTime.String) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`first_response_time`"), nil) | ||||||
|  | 		} | ||||||
|  | 		if frt.Minutes() < 1 { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`first_response_time`"), nil) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	// Validate resolution time duration string if not empty. | ||||||
|  | 	if sla.ResolutionTime.String != "" { | ||||||
|  | 		rt, err := time.ParseDuration(sla.ResolutionTime.String) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`resolution_time`"), nil) | ||||||
|  | 		} | ||||||
|  | 		if rt.Minutes() < 1 { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`resolution_time`"), nil) | ||||||
|  | 		} | ||||||
|  | 		// Compare with first response time if both are present. | ||||||
|  | 		if sla.FirstResponseTime.String != "" { | ||||||
|  | 			frt, _ := time.ParseDuration(sla.FirstResponseTime.String) | ||||||
|  | 			if frt > rt { | ||||||
|  | 				return envelope.NewError(envelope.InputError, app.i18n.T("sla.firstResponseTimeAfterResolution"), nil) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Validate next response time duration string if not empty. | ||||||
|  | 	if sla.NextResponseTime.String != "" { | ||||||
|  | 		nrt, err := time.ParseDuration(sla.NextResponseTime.String) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`next_response_time`"), nil) | ||||||
|  | 		} | ||||||
|  | 		if nrt.Minutes() < 1 { | ||||||
|  | 			return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.invalid", "name", "`next_response_time`"), nil) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -26,19 +26,19 @@ func handleCreateStatus(r *fastglue.Request) error { | |||||||
| 		status = cmodels.Status{} | 		status = cmodels.Status{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&status, "json"); err != nil { | 	if err := r.Decode(&status, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if status.Name == "" { | 	if status.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty status `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err := app.status.Create(status.Name) | 	createdStatus, err := app.status.Create(status.Name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(createdStatus) | ||||||
| } | } | ||||||
|  |  | ||||||
| func handleDeleteStatus(r *fastglue.Request) error { | func handleDeleteStatus(r *fastglue.Request) error { | ||||||
| @@ -46,20 +46,13 @@ func handleDeleteStatus(r *fastglue.Request) error { | |||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id <= 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid status `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if id <= 0 { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty status `ID`", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	err = app.status.Delete(id) | 	err = app.status.Delete(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -70,22 +63,21 @@ func handleUpdateStatus(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid status `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&status, "json"); err != nil { | 	if err := r.Decode(&status, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if status.Name == "" { | 	if status.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty status `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err = app.status.Update(id, status.Name) | 	updatedStatus, err := app.status.Update(id, status.Name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(updatedStatus) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								cmd/tags.go
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								cmd/tags.go
									
									
									
									
									
								
							| @@ -9,83 +9,80 @@ import ( | |||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | // handleGetTags returns all tags from the database. | ||||||
| func handleGetTags(r *fastglue.Request) error { | func handleGetTags(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 	) | 	) | ||||||
| 	t, err := app.tag.GetAll() | 	t, err := app.tag.GetAll() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, "") | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(t) | 	return r.SendEnvelope(t) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleCreateTag creates a new tag in the database. | ||||||
| func handleCreateTag(r *fastglue.Request) error { | func handleCreateTag(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		tag = tmodels.Tag{} | 		tag = tmodels.Tag{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&tag, "json"); err != nil { | 	if err := r.Decode(&tag, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if tag.Name == "" { | 	if tag.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty tag `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err := app.tag.Create(tag.Name) | 	createdTag, err := app.tag.Create(tag.Name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(createdTag) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleDeleteTag deletes a tag from the database. | ||||||
| func handleDeleteTag(r *fastglue.Request) error { | func handleDeleteTag(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id <= 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid tag `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if id <= 0 { | 	if err = app.tag.Delete(id); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty tag `ID`", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	err = app.tag.Delete(id) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // handleUpdateTag updates an existing tag in the database. | ||||||
| func handleUpdateTag(r *fastglue.Request) error { | func handleUpdateTag(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		tag = tmodels.Tag{} | 		tag = tmodels.Tag{} | ||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id <= 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid tag `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&tag, "json"); err != nil { | 	if err := r.Decode(&tag, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if tag.Name == "" { | 	if tag.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty tag `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err = app.tag.Update(id, tag.Name) | 	updatedTag, err := app.tag.Update(id, tag.Name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(updatedTag) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										54
									
								
								cmd/teams.go
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								cmd/teams.go
									
									
									
									
									
								
							| @@ -4,8 +4,8 @@ import ( | |||||||
| 	"strconv" | 	"strconv" | ||||||
|  |  | ||||||
| 	"github.com/abhinavxd/libredesk/internal/envelope" | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/team/models" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/volatiletech/null/v9" |  | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -40,7 +40,7 @@ func handleGetTeam(r *fastglue.Request) error { | |||||||
| 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	) | 	) | ||||||
| 	if id < 1 { | 	if id < 1 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid team `id`.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	team, err := app.team.Get(id) | 	team, err := app.team.Get(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -52,41 +52,42 @@ func handleGetTeam(r *fastglue.Request) error { | |||||||
| // handleCreateTeam creates a new team. | // handleCreateTeam creates a new team. | ||||||
| func handleCreateTeam(r *fastglue.Request) error { | func handleCreateTeam(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app                             = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		name                            = string(r.RequestCtx.PostArgs().Peek("name")) | 		req = models.Team{} | ||||||
| 		timezone                        = string(r.RequestCtx.PostArgs().Peek("timezone")) |  | ||||||
| 		emoji                           = string(r.RequestCtx.PostArgs().Peek("emoji")) |  | ||||||
| 		conversationAssignmentType      = string(r.RequestCtx.PostArgs().Peek("conversation_assignment_type")) |  | ||||||
| 		businessHrsID, _                = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("business_hours_id"))) |  | ||||||
| 		slaPolicyID, _                  = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("sla_policy_id"))) |  | ||||||
| 		maxAutoAssignedConversations, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("max_auto_assigned_conversations"))) |  | ||||||
| 	) | 	) | ||||||
| 	if err := app.team.Create(name, timezone, conversationAssignmentType, null.NewInt(businessHrsID, businessHrsID != 0), null.NewInt(slaPolicyID, slaPolicyID != 0), emoji, maxAutoAssignedConversations); err != nil { |  | ||||||
|  | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	createdTeam, err := app.team.Create(req.Name, req.Timezone, req.ConversationAssignmentType, req.BusinessHoursID, req.SLAPolicyID, req.Emoji.String, req.MaxAutoAssignedConversations) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Team created successfully.") | 	return r.SendEnvelope(createdTeam) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateTeam updates an existing team. | // handleUpdateTeam updates an existing team. | ||||||
| func handleUpdateTeam(r *fastglue.Request) error { | func handleUpdateTeam(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app                             = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		name                            = string(r.RequestCtx.PostArgs().Peek("name")) | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 		timezone                        = string(r.RequestCtx.PostArgs().Peek("timezone")) | 		req   = models.Team{} | ||||||
| 		emoji                           = string(r.RequestCtx.PostArgs().Peek("emoji")) |  | ||||||
| 		conversationAssignmentType      = string(r.RequestCtx.PostArgs().Peek("conversation_assignment_type")) |  | ||||||
| 		id, _                           = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) |  | ||||||
| 		businessHrsID, _                = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("business_hours_id"))) |  | ||||||
| 		slaPolicyID, _                  = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("sla_policy_id"))) |  | ||||||
| 		maxAutoAssignedConversations, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("max_auto_assigned_conversations"))) |  | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	if id < 1 { | 	if id < 1 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid team `id`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err := app.team.Update(id, name, timezone, conversationAssignmentType, null.NewInt(businessHrsID, businessHrsID != 0), null.NewInt(slaPolicyID, slaPolicyID != 0), emoji, maxAutoAssignedConversations); err != nil { |  | ||||||
|  | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	updatedTeam, err := app.team.Update(id, req.Name, req.Timezone, req.ConversationAssignmentType, req.BusinessHoursID, req.SLAPolicyID, req.Emoji.String, req.MaxAutoAssignedConversations) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Team updated successfully.") | 	return r.SendEnvelope(updatedTeam) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteTeam deletes a team | // handleDeleteTeam deletes a team | ||||||
| @@ -96,12 +97,11 @@ func handleDeleteTeam(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid team `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
| 	err = app.team.Delete(id) | 	err = app.team.Delete(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Team deleted successfully.") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ func handleGetTemplates(r *fastglue.Request) error { | |||||||
| 		typ = string(r.RequestCtx.QueryArgs().Peek("type")) | 		typ = string(r.RequestCtx.QueryArgs().Peek("type")) | ||||||
| 	) | 	) | ||||||
| 	if typ == "" { | 	if typ == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid `type`.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`type`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	t, err := app.tmpl.GetAll(typ) | 	t, err := app.tmpl.GetAll(typ) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -32,8 +32,7 @@ func handleGetTemplate(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid template `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
| 	t, err := app.tmpl.Get(id) | 	t, err := app.tmpl.Get(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -49,12 +48,16 @@ func handleCreateTemplate(r *fastglue.Request) error { | |||||||
| 		req = models.Template{} | 		req = models.Template{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Bad request", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err := app.tmpl.Create(req); err != nil { | 	if req.Name == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	template, err := app.tmpl.Create(req) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(template) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateTemplate updates a template. | // handleUpdateTemplate updates a template. | ||||||
| @@ -69,12 +72,16 @@ func handleUpdateTemplate(r *fastglue.Request) error { | |||||||
| 			"Invalid template `id`.", nil, envelope.InputError) | 			"Invalid template `id`.", nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Bad request", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err = app.tmpl.Update(id, req); err != nil { | 	if req.Name == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  | 	updatedTemplate, err := app.tmpl.Update(id, req) | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(true) | 	return r.SendEnvelope(updatedTemplate) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteTemplate deletes a template. | // handleDeleteTemplate deletes a template. | ||||||
| @@ -89,7 +96,7 @@ func handleDeleteTemplate(r *fastglue.Request) error { | |||||||
| 			"Invalid template `id`.", nil, envelope.InputError) | 			"Invalid template `id`.", nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err := r.Decode(&req, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Bad request", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	if err = app.tmpl.Delete(id); err != nil { | 	if err = app.tmpl.Delete(id); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
|   | |||||||
| @@ -32,6 +32,10 @@ type migFunc struct { | |||||||
| var migList = []migFunc{ | var migList = []migFunc{ | ||||||
| 	{"v0.3.0", migrations.V0_3_0}, | 	{"v0.3.0", migrations.V0_3_0}, | ||||||
| 	{"v0.4.0", migrations.V0_4_0}, | 	{"v0.4.0", migrations.V0_4_0}, | ||||||
|  | 	{"v0.5.0", migrations.V0_5_0}, | ||||||
|  | 	{"v0.6.0", migrations.V0_6_0}, | ||||||
|  | 	{"v0.7.0", migrations.V0_7_0}, | ||||||
|  | 	{"v0.7.4", migrations.V0_7_4}, | ||||||
| } | } | ||||||
|  |  | ||||||
| // upgrade upgrades the database to the current version by running SQL migration files | // upgrade upgrades the database to the current version by running SQL migration files | ||||||
|   | |||||||
							
								
								
									
										588
									
								
								cmd/users.go
									
									
									
									
									
								
							
							
						
						
									
										588
									
								
								cmd/users.go
									
									
									
									
									
								
							| @@ -2,7 +2,7 @@ package main | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"mime/multipart" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"slices" | 	"slices" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| @@ -16,202 +16,201 @@ import ( | |||||||
| 	"github.com/abhinavxd/libredesk/internal/stringutil" | 	"github.com/abhinavxd/libredesk/internal/stringutil" | ||||||
| 	tmpl "github.com/abhinavxd/libredesk/internal/template" | 	tmpl "github.com/abhinavxd/libredesk/internal/template" | ||||||
| 	"github.com/abhinavxd/libredesk/internal/user/models" | 	"github.com/abhinavxd/libredesk/internal/user/models" | ||||||
|  | 	realip "github.com/ferluci/fast-realip" | ||||||
| 	"github.com/valyala/fasthttp" | 	"github.com/valyala/fasthttp" | ||||||
| 	"github.com/volatiletech/null/v9" | 	"github.com/volatiletech/null/v9" | ||||||
| 	"github.com/zerodha/fastglue" | 	"github.com/zerodha/fastglue" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
| 	maxAvatarSizeMB = 20 | 	maxAvatarSizeMB = 2 | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // handleGetUsers returns all users. | type updateAvailabilityRequest struct { | ||||||
| func handleGetUsers(r *fastglue.Request) error { | 	Status string `json:"status"` | ||||||
| 	var ( |  | ||||||
| 		app = r.Context.(*App) |  | ||||||
| 	) |  | ||||||
| 	agents, err := app.user.GetAll() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, "") |  | ||||||
| 	} |  | ||||||
| 	return r.SendEnvelope(agents) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetUsersCompact returns all users in a compact format. | type resetPasswordRequest struct { | ||||||
| func handleGetUsersCompact(r *fastglue.Request) error { | 	Email string `json:"email"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type setPasswordRequest struct { | ||||||
|  | 	Token    string `json:"token"` | ||||||
|  | 	Password string `json:"password"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type availabilityRequest struct { | ||||||
|  | 	Status string `json:"status"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type agentReq struct { | ||||||
|  | 	FirstName          string   `json:"first_name"` | ||||||
|  | 	LastName           string   `json:"last_name"` | ||||||
|  | 	Email              string   `json:"email"` | ||||||
|  | 	SendWelcomeEmail   bool     `json:"send_welcome_email"` | ||||||
|  | 	Teams              []string `json:"teams"` | ||||||
|  | 	Roles              []string `json:"roles"` | ||||||
|  | 	Enabled            bool     `json:"enabled"` | ||||||
|  | 	AvailabilityStatus string   `json:"availability_status"` | ||||||
|  | 	NewPassword        string   `json:"new_password,omitempty"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGetAgents returns all agents. | ||||||
|  | func handleGetAgents(r *fastglue.Request) error { | ||||||
| 	var app = r.Context.(*App) | 	var app = r.Context.(*App) | ||||||
| 	agents, err := app.user.GetAllCompact() | 	agents, err := app.user.GetAgents() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, "") | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(agents) | 	return r.SendEnvelope(agents) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetUser returns a user. | // handleGetAgentsCompact returns all agents in a compact format. | ||||||
| func handleGetUser(r *fastglue.Request) error { | func handleGetAgentsCompact(r *fastglue.Request) error { | ||||||
| 	var ( | 	var app = r.Context.(*App) | ||||||
| 		app = r.Context.(*App) | 	agents, err := app.user.GetAgentsCompact() | ||||||
| 	) |  | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) |  | ||||||
| 	if err != nil || id == 0 { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, |  | ||||||
| 			"Invalid user `id`.", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
| 	user, err := app.user.GetAgent(id) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(user) | 	return r.SendEnvelope(agents) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateUserAvailability updates the current user availability. | // handleGetAgent returns an agent. | ||||||
| func handleUpdateUserAvailability(r *fastglue.Request) error { | func handleGetAgent(r *fastglue.Request) error { | ||||||
| 	var ( | 	var app = r.Context.(*App) | ||||||
| 		app    = r.Context.(*App) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 		auser  = r.RequestCtx.UserValue("user").(amodels.User) | 	if err != nil || id <= 0 { | ||||||
| 		status = string(r.RequestCtx.PostArgs().Peek("status")) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 	) | 	} | ||||||
| 	if err := app.user.UpdateAvailability(auser.ID, status); err != nil { | 	agent, err := app.user.GetAgent(id, "") | ||||||
|  | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("User availability updated successfully.") | 	return r.SendEnvelope(agent) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetCurrentUserTeams returns the teams of a user. | // handleUpdateAgentAvailability updates the current agent availability. | ||||||
| func handleGetCurrentUserTeams(r *fastglue.Request) error { | func handleUpdateAgentAvailability(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app      = r.Context.(*App) | ||||||
|  | 		auser    = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		ip       = realip.FromRequest(r.RequestCtx) | ||||||
|  | 		availReq availabilityRequest | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	// Decode JSON request | ||||||
|  | 	if err := r.Decode(&availReq, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Fetch entire agent | ||||||
|  | 	agent, err := app.user.GetAgent(auser.ID, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Same status? | ||||||
|  | 	if agent.AvailabilityStatus == availReq.Status { | ||||||
|  | 		return r.SendEnvelope(agent) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Update availability status | ||||||
|  | 	if err := app.user.UpdateAvailability(auser.ID, availReq.Status); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Skip activity log if agent returns online from away (to avoid spam). | ||||||
|  | 	if !(agent.AvailabilityStatus == models.Away && availReq.Status == models.Online) { | ||||||
|  | 		if err := app.activityLog.UserAvailability(auser.ID, auser.Email, availReq.Status, ip, "", 0); err != nil { | ||||||
|  | 			app.lo.Error("error creating activity log", "error", err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Fetch updated agent and return | ||||||
|  | 	agent, err = app.user.GetAgent(auser.ID, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(agent) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGetCurrentAgentTeams returns the teams of current agent. | ||||||
|  | func handleGetCurrentAgentTeams(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	teams, err := app.team.GetUserTeams(auser.ID) | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	teams, err := app.team.GetUserTeams(user.ID) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(teams) | 	return r.SendEnvelope(teams) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateCurrentUser updates the current user. | // handleUpdateCurrentAgent updates the current agent. | ||||||
| func handleUpdateCurrentUser(r *fastglue.Request) error { | func handleUpdateCurrentAgent(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	form, err := r.RequestCtx.MultipartForm() | 	form, err := r.RequestCtx.MultipartForm() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error parsing form data", "error", err) | 		app.lo.Error("error parsing form data", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error parsing data", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	files, ok := form.File["files"] | 	files, ok := form.File["files"] | ||||||
|  |  | ||||||
| 	// Upload avatar? | 	// Upload avatar? | ||||||
| 	if ok && len(files) > 0 { | 	if ok && len(files) > 0 { | ||||||
| 		fileHeader := files[0] | 		agent, err := app.user.GetAgent(auser.ID, "") | ||||||
| 		file, err := fileHeader.Open() |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			app.lo.Error("error reading uploaded", "error", err) | 			return sendErrorEnvelope(r, err) | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error reading file", nil, envelope.GeneralError) |  | ||||||
| 		} | 		} | ||||||
| 		defer file.Close() | 		if err := uploadUserAvatar(r, agent, files); err != nil { | ||||||
|  |  | ||||||
| 		// Sanitize filename. |  | ||||||
| 		srcFileName := stringutil.SanitizeFilename(fileHeader.Filename) |  | ||||||
| 		srcContentType := fileHeader.Header.Get("Content-Type") |  | ||||||
| 		srcFileSize := fileHeader.Size |  | ||||||
| 		srcExt := strings.TrimPrefix(strings.ToLower(filepath.Ext(srcFileName)), ".") |  | ||||||
|  |  | ||||||
| 		if !slices.Contains(image.Exts, srcExt) { |  | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "File type is not an image", nil, envelope.InputError) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Check file size |  | ||||||
| 		if bytesToMegabytes(srcFileSize) > maxAvatarSizeMB { |  | ||||||
| 			app.lo.Error("error uploaded file size is larger than max allowed", "size", bytesToMegabytes(srcFileSize), "max_allowed", maxAvatarSizeMB) |  | ||||||
| 			return r.SendErrorEnvelope( |  | ||||||
| 				http.StatusRequestEntityTooLarge, |  | ||||||
| 				fmt.Sprintf("File size is too large. Please upload file lesser than %d MB", maxAvatarSizeMB), |  | ||||||
| 				nil, |  | ||||||
| 				envelope.GeneralError, |  | ||||||
| 			) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Reset ptr. |  | ||||||
| 		file.Seek(0, 0) |  | ||||||
| 		linkedModel := null.StringFrom(mmodels.ModelUser) |  | ||||||
| 		linkedID := null.IntFrom(user.ID) |  | ||||||
| 		disposition := null.NewString("", false) |  | ||||||
| 		contentID := "" |  | ||||||
| 		meta := []byte("{}") |  | ||||||
| 		media, err := app.media.UploadAndInsert(srcFileName, srcContentType, contentID, linkedModel, linkedID, file, int(srcFileSize), disposition, meta) |  | ||||||
| 		if err != nil { |  | ||||||
| 			app.lo.Error("error uploading file", "error", err) |  | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error uploading file", nil, envelope.GeneralError) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Delete current avatar. |  | ||||||
| 		if user.AvatarURL.Valid { |  | ||||||
| 			fileName := filepath.Base(user.AvatarURL.String) |  | ||||||
| 			app.media.Delete(fileName) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Save file path. |  | ||||||
| 		path, err := stringutil.GetPathFromURL(media.URL) |  | ||||||
| 		if err != nil { |  | ||||||
| 			app.lo.Debug("error getting path from URL", "url", media.URL, "error", err) |  | ||||||
| 			return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error uploading file", nil, envelope.GeneralError) |  | ||||||
| 		} |  | ||||||
| 		if err := app.user.UpdateAvatar(user.ID, path); err != nil { |  | ||||||
| 			return sendErrorEnvelope(r, err) | 			return sendErrorEnvelope(r, err) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("User updated successfully.") |  | ||||||
|  | 	// Fetch updated agent and return. | ||||||
|  | 	agent, err := app.user.GetAgent(auser.ID, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(agent) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleCreateUser creates a new user. | // handleCreateAgent creates a new agent. | ||||||
| func handleCreateUser(r *fastglue.Request) error { | func handleCreateAgent(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app  = r.Context.(*App) | 		app = r.Context.(*App) | ||||||
| 		user = models.User{} | 		req = agentReq{} | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&user, "json"); err != nil { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if user.Email.String == "" { | 	// Validate agent request | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty `email`", nil, envelope.InputError) | 	if err := validateAgentRequest(r, &req); err != nil { | ||||||
|  | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if user.Roles == nil { | 	agent, err := app.user.CreateAgent(req.FirstName, req.LastName, req.Email, req.Roles) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Please select at least one role", nil, envelope.InputError) | 	if err != nil { | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if user.FirstName == "" { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty `first_name`", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// Right now, only agents can be created. |  | ||||||
| 	if err := app.user.CreateAgent(&user); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Upsert user teams. | 	// Upsert user teams. | ||||||
| 	if err := app.team.UpsertUserTeams(user.ID, user.Teams.Names()); err != nil { | 	if len(req.Teams) > 0 { | ||||||
| 		return sendErrorEnvelope(r, err) | 		app.team.UpsertUserTeams(agent.ID, req.Teams) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if user.SendWelcomeEmail { | 	if req.SendWelcomeEmail { | ||||||
| 		// Generate reset token. | 		// Generate reset token. | ||||||
| 		resetToken, err := app.user.SetResetPasswordToken(user.ID) | 		resetToken, err := app.user.SetResetPasswordToken(agent.ID) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return sendErrorEnvelope(r, err) | 			return sendErrorEnvelope(r, err) | ||||||
| 		} | 		} | ||||||
| @@ -219,80 +218,106 @@ func handleCreateUser(r *fastglue.Request) error { | |||||||
| 		// Render template and send email. | 		// Render template and send email. | ||||||
| 		content, err := app.tmpl.RenderInMemoryTemplate(tmpl.TmplWelcome, map[string]any{ | 		content, err := app.tmpl.RenderInMemoryTemplate(tmpl.TmplWelcome, map[string]any{ | ||||||
| 			"ResetToken": resetToken, | 			"ResetToken": resetToken, | ||||||
| 			"Email":      user.Email.String, | 			"Email":      req.Email, | ||||||
| 		}) | 		}) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			app.lo.Error("error rendering template", "error", err) | 			app.lo.Error("error rendering template", "error", err) | ||||||
| 			return r.SendEnvelope("User created successfully, but error rendering welcome email.") |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if err := app.notifier.Send(notifier.Message{ | 		if err := app.notifier.Send(notifier.Message{ | ||||||
| 			UserIDs:  []int{user.ID}, | 			RecipientEmails: []string{req.Email}, | ||||||
| 			Subject:  "Welcome", | 			Subject:         app.i18n.T("globals.messages.welcomeToLibredesk"), | ||||||
| 			Content:  content, | 			Content:         content, | ||||||
| 			Provider: notifier.ProviderEmail, | 			Provider:        notifier.ProviderEmail, | ||||||
| 		}); err != nil { | 		}); err != nil { | ||||||
| 			app.lo.Error("error sending notification message", "error", err) | 			app.lo.Error("error sending notification message", "error", err) | ||||||
| 			return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, "User created successfully, but could not send welcome email.", nil)) |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("User created successfully.") |  | ||||||
|  | 	// Refetch agent as other details might've changed. | ||||||
|  | 	agent, err = app.user.GetAgent(agent.ID, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(agent) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateUser updates a user. | // handleUpdateAgent updates an agent. | ||||||
| func handleUpdateUser(r *fastglue.Request) error { | func handleUpdateAgent(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app  = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		user = models.User{} | 		req   = agentReq{} | ||||||
|  | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
|  | 		ip    = realip.FromRequest(r.RequestCtx) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	if id == 0 { | ||||||
| 	if err != nil || id == 0 { | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`id`"), nil, envelope.InputError) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, |  | ||||||
| 			"Invalid user `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&user, "json"); err != nil { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if user.Email.String == "" { | 	// Validate agent request | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty `email`", nil, envelope.InputError) | 	if err := validateAgentRequest(r, &req); err != nil { | ||||||
|  | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if user.Roles == nil { | 	agent, err := app.user.GetAgent(id, "") | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Please select at least one role", nil, envelope.InputError) | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	oldAvailabilityStatus := agent.AvailabilityStatus | ||||||
|  |  | ||||||
| 	if user.FirstName == "" { | 	// Update agent with individual fields | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty `first_name`", nil, envelope.InputError) | 	if err = app.user.UpdateAgent(id, req.FirstName, req.LastName, req.Email, req.Roles, req.Enabled, req.AvailabilityStatus, req.NewPassword); err != nil { | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// Update user. |  | ||||||
| 	if err = app.user.Update(id, user); err != nil { |  | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Upsert user teams. | 	// Invalidate authz cache. | ||||||
| 	if err := app.team.UpsertUserTeams(id, user.Teams.Names()); err != nil { | 	defer app.authz.InvalidateUserCache(id) | ||||||
|  |  | ||||||
|  | 	// Create activity log if user availability status changed. | ||||||
|  | 	if oldAvailabilityStatus != req.AvailabilityStatus { | ||||||
|  | 		if err := app.activityLog.UserAvailability(auser.ID, auser.Email, req.AvailabilityStatus, ip, req.Email, id); err != nil { | ||||||
|  | 			app.lo.Error("error creating activity log", "error", err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Upsert agent teams. | ||||||
|  | 	if err := app.team.UpsertUserTeams(id, req.Teams); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope("User updated successfully.") | 	// Refetch agent and return. | ||||||
|  | 	agent, err = app.user.GetAgent(id, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(agent) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteUser soft deletes a user. | // handleDeleteAgent soft deletes an agent. | ||||||
| func handleDeleteUser(r *fastglue.Request) error { | func handleDeleteAgent(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app     = r.Context.(*App) | 		app     = r.Context.(*App) | ||||||
| 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 		id, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 		auser   = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "{globals.terms.user} `id`"), nil, envelope.InputError) | ||||||
| 			"Invalid user `id`.", nil, envelope.InputError) | 	} | ||||||
|  |  | ||||||
|  | 	// Disallow if self-deleting. | ||||||
|  | 	if id == auser.ID { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("user.userCannotDeleteSelf"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Soft delete user. | 	// Soft delete user. | ||||||
| 	if err = app.user.SoftDelete(id); err != nil { | 	if err = app.user.SoftDeleteAgent(id); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -301,76 +326,80 @@ func handleDeleteUser(r *fastglue.Request) error { | |||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope("User deleted successfully.") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetCurrentUser returns the current logged in user. | // handleGetCurrentAgent returns the current logged in agent. | ||||||
| func handleGetCurrentUser(r *fastglue.Request) error { | func handleGetCurrentAgent(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	u, err := app.user.GetAgent(auser.ID) | 	u, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope(u) | 	return r.SendEnvelope(u) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleDeleteAvatar deletes a user avatar. | // handleDeleteCurrentAgentAvatar deletes the current agent's avatar. | ||||||
| func handleDeleteAvatar(r *fastglue.Request) error { | func handleDeleteCurrentAgentAvatar(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	// Get user | 	// Get user | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	agent, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Valid str? | 	// Valid str? | ||||||
| 	if user.AvatarURL.String == "" { | 	if agent.AvatarURL.String == "" { | ||||||
| 		return r.SendEnvelope("Avatar deleted successfully.") | 		return r.SendEnvelope(true) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	fileName := filepath.Base(user.AvatarURL.String) | 	fileName := filepath.Base(agent.AvatarURL.String) | ||||||
|  |  | ||||||
| 	// Delete file from the store. | 	// Delete file from the store. | ||||||
| 	if err := app.media.Delete(fileName); err != nil { | 	if err := app.media.Delete(fileName); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.user.UpdateAvatar(user.ID, ""); err != nil { | 	if err = app.user.UpdateAvatar(agent.ID, ""); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("Avatar deleted successfully.") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleResetPassword generates a reset password token and sends an email to the user. | // handleResetPassword generates a reset password token and sends an email to the agent. | ||||||
| func handleResetPassword(r *fastglue.Request) error { | func handleResetPassword(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app       = r.Context.(*App) | 		app       = r.Context.(*App) | ||||||
| 		p         = r.RequestCtx.PostArgs() |  | ||||||
| 		auser, ok = r.RequestCtx.UserValue("user").(amodels.User) | 		auser, ok = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		email     = string(p.Peek("email")) | 		resetReq  resetPasswordRequest | ||||||
| 	) | 	) | ||||||
| 	if ok && auser.ID > 0 { | 	if ok && auser.ID > 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "User is already logged in, Please logout to reset password.", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("user.userAlreadyLoggedIn"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if email == "" { | 	// Decode JSON request | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty `email`", nil, envelope.InputError) | 	if err := r.Decode(&resetReq, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgentByEmail(email) | 	if resetReq.Email == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`email`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	agent, err := app.user.GetAgent(0, resetReq.Email) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		// Send 200 even if user not found, to prevent email enumeration. | 		// Send 200 even if user not found, to prevent email enumeration. | ||||||
| 		return r.SendEnvelope("Reset password email sent successfully.") | 		return r.SendEnvelope(true) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	token, err := app.user.SetResetPasswordToken(user.ID) | 	token, err := app.user.SetResetPasswordToken(agent.ID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -381,43 +410,194 @@ func handleResetPassword(r *fastglue.Request) error { | |||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		app.lo.Error("error rendering template", "error", err) | 		app.lo.Error("error rendering template", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error rendering template", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("globals.messages.errorSendingPasswordResetEmail"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := app.notifier.Send(notifier.Message{ | 	if err := app.notifier.Send(notifier.Message{ | ||||||
| 		UserIDs:  []int{user.ID}, | 		RecipientEmails: []string{agent.Email.String}, | ||||||
| 		Subject:  "Reset Password", | 		Subject:         "Reset Password", | ||||||
| 		Content:  content, | 		Content:         content, | ||||||
| 		Provider: notifier.ProviderEmail, | 		Provider:        notifier.ProviderEmail, | ||||||
| 	}); err != nil { | 	}); err != nil { | ||||||
| 		app.lo.Error("error sending password reset email", "error", err) | 		app.lo.Error("error sending password reset email", "error", err) | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, "Error sending password reset email", nil, envelope.GeneralError) | 		return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("globals.messages.errorSendingPasswordResetEmail"), nil, envelope.GeneralError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope("Reset password email sent successfully.") | 	return r.SendEnvelope(true) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleSetPassword resets the password with the provided token. | // handleSetPassword resets the password with the provided token. | ||||||
| func handleSetPassword(r *fastglue.Request) error { | func handleSetPassword(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app      = r.Context.(*App) | 		app       = r.Context.(*App) | ||||||
| 		user, ok = r.RequestCtx.UserValue("user").(amodels.User) | 		agent, ok = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 		p        = r.RequestCtx.PostArgs() | 		req       setPasswordRequest | ||||||
| 		password = string(p.Peek("password")) |  | ||||||
| 		token    = string(p.Peek("token")) |  | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| 	if ok && user.ID > 0 { | 	if ok && agent.ID > 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "User is already logged in", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("user.userAlreadyLoggedIn"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if password == "" { | 	if err := r.Decode(&req, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty `password`", nil, envelope.InputError) | 		return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := app.user.ResetPassword(token, password); err != nil { | 	if req.Password == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "{globals.terms.password}"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := app.user.ResetPassword(req.Token, req.Password); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.SendEnvelope("Password reset successfully.") | 	return r.SendEnvelope(true) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // uploadUserAvatar uploads the user avatar. | ||||||
|  | func uploadUserAvatar(r *fastglue.Request, user models.User, files []*multipart.FileHeader) error { | ||||||
|  | 	var app = r.Context.(*App) | ||||||
|  |  | ||||||
|  | 	fileHeader := files[0] | ||||||
|  | 	file, err := fileHeader.Open() | ||||||
|  | 	if err != nil { | ||||||
|  | 		app.lo.Error("error opening uploaded file", "user_id", user.ID, "error", err) | ||||||
|  | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorReading", "name", "{globals.terms.file}"), nil) | ||||||
|  | 	} | ||||||
|  | 	defer file.Close() | ||||||
|  |  | ||||||
|  | 	// Sanitize filename. | ||||||
|  | 	srcFileName := stringutil.SanitizeFilename(fileHeader.Filename) | ||||||
|  | 	srcContentType := fileHeader.Header.Get("Content-Type") | ||||||
|  | 	srcFileSize := fileHeader.Size | ||||||
|  | 	srcExt := strings.TrimPrefix(strings.ToLower(filepath.Ext(srcFileName)), ".") | ||||||
|  |  | ||||||
|  | 	if !slices.Contains(image.Exts, srcExt) { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.T("globals.messages.fileTypeisNotAnImage"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Check file size | ||||||
|  | 	if bytesToMegabytes(srcFileSize) > maxAvatarSizeMB { | ||||||
|  | 		app.lo.Error("error uploaded file size is larger than max allowed", "user_id", user.ID, "size", bytesToMegabytes(srcFileSize), "max_allowed", maxAvatarSizeMB) | ||||||
|  | 		return envelope.NewError( | ||||||
|  | 			envelope.InputError, | ||||||
|  | 			app.i18n.Ts("media.fileSizeTooLarge", "size", fmt.Sprintf("%dMB", maxAvatarSizeMB)), | ||||||
|  | 			nil, | ||||||
|  | 		) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Reset ptr. | ||||||
|  | 	file.Seek(0, 0) | ||||||
|  | 	linkedModel := null.StringFrom(mmodels.ModelUser) | ||||||
|  | 	linkedID := null.IntFrom(user.ID) | ||||||
|  | 	disposition := null.NewString("", false) | ||||||
|  | 	contentID := "" | ||||||
|  | 	meta := []byte("{}") | ||||||
|  | 	media, err := app.media.UploadAndInsert(srcFileName, srcContentType, contentID, linkedModel, linkedID, file, int(srcFileSize), disposition, meta) | ||||||
|  | 	if err != nil { | ||||||
|  | 		app.lo.Error("error uploading file", "user_id", user.ID, "error", err) | ||||||
|  | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorUploading", "name", "{globals.terms.file}"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Delete current avatar. | ||||||
|  | 	if user.AvatarURL.Valid { | ||||||
|  | 		fileName := filepath.Base(user.AvatarURL.String) | ||||||
|  | 		if err := app.media.Delete(fileName); err != nil { | ||||||
|  | 			app.lo.Error("error deleting user avatar", "user_id", user.ID, "error", err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Save file path. | ||||||
|  | 	path, err := stringutil.GetPathFromURL(media.URL) | ||||||
|  | 	if err != nil { | ||||||
|  | 		app.lo.Debug("error getting path from URL", "user_id", user.ID, "url", media.URL, "error", err) | ||||||
|  | 		return envelope.NewError(envelope.GeneralError, app.i18n.Ts("globals.messages.errorUploading", "name", "{globals.terms.file}"), nil) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := app.user.UpdateAvatar(user.ID, path); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGenerateAPIKey generates a new API key for a user | ||||||
|  | func handleGenerateAPIKey(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Check if user exists | ||||||
|  | 	user, err := app.user.GetAgent(id, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Generate API key and secret | ||||||
|  | 	apiKey, apiSecret, err := app.user.GenerateAPIKey(user.ID) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Return the API key and secret (only shown once) | ||||||
|  | 	response := struct { | ||||||
|  | 		APIKey    string `json:"api_key"` | ||||||
|  | 		APISecret string `json:"api_secret"` | ||||||
|  | 	}{ | ||||||
|  | 		APIKey:    apiKey, | ||||||
|  | 		APISecret: apiSecret, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(response) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleRevokeAPIKey revokes a user's API key | ||||||
|  | func handleRevokeAPIKey(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Check if user exists | ||||||
|  | 	_, err := app.user.GetAgent(id, "") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Revoke API key | ||||||
|  | 	if err := app.user.RevokeAPIKey(id); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // validateAgentRequest validates common agent request fields and normalizes the email | ||||||
|  | func validateAgentRequest(r *fastglue.Request, req *agentReq) error { | ||||||
|  | 	var app = r.Context.(*App) | ||||||
|  |  | ||||||
|  | 	// Normalize email | ||||||
|  | 	req.Email = strings.TrimSpace(strings.ToLower(req.Email)) | ||||||
|  | 	if req.Email == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`email`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if !stringutil.ValidEmail(req.Email) { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`email`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if req.Roles == nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`role`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if req.FirstName == "" { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`first_name`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										65
									
								
								cmd/views.go
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								cmd/views.go
									
									
									
									
									
								
							| @@ -16,7 +16,7 @@ func handleGetUserViews(r *fastglue.Request) error { | |||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| @@ -35,61 +35,50 @@ func handleCreateUserView(r *fastglue.Request) error { | |||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	if err := r.Decode(&view, "json"); err != nil { | 	if err := r.Decode(&view, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
| 	user, err := app.user.GetAgent(auser.ID) | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	if view.Name == "" { | 	if view.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty view `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`Name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if string(view.Filters) == "" { | 	if string(view.Filters) == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Please provide at least one filter", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`Filters`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  | 	createdView, err := app.view.Create(view.Name, view.Filters, user.ID) | ||||||
| 	if err := app.view.Create(view.Name, view.Filters, user.ID); err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
| 	return r.SendEnvelope("View created successfully") | 	return r.SendEnvelope(createdView) | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleGetUserView deletes a view for a user. | // handleDeleteUserView deletes a view for a user. | ||||||
| func handleDeleteUserView(r *fastglue.Request) error { | func handleDeleteUserView(r *fastglue.Request) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		app   = r.Context.(*App) | 		app   = r.Context.(*App) | ||||||
| 		auser = r.RequestCtx.UserValue("user").(amodels.User) | 		auser = r.RequestCtx.UserValue("user").(amodels.User) | ||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id <= 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid view `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	if id <= 0 { |  | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty view `ID`", nil, envelope.InputError) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	user, err := app.user.GetAgent(auser.ID) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	view, err := app.view.Get(id) | 	view, err := app.view.Get(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if view.UserID != user.ID { | 	if view.UserID != user.ID { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, "Forbidden", nil, envelope.PermissionError) | 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.Ts("globals.messages.denied", "name", "{globals.terms.permission}"), nil, envelope.PermissionError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = app.view.Delete(id); err != nil { | 	if err = app.view.Delete(id); err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
| 	return r.SendEnvelope("View deleted successfully") |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // handleUpdateUserView updates a view for a user. | // handleUpdateUserView updates a view for a user. | ||||||
| @@ -101,39 +90,31 @@ func handleUpdateUserView(r *fastglue.Request) error { | |||||||
| 	) | 	) | ||||||
| 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | 	id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
| 	if err != nil || id == 0 { | 	if err != nil || id == 0 { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
| 			"Invalid view `id`.", nil, envelope.InputError) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := r.Decode(&view, "json"); err != nil { | 	if err := r.Decode(&view, "json"); err != nil { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "decode failed", err.Error(), envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  | 	user, err := app.user.GetAgent(auser.ID, "") | ||||||
| 	user, err := app.user.GetAgent(auser.ID) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if view.Name == "" { | 	if view.Name == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty view `Name`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if string(view.Filters) == "" { | 	if string(view.Filters) == "" { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Empty view `Filter`", nil, envelope.InputError) | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`filters`"), nil, envelope.InputError) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	v, err := app.view.Get(id) | 	v, err := app.view.Get(id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if v.UserID != user.ID { | 	if v.UserID != user.ID { | ||||||
| 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, "Forbidden", nil, envelope.PermissionError) | 		return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.Ts("globals.messages.denied", "name", "{globals.terms.permission}"), nil, envelope.PermissionError) | ||||||
| 	} | 	} | ||||||
|  | 	updatedView, err := app.view.Update(id, view.Name, view.Filters) | ||||||
| 	if err = app.view.Update(id, view.Name, view.Filters); err != nil { | 	if err != nil { | ||||||
| 		return sendErrorEnvelope(r, err) | 		return sendErrorEnvelope(r, err) | ||||||
| 	} | 	} | ||||||
|  | 	return r.SendEnvelope(updatedView) | ||||||
| 	return r.SendEnvelope(true) |  | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										191
									
								
								cmd/webhooks.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										191
									
								
								cmd/webhooks.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,191 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/envelope" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/stringutil" | ||||||
|  | 	"github.com/abhinavxd/libredesk/internal/webhook/models" | ||||||
|  | 	"github.com/valyala/fasthttp" | ||||||
|  | 	"github.com/zerodha/fastglue" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // handleGetWebhooks returns all webhooks from the database. | ||||||
|  | func handleGetWebhooks(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app = r.Context.(*App) | ||||||
|  | 	) | ||||||
|  | 	webhooks, err := app.webhook.GetAll() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  | 	// Hide secrets. | ||||||
|  | 	for i := range webhooks { | ||||||
|  | 		if webhooks[i].Secret != "" { | ||||||
|  | 			webhooks[i].Secret = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return r.SendEnvelope(webhooks) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleGetWebhook returns a specific webhook by ID. | ||||||
|  | func handleGetWebhook(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	webhook, err := app.webhook.Get(id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Hide secret in the response. | ||||||
|  | 	if webhook.Secret != "" { | ||||||
|  | 		webhook.Secret = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(webhook) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleCreateWebhook creates a new webhook in the database. | ||||||
|  | func handleCreateWebhook(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app     = r.Context.(*App) | ||||||
|  | 		webhook = models.Webhook{} | ||||||
|  | 	) | ||||||
|  | 	if err := r.Decode(&webhook, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Validate webhook fields | ||||||
|  | 	if err := validateWebhook(app, webhook); err != nil { | ||||||
|  | 		return r.SendEnvelope(err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	webhook, err := app.webhook.Create(webhook) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Clear secret before returning | ||||||
|  | 	webhook.Secret = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(webhook) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleUpdateWebhook updates an existing webhook in the database. | ||||||
|  | func handleUpdateWebhook(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app     = r.Context.(*App) | ||||||
|  | 		webhook = models.Webhook{} | ||||||
|  | 		id, _   = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := r.Decode(&webhook, "json"); err != nil { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Validate webhook fields | ||||||
|  | 	if err := validateWebhook(app, webhook); err != nil { | ||||||
|  | 		return r.SendEnvelope(err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// If secret is empty or contains dummy characters, fetch existing webhook and preserve the secret | ||||||
|  | 	if webhook.Secret == "" || strings.Contains(webhook.Secret, stringutil.PasswordDummy) { | ||||||
|  | 		existingWebhook, err := app.webhook.Get(id) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return sendErrorEnvelope(r, err) | ||||||
|  | 		} | ||||||
|  | 		webhook.Secret = existingWebhook.Secret | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	updatedWebhook, err := app.webhook.Update(id, webhook) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Clear secret before returning | ||||||
|  | 	updatedWebhook.Secret = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(updatedWebhook) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleDeleteWebhook deletes a webhook from the database. | ||||||
|  | func handleDeleteWebhook(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := app.webhook.Delete(id); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleToggleWebhook toggles the active status of a webhook. | ||||||
|  | func handleToggleWebhook(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	toggledWebhook, err := app.webhook.Toggle(id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Clear secret before returning | ||||||
|  | 	toggledWebhook.Secret = strings.Repeat(stringutil.PasswordDummy, 10) | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(toggledWebhook) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // handleTestWebhook sends a test payload to a webhook. | ||||||
|  | func handleTestWebhook(r *fastglue.Request) error { | ||||||
|  | 	var ( | ||||||
|  | 		app   = r.Context.(*App) | ||||||
|  | 		id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string)) | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	if id <= 0 { | ||||||
|  | 		return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := app.webhook.SendTestWebhook(id); err != nil { | ||||||
|  | 		return sendErrorEnvelope(r, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return r.SendEnvelope(true) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // validateWebhook validates the webhook data. | ||||||
|  | func validateWebhook(app *App, webhook models.Webhook) error { | ||||||
|  | 	if webhook.Name == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if webhook.URL == "" { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`url`"), nil) | ||||||
|  | 	} | ||||||
|  | 	if len(webhook.Events) == 0 { | ||||||
|  | 		return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`events`"), nil) | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
| @@ -1,76 +1,124 @@ | |||||||
| # App. |  | ||||||
| [app] | [app] | ||||||
|  | # Log level: info, debug, warn, error, fatal | ||||||
| log_level = "debug" | log_level = "debug" | ||||||
|  | # Environment: dev, prod. | ||||||
|  | # Setting to "dev" will enable color logging in terminal. | ||||||
| env = "dev" | env = "dev" | ||||||
|  | # Whether to automatically check for application updates on start up, app updates are shown as a banner in the admin panel. | ||||||
| check_updates = true | check_updates = true | ||||||
|  |  | ||||||
| # HTTP server. | # HTTP server. | ||||||
| [app.server] | [app.server] | ||||||
|  | # Address to bind the HTTP server to. | ||||||
| address = "0.0.0.0:9000" | address = "0.0.0.0:9000" | ||||||
|  | # Unix socket path (leave empty to use TCP address instead) | ||||||
| socket = "" | socket = "" | ||||||
|  | # Do NOT disable secure cookies in production environment if you don't know exactly what you're doing! | ||||||
|  | disable_secure_cookies = false | ||||||
|  | # Request read and write timeouts. | ||||||
| read_timeout = "5s" | read_timeout = "5s" | ||||||
| write_timeout = "5s" | write_timeout = "5s" | ||||||
| max_body_size = 500000000 | # Maximum request body size in bytes (100MB) | ||||||
|  | # If you are using proxy, you may need to configure them to allow larger request bodies. | ||||||
|  | max_body_size = 104857600 | ||||||
|  | # Size of the read buffer for incoming requests | ||||||
|  | read_buffer_size = 4096 | ||||||
|  | # Keepalive settings. | ||||||
| keepalive_timeout = "10s" | keepalive_timeout = "10s" | ||||||
|  |  | ||||||
| # File upload provider to use, either `fs` or `s3`. | # File upload provider to use, either `fs` or `s3`. | ||||||
| [upload] | [upload] | ||||||
| provider = "fs" | provider = "fs" | ||||||
|  |  | ||||||
| # Filesytem provider. | # Filesystem provider. | ||||||
| [upload.fs] | [upload.fs] | ||||||
|  | # Directory where uploaded files are stored, make sure this directory exists and is writable by the application. | ||||||
| upload_path = 'uploads' | upload_path = 'uploads' | ||||||
|  |  | ||||||
| # S3 provider. | # S3 provider. | ||||||
| [upload.s3] | [upload.s3] | ||||||
|  | # S3 endpoint URL (required only for non-AWS S3-compatible providers like MinIO). | ||||||
|  | # Leave empty to use default AWS endpoints. | ||||||
| url = "" | url = "" | ||||||
|  |  | ||||||
|  | # AWS S3 credentials, keep empty to use attached IAM roles. | ||||||
| access_key = "" | access_key = "" | ||||||
| secret_key = "" | secret_key = "" | ||||||
|  |  | ||||||
|  | # AWS region, e.g., "us-east-1", "eu-west-1", etc. | ||||||
| region = "ap-south-1" | region = "ap-south-1" | ||||||
| bucket = "bucket" | # S3 bucket name where files will be stored. | ||||||
|  | bucket = "bucket-name" | ||||||
|  | # Optional prefix path within the S3 bucket where files will be stored. | ||||||
|  | # Example, if set to "uploads/media", files will be stored under that path. | ||||||
|  | # Useful for organizing files inside a shared bucket. | ||||||
| bucket_path = "" | bucket_path = "" | ||||||
| expiry = "6h" | # S3 signed URL expiry duration (e.g., "30m", "1h") | ||||||
|  | expiry = "30m" | ||||||
|  |  | ||||||
| # Postgres. | # Postgres. | ||||||
| [db] | [db] | ||||||
| # If using docker compose, use the service name as the host. e.g. db | # If running locally, use `localhost`. | ||||||
| host = "127.0.0.1" | host = "db" | ||||||
|  | # Database port, default is 5432. | ||||||
| port = 5432 | port = 5432 | ||||||
| # Update the following values with your database credentials. | # Update the following values with your database credentials. | ||||||
| user = "libredesk" | user = "libredesk" | ||||||
| password = "libredesk" | password = "libredesk" | ||||||
| database = "libredesk" | database = "libredesk" | ||||||
| ssl_mode = "disable" | ssl_mode = "disable" | ||||||
|  | # Maximum number of open database connections | ||||||
| max_open = 30 | max_open = 30 | ||||||
|  | # Maximum number of idle connections in the pool | ||||||
| max_idle = 30 | max_idle = 30 | ||||||
|  | # Maximum time a connection can be reused before being closed | ||||||
| max_lifetime = "300s" | max_lifetime = "300s" | ||||||
|  |  | ||||||
| # Redis. | # Redis. | ||||||
| [redis] | [redis] | ||||||
| # If using docker compose, use the service name as the host. e.g. redis:6379 | # If running locally, use `localhost:6379`. | ||||||
| address = "127.0.0.1:6379" | address = "redis:6379" | ||||||
| password = "" | password = "" | ||||||
| db = 0 | db = 0 | ||||||
|  |  | ||||||
| [message] | [message] | ||||||
|  | # Number of workers processing outgoing message queue | ||||||
| outgoing_queue_workers = 10 | outgoing_queue_workers = 10 | ||||||
|  | # Number of workers processing incoming message queue | ||||||
| incoming_queue_workers = 10 | incoming_queue_workers = 10 | ||||||
| message_outoing_scan_interval = "50ms" | # How often to scan for outgoing messages to process, keep it low to process messages quickly. | ||||||
|  | message_outgoing_scan_interval = "50ms" | ||||||
|  | # Maximum number of messages that can be queued for incoming processing | ||||||
| incoming_queue_size = 5000 | incoming_queue_size = 5000 | ||||||
|  | # Maximum number of messages that can be queued for outgoing processing | ||||||
| outgoing_queue_size = 5000 | outgoing_queue_size = 5000 | ||||||
|  |  | ||||||
| [notification] | [notification] | ||||||
|  | # Number of concurrent notification workers | ||||||
| concurrency = 2 | concurrency = 2 | ||||||
|  | # Maximum number of notifications that can be queued | ||||||
| queue_size = 2000 | queue_size = 2000 | ||||||
|  |  | ||||||
| [automation] | [automation] | ||||||
|  | # Number of workers processing automation rules | ||||||
| worker_count = 10 | worker_count = 10 | ||||||
|  |  | ||||||
| [autoassigner] | [autoassigner] | ||||||
|  | # How often to run automatic conversation assignment | ||||||
| autoassign_interval = "5m" | autoassign_interval = "5m" | ||||||
|  |  | ||||||
|  | [webhook] | ||||||
|  | # Number of webhook delivery workers | ||||||
|  | workers = 5 | ||||||
|  | # Maximum number of webhook deliveries that can be queued | ||||||
|  | queue_size = 10000 | ||||||
|  | # HTTP timeout for webhook requests | ||||||
|  | timeout = "15s" | ||||||
|  |  | ||||||
| [conversation] | [conversation] | ||||||
|  | # How often to check for conversations to unsnooze | ||||||
| unsnooze_interval = "5m" | unsnooze_interval = "5m" | ||||||
|  |  | ||||||
| [sla] | [sla] | ||||||
|  | # How often to evaluate SLA compliance for conversations | ||||||
| evaluation_interval = "5m" | evaluation_interval = "5m" | ||||||
|   | |||||||
| @@ -28,14 +28,15 @@ services: | |||||||
|     networks: |     networks: | ||||||
|       - libredesk |       - libredesk | ||||||
|     ports: |     ports: | ||||||
|       - "5432:5432" |       # Only bind on the local interface. To connect to Postgres externally, change this to 0.0.0.0 | ||||||
|  |       - "127.0.0.1:5432:5432" | ||||||
|     environment: |     environment: | ||||||
|       # Set these environment variables to configure the database, defaults to libredesk. |       # Set these environment variables to configure the database, defaults to libredesk. | ||||||
|       POSTGRES_USER: ${POSTGRES_USER:-libredesk} |       POSTGRES_USER: ${POSTGRES_USER:-libredesk} | ||||||
|       POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-libredesk}  |       POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-libredesk}  | ||||||
|       POSTGRES_DB: ${POSTGRES_DB:-libredesk} |       POSTGRES_DB: ${POSTGRES_DB:-libredesk} | ||||||
|     healthcheck: |     healthcheck: | ||||||
|       test: ["CMD-SHELL", "pg_isready -U libredesk"] |       test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-libredesk} -d ${POSTGRES_DB:-libredesk}"] | ||||||
|       interval: 10s |       interval: 10s | ||||||
|       timeout: 5s |       timeout: 5s | ||||||
|       retries: 6 |       retries: 6 | ||||||
| @@ -48,7 +49,8 @@ services: | |||||||
|     container_name: libredesk_redis |     container_name: libredesk_redis | ||||||
|     restart: unless-stopped |     restart: unless-stopped | ||||||
|     ports: |     ports: | ||||||
|       - "6379:6379" |       # Only bind on the local interface. | ||||||
|  |       - "127.0.0.1:6379:6379" | ||||||
|     networks: |     networks: | ||||||
|       - libredesk |       - libredesk | ||||||
|     volumes: |     volumes: | ||||||
| @@ -59,4 +61,4 @@ networks: | |||||||
|  |  | ||||||
| volumes: | volumes: | ||||||
|   postgres-data: |   postgres-data: | ||||||
|   redis-data: |   redis-data: | ||||||
|   | |||||||
| @@ -1,31 +0,0 @@ | |||||||
| # Developer Setup |  | ||||||
|  |  | ||||||
| Libredesk is a monorepo with a Go backend and a Vue.js frontend. The frontend uses Shadcn for UI components. |  | ||||||
|  |  | ||||||
| ### Pre-requisites |  | ||||||
|  |  | ||||||
| - `go` |  | ||||||
| - `nodejs` (if you are working on the frontend) and `pnpm` |  | ||||||
| - Postgres database (>= 13) |  | ||||||
|  |  | ||||||
| ### First time setup |  | ||||||
|  |  | ||||||
| Clone the repository: |  | ||||||
|  |  | ||||||
| ```sh |  | ||||||
| git clone https://github.com/abhinavxd/libredesk.git |  | ||||||
| ``` |  | ||||||
|  |  | ||||||
| 1. Copy `config.toml.sample` as `config.toml` and add your config. |  | ||||||
| 2. Run `make` to build the libredesk binary. Once the binary is built, run `./libredesk --install` to run the DB setup and set the System user password. |  | ||||||
|  |  | ||||||
| ### Running the Dev Environment |  | ||||||
|  |  | ||||||
| 1. Run `make run-backend` to start the libredesk backend dev server on `:9000`. |  | ||||||
| 2. Run `make run-frontend` to start the Vue frontend in dev mode using pnpm on `:8000`. Requests are proxied to the backend running on `:9000` check `vite.config.js` for the proxy config. |  | ||||||
|  |  | ||||||
| --- |  | ||||||
|  |  | ||||||
| # Production Build |  | ||||||
|  |  | ||||||
| Run `make` to build the Go binary, build the Javascript frontend, and embed the static assets producing a single self-contained binary, `libredesk`. |  | ||||||
| @@ -1,13 +0,0 @@ | |||||||
| # Introduction |  | ||||||
|  |  | ||||||
| Libredesk is an open source, self-hosted customer support desk. Single binary app. |  | ||||||
|  |  | ||||||
|  |  | ||||||
| <div style="border: 1px solid #ccc; padding: 1px; border-radius:5px; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1); background-color: #fff;"> |  | ||||||
|     <a href="https://libredesk.io"> |  | ||||||
|         <img src="https://hebbkx1anhila5yf.public.blob.vercel-storage.com/image-HvmxvOkalQSLp4qVezdTXaCd3dB4Rm.png" alt="libredesk screenshot" style="display: block; margin: 0 auto;"> |  | ||||||
|     </a> |  | ||||||
| </div> |  | ||||||
|  |  | ||||||
| ## Developers |  | ||||||
| Libredesk is a free and open source software licensed under AGPLv3. If you are interested in contributing, check out the [GitHub repository](https://github.com/abhinavxd/libredesk) and refer to the [developer setup](developer-setup.md). The backend is written in Go and the frontend is Vue js 3 with Shadcn for UI components. |  | ||||||
| @@ -1,48 +0,0 @@ | |||||||
| # Installation |  | ||||||
|  |  | ||||||
| Libredesk is a single binary application that requires postgres and redis to run. You can install it using the binary or docker. |  | ||||||
|  |  | ||||||
| ## Binary |  | ||||||
|  |  | ||||||
| 1. Download the [latest release](https://github.com/abhinavxd/libredesk/releases) and extract the libredesk binary. |  | ||||||
| 2. `./libredesk --install` to install the tables in the Postgres DB (⩾ 13) and set the System user password. |  | ||||||
| 3. Run `./libredesk` and visit `http://localhost:9000` and login with the email `System` and the password you set during installation. |  | ||||||
|  |  | ||||||
| !!! Tip |  | ||||||
|     To set the System user password during installation, set the environment variables: |  | ||||||
|     `LIBREDESK_SYSTEM_USER_PASSWORD=xxxxxxxxxxx ./libredesk --install` |  | ||||||
|  |  | ||||||
|  |  | ||||||
| ## Docker |  | ||||||
|  |  | ||||||
| The latest image is available on DockerHub at `libredesk/libredesk:latest` |  | ||||||
|  |  | ||||||
| The recommended method is to download the [docker-compose.yml](https://github.com/abhinavxd/libredesk/blob/main/docker-compose.yml) file, customize it for your environment and then to simply run `docker compose up -d`. |  | ||||||
|  |  | ||||||
| ```shell |  | ||||||
| # Download the compose file and the sample config file in the current directory. |  | ||||||
| curl -LO https://github.com/abhinavxd/libredesk/raw/main/docker-compose.yml |  | ||||||
| curl -LO https://github.com/abhinavxd/libredesk/raw/main/config.sample.toml |  | ||||||
|  |  | ||||||
| # Copy the config.sample.toml to config.toml and edit it as needed. |  | ||||||
| cp config.sample.toml config.toml |  | ||||||
|  |  | ||||||
| # Run the services in the background. |  | ||||||
| docker compose up -d |  | ||||||
|  |  | ||||||
| # Setting System user password. |  | ||||||
| docker exec -it libredesk_app ./libredesk --set-system-user-password |  | ||||||
| ``` |  | ||||||
|  |  | ||||||
| Go to `http://localhost:9000` and login with the email `System` and the password you set using the `--set-system-user-password` command. |  | ||||||
|  |  | ||||||
| --- |  | ||||||
|  |  | ||||||
|  |  | ||||||
| ## Compiling from source |  | ||||||
|  |  | ||||||
| To compile the latest unreleased version (`main` branch): |  | ||||||
|  |  | ||||||
| 1. Make sure `go`, `nodejs`, and `pnpm` are installed on your system. |  | ||||||
| 2. `git clone git@github.com:abhinavxd/libredesk.git` |  | ||||||
| 3. `cd libredesk && make`. This will generate the `libredesk` binary. |  | ||||||
| @@ -1,18 +0,0 @@ | |||||||
| # Upgrade |  | ||||||
|  |  | ||||||
| !!! Warning |  | ||||||
|     Always take a backup of the Postgres database before upgrading Libredesk. |  | ||||||
|  |  | ||||||
| ## Binary |  | ||||||
| - Stop running libredesk binary. |  | ||||||
| - Download the [latest release](https://github.com/abhinavxd/libredesk/releases) and extract the libredesk binary and overwrite the previous version. |  | ||||||
| - `./libredesk --upgrade` to upgrade an existing database schema. Upgrades are idempotent and running them multiple times have no side effects. |  | ||||||
| - Run `./libredesk` again. |  | ||||||
|  |  | ||||||
| ## Docker |  | ||||||
|  |  | ||||||
| ```shell |  | ||||||
| docker compose down app |  | ||||||
| docker compose pull |  | ||||||
| docker compose up app -d |  | ||||||
| ``` |  | ||||||
| @@ -1,34 +0,0 @@ | |||||||
| site_name: Libredesk Documentation |  | ||||||
| theme: |  | ||||||
|   name: material |  | ||||||
|   language: en |  | ||||||
|   font: |  | ||||||
|     text: Source Sans Pro |  | ||||||
|     code: Roboto Mono |  | ||||||
|     weights:  |  | ||||||
|       - 400 |  | ||||||
|       - 700 |  | ||||||
|   direction: ltr |  | ||||||
|   palette: |  | ||||||
|     primary: white |  | ||||||
|     accent: red |  | ||||||
|   features: |  | ||||||
|     - navigation.indexes |  | ||||||
|     - navigation.sections |  | ||||||
|     - content.code.copy |  | ||||||
|   extra: |  | ||||||
|     search: |  | ||||||
|       language: en |  | ||||||
|  |  | ||||||
| markdown_extensions: |  | ||||||
|   - admonition |  | ||||||
|   - codehilite |  | ||||||
|   - toc: |  | ||||||
|       permalink: true |  | ||||||
|  |  | ||||||
| nav: |  | ||||||
|   - Introduction: index.md |  | ||||||
|   - Getting Started: |  | ||||||
|       - Installation: installation.md |  | ||||||
|       - Upgrade: upgrade.md |  | ||||||
|   - Developer Setup: developer-setup.md |  | ||||||
| @@ -8,7 +8,6 @@ | |||||||
|     "baseColor": "gray", |     "baseColor": "gray", | ||||||
|     "cssVariables": true |     "cssVariables": true | ||||||
|   }, |   }, | ||||||
|   "framework": "vite", |  | ||||||
|   "aliases": { |   "aliases": { | ||||||
|     "components": "@/components", |     "components": "@/components", | ||||||
|     "utils": "@/lib/utils" |     "utils": "@/lib/utils" | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ import { defineConfig } from 'cypress' | |||||||
| export default defineConfig({ | export default defineConfig({ | ||||||
|   e2e: { |   e2e: { | ||||||
|     specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}', |     specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}', | ||||||
|     baseUrl: 'http://localhost:4173' |     baseUrl: 'http://localhost:9000' | ||||||
|   }, |   }, | ||||||
|   component: { |   component: { | ||||||
|     specPattern: 'src/**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}', |     specPattern: 'src/**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}', | ||||||
|   | |||||||
| @@ -1,8 +0,0 @@ | |||||||
| // https://on.cypress.io/api |  | ||||||
|  |  | ||||||
| describe('My First Test', () => { |  | ||||||
|   it('visits the app root url', () => { |  | ||||||
|     cy.visit('/') |  | ||||||
|     cy.contains('h1', 'You did it!') |  | ||||||
|   }) |  | ||||||
| }) |  | ||||||
							
								
								
									
										150
									
								
								frontend/cypress/e2e/testLogin.cy.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										150
									
								
								frontend/cypress/e2e/testLogin.cy.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,150 @@ | |||||||
|  | // cypress/e2e/login.cy.js | ||||||
|  |  | ||||||
|  | describe('Login Component', () => { | ||||||
|  |     beforeEach(() => { | ||||||
|  |         // Mock the API response for OIDC providers | ||||||
|  |         cy.intercept('GET', '**/api/v1/config', { | ||||||
|  |             statusCode: 200, | ||||||
|  |             body: { | ||||||
|  |                 data: { | ||||||
|  |                     "app.favicon_url": "http://localhost:9000/favicon.ico", | ||||||
|  |                     "app.lang": "en", | ||||||
|  |                     "app.logo_url": "http://localhost:9000/logo.png", | ||||||
|  |                     "app.site_name": "Libredesk", | ||||||
|  |                     "app.sso_providers": [ | ||||||
|  |                         { | ||||||
|  |                             "client_id": "xx", | ||||||
|  |                             "enabled": true, | ||||||
|  |                             "id": 1, | ||||||
|  |                             "logo_url": "/images/google-logo.png", | ||||||
|  |                             "name": "Google", | ||||||
|  |                             "provider": "Google", | ||||||
|  |                             "provider_url": "https://accounts.google.com", | ||||||
|  |                             "redirect_uri": "http://localhost:9000/api/v1/oidc/1/finish" | ||||||
|  |                         } | ||||||
|  |                     ] | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }).as('getOIDCProviders') | ||||||
|  |  | ||||||
|  |         // Visit the login page | ||||||
|  |         cy.visit('/') | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     it('should display login form', () => { | ||||||
|  |         cy.contains('h3', 'Libredesk').should('be.visible') | ||||||
|  |         cy.contains('p', 'Sign in to your account').should('be.visible') | ||||||
|  |         cy.get('#email').should('be.visible') | ||||||
|  |         cy.get('#password').should('be.visible') | ||||||
|  |         cy.contains('a', 'Forgot password?').should('be.visible') | ||||||
|  |         cy.contains('button', 'Sign in').should('be.visible') | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     it('should display OIDC providers when loaded', () => { | ||||||
|  |         cy.wait('@getOIDCProviders') | ||||||
|  |         cy.contains('button', 'Google').should('be.visible') | ||||||
|  |         cy.contains('div', 'Or continue with').should('be.visible') | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     it('should show error for invalid login attempt', () => { | ||||||
|  |         // Mock failed login API call | ||||||
|  |         cy.intercept('POST', '**/api/v1/auth/login', { | ||||||
|  |             statusCode: 401, | ||||||
|  |             body: { | ||||||
|  |                 message: 'Invalid credentials' | ||||||
|  |             } | ||||||
|  |         }).as('loginFailure') | ||||||
|  |  | ||||||
|  |         // Enter System username and wrong password | ||||||
|  |         cy.get('#email').type('System') | ||||||
|  |         cy.get('#password').type('WrongPassword') | ||||||
|  |  | ||||||
|  |         // Submit form | ||||||
|  |         cy.contains('button', 'Sign in').click() | ||||||
|  |  | ||||||
|  |         // Wait for API call | ||||||
|  |         cy.wait('@loginFailure') | ||||||
|  |  | ||||||
|  |         // Verify error message appears | ||||||
|  |         cy.contains('Invalid credentials').should('be.visible') | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     it('should login successfully with correct credentials', () => { | ||||||
|  |         // Mock successful login API call | ||||||
|  |         cy.intercept('POST', '**/api/v1/auth/login', { | ||||||
|  |             statusCode: 200, | ||||||
|  |             body: { | ||||||
|  |                 data: { | ||||||
|  |                     id: 1, | ||||||
|  |                     email: 'System', | ||||||
|  |                     name: 'System User' | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }).as('loginSuccess') | ||||||
|  |  | ||||||
|  |         // Enter System username and correct password | ||||||
|  |         cy.get('#email').type('System') | ||||||
|  |         cy.get('#password').type('StrongPass!123') | ||||||
|  |  | ||||||
|  |         // Submit form | ||||||
|  |         cy.contains('button', 'Sign in').click() | ||||||
|  |  | ||||||
|  |         // Wait for API call | ||||||
|  |         cy.wait('@loginSuccess') | ||||||
|  |  | ||||||
|  |         // Verify redirection to inboxes page | ||||||
|  |         cy.url().should('include', '/inboxes/assigned') | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     it('should validate email format', () => { | ||||||
|  |         // Enter invalid email and a password | ||||||
|  |         cy.get('#email').type('invalid-email') | ||||||
|  |         cy.get('#password').type('password') | ||||||
|  |  | ||||||
|  |         // Submit form | ||||||
|  |         cy.contains('button', 'Sign in').click() | ||||||
|  |  | ||||||
|  |         // Check for validation error (matching the error message with a trailing period) | ||||||
|  |         cy.contains('Invalid email address').should('be.visible') | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     it('should validate empty password', () => { | ||||||
|  |         // Enter email but no password | ||||||
|  |         cy.get('#email').type('valid@example.com') | ||||||
|  |  | ||||||
|  |         // Submit form | ||||||
|  |         cy.contains('button', 'Sign in').click() | ||||||
|  |  | ||||||
|  |         // Check for validation error (matching the error message with a trailing period) | ||||||
|  |         cy.contains('Password cannot be empty').should('be.visible') | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     it('should show loading state during login', () => { | ||||||
|  |         // Mock slow API response | ||||||
|  |         cy.intercept('POST', '**/api/v1/auth/login', { | ||||||
|  |             statusCode: 200, | ||||||
|  |             body: { | ||||||
|  |                 data: { | ||||||
|  |                     id: 1, | ||||||
|  |                     email: 'System', | ||||||
|  |                     name: 'System User' | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             delay: 1000 | ||||||
|  |         }).as('slowLogin') | ||||||
|  |  | ||||||
|  |         // Enter credentials | ||||||
|  |         cy.get('#email').type('System') | ||||||
|  |         cy.get('#password').type('StrongPass!123') | ||||||
|  |  | ||||||
|  |         // Submit form | ||||||
|  |         cy.contains('button', 'Sign in').click() | ||||||
|  |  | ||||||
|  |         // Check if loading state is shown | ||||||
|  |         cy.contains('Logging in...').should('be.visible') | ||||||
|  |         cy.get('.animate-spin').should('be.visible') | ||||||
|  |  | ||||||
|  |         // Wait for API call to finish | ||||||
|  |         cy.wait('@slowLogin') | ||||||
|  |     }) | ||||||
|  | }) | ||||||
| @@ -6,8 +6,7 @@ | |||||||
|   <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |   <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||||||
|   <link rel="preconnect" href="https://fonts.googleapis.com"> |   <link rel="preconnect" href="https://fonts.googleapis.com"> | ||||||
|   <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> |   <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||||||
|   <link |   <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:ital,wght@0,200..800;1,200..800&display=swap" | ||||||
|     href="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=Plus+Jakarta+Sans:ital,wght@0,200..800;1,200..800&display=swap" |  | ||||||
|     rel="stylesheet"> |     rel="stylesheet"> | ||||||
| </head> | </head> | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,13 +1,16 @@ | |||||||
| { | { | ||||||
|   "name": "libredesk", |   "name": "libredesk", | ||||||
|   "version": "0.3.0", |   "version": "0.8.0-beta", | ||||||
|   "private": true, |   "private": true, | ||||||
|   "type": "module", |   "type": "module", | ||||||
|   "scripts": { |   "scripts": { | ||||||
|     "dev": "pnpm exec vite", |     "dev": "pnpm exec vite", | ||||||
|     "build": "vite build", |     "build": "vite build", | ||||||
|     "preview": "vite preview", |     "preview": "vite preview", | ||||||
|  |     "test": "vitest", | ||||||
|  |     "test:run": "vitest run", | ||||||
|     "test:e2e": "start-server-and-test preview http://localhost:4173 'cypress run --e2e'", |     "test:e2e": "start-server-and-test preview http://localhost:4173 'cypress run --e2e'", | ||||||
|  |     "test:e2e:ci": "cypress run --e2e --headless", | ||||||
|     "test:e2e:dev": "start-server-and-test 'vite dev --port 4173' http://localhost:4173 'cypress open --e2e'", |     "test:e2e:dev": "start-server-and-test 'vite dev --port 4173' http://localhost:4173 'cypress open --e2e'", | ||||||
|     "test:unit": "cypress run --component", |     "test:unit": "cypress run --component", | ||||||
|     "test:unit:dev": "cypress open --component", |     "test:unit:dev": "cypress open --component", | ||||||
| @@ -15,33 +18,40 @@ | |||||||
|     "format": "prettier --write src/" |     "format": "prettier --write src/" | ||||||
|   }, |   }, | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
|  |     "@codemirror/lang-html": "^6.4.9", | ||||||
|  |     "@codemirror/theme-one-dark": "^6.1.3", | ||||||
|     "@formkit/auto-animate": "^0.8.2", |     "@formkit/auto-animate": "^0.8.2", | ||||||
|     "@internationalized/date": "^3.5.5", |     "@internationalized/date": "^3.5.5", | ||||||
|     "@radix-icons/vue": "^1.0.0", |     "@radix-icons/vue": "^1.0.0", | ||||||
|     "@tailwindcss/typography": "^0.5.16", |     "@tailwindcss/typography": "^0.5.16", | ||||||
|     "@tanstack/vue-table": "^8.19.2", |     "@tanstack/vue-table": "^8.19.2", | ||||||
|     "@tiptap/extension-image": "^2.5.9", |     "@tiptap/extension-image": "^2.5.9", | ||||||
|     "@tiptap/extension-link": "^2.9.1", |     "@tiptap/extension-link": "^2.11.2", | ||||||
|     "@tiptap/extension-placeholder": "^2.4.0", |     "@tiptap/extension-placeholder": "^2.4.0", | ||||||
|  |     "@tiptap/extension-table": "^2.11.5", | ||||||
|  |     "@tiptap/extension-table-cell": "^2.11.5", | ||||||
|  |     "@tiptap/extension-table-header": "^2.11.5", | ||||||
|  |     "@tiptap/extension-table-row": "^2.11.5", | ||||||
|     "@tiptap/pm": "^2.4.0", |     "@tiptap/pm": "^2.4.0", | ||||||
|     "@tiptap/starter-kit": "^2.4.0", |     "@tiptap/starter-kit": "^2.4.0", | ||||||
|     "@tiptap/vue-3": "^2.4.0", |     "@tiptap/vue-3": "^2.4.0", | ||||||
|     "@unovis/ts": "^1.4.4", |     "@unovis/ts": "^1.4.4", | ||||||
|     "@unovis/vue": "^1.4.4", |     "@unovis/vue": "^1.4.4", | ||||||
|     "@vee-validate/zod": "^4.13.2", |     "@vee-validate/zod": "^4.15.0", | ||||||
|     "@vueuse/core": "^12.4.0", |     "@vueuse/core": "^12.4.0", | ||||||
|     "axios": "^1.7.9", |     "axios": "^1.12.0", | ||||||
|     "class-variance-authority": "^0.7.0", |     "class-variance-authority": "^0.7.0", | ||||||
|     "clsx": "^2.1.1", |     "clsx": "^2.1.1", | ||||||
|     "codeflask": "^1.4.1", |     "codemirror": "^6.0.2", | ||||||
|     "date-fns": "^3.6.0", |     "date-fns": "^3.6.0", | ||||||
|     "lucide-vue-next": "^0.378.0", |     "lucide-vue-next": "^0.378.0", | ||||||
|     "mitt": "^3.0.1", |     "mitt": "^3.0.1", | ||||||
|     "pinia": "^2.1.7", |     "pinia": "^2.1.7", | ||||||
|     "qs": "^6.12.1", |     "qs": "^6.12.1", | ||||||
|     "radix-vue": "latest", |     "radix-vue": "^1.9.17", | ||||||
|  |     "reka-ui": "^2.2.0", | ||||||
|     "tailwind-merge": "^2.3.0", |     "tailwind-merge": "^2.3.0", | ||||||
|     "vee-validate": "^4.13.2", |     "vee-validate": "^4.15.0", | ||||||
|     "vue": "^3.4.37", |     "vue": "^3.4.37", | ||||||
|     "vue-dompurify-html": "^5.2.0", |     "vue-dompurify-html": "^5.2.0", | ||||||
|     "vue-i18n": "9", |     "vue-i18n": "9", | ||||||
| @@ -51,7 +61,7 @@ | |||||||
|     "vue-sonner": "^1.3.0", |     "vue-sonner": "^1.3.0", | ||||||
|     "vue3-emoji-picker": "^1.1.8", |     "vue3-emoji-picker": "^1.1.8", | ||||||
|     "vuedraggable": "^4.1.0", |     "vuedraggable": "^4.1.0", | ||||||
|     "zod": "^3.23.8" |     "zod": "^3.24.1" | ||||||
|   }, |   }, | ||||||
|   "devDependencies": { |   "devDependencies": { | ||||||
|     "@rushstack/eslint-patch": "^1.3.3", |     "@rushstack/eslint-patch": "^1.3.3", | ||||||
| @@ -66,9 +76,10 @@ | |||||||
|     "prettier": "^3.0.3", |     "prettier": "^3.0.3", | ||||||
|     "sass": "^1.70.0", |     "sass": "^1.70.0", | ||||||
|     "start-server-and-test": "^2.0.3", |     "start-server-and-test": "^2.0.3", | ||||||
|     "tailwindcss": "latest", |     "tailwindcss": "^3.4.17", | ||||||
|     "tailwindcss-animate": "^1.0.7", |     "tailwindcss-animate": "^1.0.7", | ||||||
|     "vite": "^5.4.9" |     "vite": "^5.4.20", | ||||||
|  |     "vitest": "^3.2.2" | ||||||
|   }, |   }, | ||||||
|   "packageManager": "pnpm@9.15.3+sha512.1f79bc245a66eb0b07c5d4d83131240774642caaa86ef7d0434ab47c0d16f66b04e21e0c086eb61e62c77efc4d7f7ec071afad3796af64892fae66509173893a" |   "packageManager": "pnpm@9.15.3+sha512.1f79bc245a66eb0b07c5d4d83131240774642caaa86ef7d0434ab47c0d16f66b04e21e0c086eb61e62c77efc4d7f7ec071afad3796af64892fae66509173893a" | ||||||
| } | } | ||||||
							
								
								
									
										1070
									
								
								frontend/pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1070
									
								
								frontend/pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -1,5 +1,5 @@ | |||||||
| <template> | <template> | ||||||
|   <div class="flex w-full h-screen"> |   <div class="flex w-full h-screen text-foreground"> | ||||||
|     <!-- Icon sidebar always visible --> |     <!-- Icon sidebar always visible --> | ||||||
|     <SidebarProvider style="--sidebar-width: 3rem" class="w-auto z-50"> |     <SidebarProvider style="--sidebar-width: 3rem" class="w-auto z-50"> | ||||||
|       <ShadcnSidebar collapsible="none" class="border-r"> |       <ShadcnSidebar collapsible="none" class="border-r"> | ||||||
| @@ -8,25 +8,64 @@ | |||||||
|             <SidebarGroupContent> |             <SidebarGroupContent> | ||||||
|               <SidebarMenu> |               <SidebarMenu> | ||||||
|                 <SidebarMenuItem> |                 <SidebarMenuItem> | ||||||
|                   <SidebarMenuButton asChild :isActive="route.path.startsWith('/inboxes')"> |                   <Tooltip> | ||||||
|                     <router-link :to="{ name: 'inboxes' }"> |                     <TooltipTrigger as-child> | ||||||
|                       <Inbox /> |                       <SidebarMenuButton asChild :isActive="route.path.startsWith('/inboxes')"> | ||||||
|                     </router-link> |                         <router-link :to="{ name: 'inboxes' }"> | ||||||
|                   </SidebarMenuButton> |                           <Inbox /> | ||||||
|  |                         </router-link> | ||||||
|  |                       </SidebarMenuButton> | ||||||
|  |                     </TooltipTrigger> | ||||||
|  |                     <TooltipContent side="right"> | ||||||
|  |                       <p>{{ t('globals.terms.inbox', 2) }}</p> | ||||||
|  |                     </TooltipContent> | ||||||
|  |                   </Tooltip> | ||||||
|                 </SidebarMenuItem> |                 </SidebarMenuItem> | ||||||
|                 <SidebarMenuItem v-if="userStore.hasAdminTabPermissions"> |                 <SidebarMenuItem v-if="userStore.can('contacts:read_all')"> | ||||||
|                   <SidebarMenuButton asChild :isActive="route.path.startsWith('/admin')"> |                   <Tooltip> | ||||||
|                     <router-link :to="{ name: 'admin' }"> |                     <TooltipTrigger as-child> | ||||||
|                       <Shield /> |                       <SidebarMenuButton asChild :isActive="route.path.startsWith('/contacts')"> | ||||||
|                     </router-link> |                         <router-link :to="{ name: 'contacts' }"> | ||||||
|                   </SidebarMenuButton> |                           <BookUser /> | ||||||
|  |                         </router-link> | ||||||
|  |                       </SidebarMenuButton> | ||||||
|  |                     </TooltipTrigger> | ||||||
|  |                     <TooltipContent side="right"> | ||||||
|  |                       <p>{{ t('globals.terms.contact', 2) }}</p> | ||||||
|  |                     </TooltipContent> | ||||||
|  |                   </Tooltip> | ||||||
|                 </SidebarMenuItem> |                 </SidebarMenuItem> | ||||||
|                 <SidebarMenuItem v-if="userStore.hasReportTabPermissions"> |                 <SidebarMenuItem v-if="userStore.hasReportTabPermissions"> | ||||||
|                   <SidebarMenuButton asChild :isActive="route.path.startsWith('/reports')"> |                   <Tooltip> | ||||||
|                     <router-link :to="{ name: 'reports' }"> |                     <TooltipTrigger as-child> | ||||||
|                       <FileLineChart /> |                       <SidebarMenuButton asChild :isActive="route.path.startsWith('/reports')"> | ||||||
|                     </router-link> |                         <router-link :to="{ name: 'reports' }"> | ||||||
|                   </SidebarMenuButton> |                           <FileLineChart /> | ||||||
|  |                         </router-link> | ||||||
|  |                       </SidebarMenuButton> | ||||||
|  |                     </TooltipTrigger> | ||||||
|  |                     <TooltipContent side="right"> | ||||||
|  |                       <p>{{ t('globals.terms.report', 2) }}</p> | ||||||
|  |                     </TooltipContent> | ||||||
|  |                   </Tooltip> | ||||||
|  |                 </SidebarMenuItem> | ||||||
|  |                 <SidebarMenuItem v-if="userStore.hasAdminTabPermissions"> | ||||||
|  |                   <Tooltip> | ||||||
|  |                     <TooltipTrigger as-child> | ||||||
|  |                       <SidebarMenuButton asChild :isActive="route.path.startsWith('/admin')"> | ||||||
|  |                         <router-link | ||||||
|  |                           :to="{ | ||||||
|  |                             name: userStore.can('general_settings:manage') ? 'general' : 'admin' | ||||||
|  |                           }" | ||||||
|  |                         > | ||||||
|  |                           <Shield /> | ||||||
|  |                         </router-link> | ||||||
|  |                       </SidebarMenuButton> | ||||||
|  |                     </TooltipTrigger> | ||||||
|  |                     <TooltipContent side="right"> | ||||||
|  |                       <p>{{ t('globals.terms.admin') }}</p> | ||||||
|  |                     </TooltipContent> | ||||||
|  |                   </Tooltip> | ||||||
|                 </SidebarMenuItem> |                 </SidebarMenuItem> | ||||||
|               </SidebarMenu> |               </SidebarMenu> | ||||||
|             </SidebarGroupContent> |             </SidebarGroupContent> | ||||||
| @@ -46,11 +85,11 @@ | |||||||
|         @create-view="openCreateViewForm = true" |         @create-view="openCreateViewForm = true" | ||||||
|         @edit-view="editView" |         @edit-view="editView" | ||||||
|         @delete-view="deleteView" |         @delete-view="deleteView" | ||||||
|         @create-conversation="() => openCreateConversationDialog = true" |         @create-conversation="() => (openCreateConversationDialog = true)" | ||||||
|       > |       > | ||||||
|         <div class="flex flex-col h-screen"> |         <div class="flex flex-col h-screen"> | ||||||
|           <!-- Show app update only in admin routes --> |           <!-- Show admin banner only in admin routes --> | ||||||
|           <AppUpdate v-if="route.path.startsWith('/admin')" /> |           <AdminBanner v-if="route.path.startsWith('/admin')" /> | ||||||
|  |  | ||||||
|           <!-- Common header for all pages --> |           <!-- Common header for all pages --> | ||||||
|           <PageHeader /> |           <PageHeader /> | ||||||
| @@ -67,7 +106,7 @@ | |||||||
|   <Command /> |   <Command /> | ||||||
|  |  | ||||||
|   <!-- Create conversation dialog --> |   <!-- Create conversation dialog --> | ||||||
|   <CreateConversation v-model="openCreateConversationDialog" /> |   <CreateConversation v-model="openCreateConversationDialog" v-if="openCreateConversationDialog" /> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
| <script setup> | <script setup> | ||||||
| @@ -85,16 +124,18 @@ import { useTeamStore } from '@/stores/team' | |||||||
| import { useSlaStore } from '@/stores/sla' | import { useSlaStore } from '@/stores/sla' | ||||||
| import { useMacroStore } from '@/stores/macro' | import { useMacroStore } from '@/stores/macro' | ||||||
| import { useTagStore } from '@/stores/tag' | import { useTagStore } from '@/stores/tag' | ||||||
|  | import { useCustomAttributeStore } from '@/stores/customAttributes' | ||||||
| import { useIdleDetection } from '@/composables/useIdleDetection' | import { useIdleDetection } from '@/composables/useIdleDetection' | ||||||
| import PageHeader from './components/layout/PageHeader.vue' | import PageHeader from './components/layout/PageHeader.vue' | ||||||
| import ViewForm from '@/features/view/ViewForm.vue' | import ViewForm from '@/features/view/ViewForm.vue' | ||||||
| import AppUpdate from '@/components/update/AppUpdate.vue' | import AdminBanner from '@/components/banner/AdminBanner.vue' | ||||||
| import api from '@/api' | import api from '@/api' | ||||||
| import { toast as sooner } from 'vue-sonner' | import { toast as sooner } from 'vue-sonner' | ||||||
| import Sidebar from '@/components/sidebar/Sidebar.vue' | import Sidebar from '@/components/sidebar/Sidebar.vue' | ||||||
| import Command from '@/features/command/CommandBox.vue' | import Command from '@/features/command/CommandBox.vue' | ||||||
| import CreateConversation from '@/features/conversation/CreateConversation.vue' | import CreateConversation from '@/features/conversation/CreateConversation.vue' | ||||||
| import { Inbox, Shield, FileLineChart } from 'lucide-vue-next' | import { Inbox, Shield, FileLineChart, BookUser } from 'lucide-vue-next' | ||||||
|  | import { useI18n } from 'vue-i18n' | ||||||
| import { useRoute } from 'vue-router' | import { useRoute } from 'vue-router' | ||||||
| import { | import { | ||||||
|   Sidebar as ShadcnSidebar, |   Sidebar as ShadcnSidebar, | ||||||
| @@ -107,6 +148,7 @@ import { | |||||||
|   SidebarMenuItem, |   SidebarMenuItem, | ||||||
|   SidebarProvider |   SidebarProvider | ||||||
| } from '@/components/ui/sidebar' | } from '@/components/ui/sidebar' | ||||||
|  | import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' | ||||||
| import SidebarNavUser from '@/components/sidebar/SidebarNavUser.vue' | import SidebarNavUser from '@/components/sidebar/SidebarNavUser.vue' | ||||||
|  |  | ||||||
| const route = useRoute() | const route = useRoute() | ||||||
| @@ -119,10 +161,12 @@ const inboxStore = useInboxStore() | |||||||
| const slaStore = useSlaStore() | const slaStore = useSlaStore() | ||||||
| const macroStore = useMacroStore() | const macroStore = useMacroStore() | ||||||
| const tagStore = useTagStore() | const tagStore = useTagStore() | ||||||
|  | const customAttributeStore = useCustomAttributeStore() | ||||||
| const userViews = ref([]) | const userViews = ref([]) | ||||||
| const view = ref({}) | const view = ref({}) | ||||||
| const openCreateViewForm = ref(false) | const openCreateViewForm = ref(false) | ||||||
| const openCreateConversationDialog = ref(false) | const openCreateConversationDialog = ref(false) | ||||||
|  | const { t } = useI18n() | ||||||
|  |  | ||||||
| initWS() | initWS() | ||||||
| useIdleDetection() | useIdleDetection() | ||||||
| @@ -133,7 +177,7 @@ onMounted(() => { | |||||||
|   initStores() |   initStores() | ||||||
| }) | }) | ||||||
|  |  | ||||||
| // initialize data stores | // Initialize data stores | ||||||
| const initStores = async () => { | const initStores = async () => { | ||||||
|   if (!userStore.userID) { |   if (!userStore.userID) { | ||||||
|     await userStore.getCurrentUser() |     await userStore.getCurrentUser() | ||||||
| @@ -147,7 +191,8 @@ const initStores = async () => { | |||||||
|     inboxStore.fetchInboxes(), |     inboxStore.fetchInboxes(), | ||||||
|     slaStore.fetchSlas(), |     slaStore.fetchSlas(), | ||||||
|     macroStore.loadMacros(), |     macroStore.loadMacros(), | ||||||
|     tagStore.fetchTags() |     tagStore.fetchTags(), | ||||||
|  |     customAttributeStore.fetchCustomAttributes() | ||||||
|   ]) |   ]) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -161,12 +206,12 @@ const deleteView = async (view) => { | |||||||
|     await api.deleteView(view.id) |     await api.deleteView(view.id) | ||||||
|     emitter.emit(EMITTER_EVENTS.REFRESH_LIST, { model: 'view' }) |     emitter.emit(EMITTER_EVENTS.REFRESH_LIST, { model: 'view' }) | ||||||
|     emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { |     emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { | ||||||
|       title: 'Success', |       description: t('globals.messages.deletedSuccessfully', { | ||||||
|       description: 'View deleted successfully' |         name: t('globals.terms.view') | ||||||
|  |       }) | ||||||
|     }) |     }) | ||||||
|   } catch (err) { |   } catch (err) { | ||||||
|     emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { |     emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { | ||||||
|       title: 'Error', |  | ||||||
|       variant: 'destructive', |       variant: 'destructive', | ||||||
|       description: handleHTTPError(err).message |       description: handleHTTPError(err).message | ||||||
|     }) |     }) | ||||||
| @@ -179,7 +224,6 @@ const getUserViews = async () => { | |||||||
|     userViews.value = response.data.data |     userViews.value = response.data.data | ||||||
|   } catch (err) { |   } catch (err) { | ||||||
|     emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { |     emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { | ||||||
|       title: 'Error', |  | ||||||
|       variant: 'destructive', |       variant: 'destructive', | ||||||
|       description: handleHTTPError(err).message |       description: handleHTTPError(err).message | ||||||
|     }) |     }) | ||||||
|   | |||||||
| @@ -1,9 +1,7 @@ | |||||||
| <template> | <template> | ||||||
|   <TooltipProvider :delay-duration="150"> |   <TooltipProvider :delay-duration="150"> | ||||||
|     <div class="!font-jakarta"> |     <Toaster class="pointer-events-auto" position="top-center" richColors /> | ||||||
|       <Toaster class="pointer-events-auto" position="top-center" richColors /> |     <RouterView /> | ||||||
|       <RouterView /> |  | ||||||
|     </div> |  | ||||||
|   </TooltipProvider> |   </TooltipProvider> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,15 +7,15 @@ const http = axios.create({ | |||||||
| }) | }) | ||||||
|  |  | ||||||
| function getCSRFToken () { | function getCSRFToken () { | ||||||
|   const name = 'csrf_token='; |   const name = 'csrf_token=' | ||||||
|   const cookies = document.cookie.split(';'); |   const cookies = document.cookie.split(';') | ||||||
|   for (let i = 0; i < cookies.length; i++) { |   for (let i = 0; i < cookies.length; i++) { | ||||||
|     let c = cookies[i].trim(); |     let c = cookies[i].trim() | ||||||
|     if (c.indexOf(name) === 0) { |     if (c.indexOf(name) === 0) { | ||||||
|       return c.substring(name.length, c.length); |       return c.substring(name.length, c.length) | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   return ''; |   return '' | ||||||
| } | } | ||||||
|  |  | ||||||
| // Request interceptor. | // Request interceptor. | ||||||
| @@ -27,20 +27,40 @@ http.interceptors.request.use((request) => { | |||||||
|  |  | ||||||
|   // Set content type for POST/PUT requests if the content type is not set. |   // Set content type for POST/PUT requests if the content type is not set. | ||||||
|   if ((request.method === 'post' || request.method === 'put') && !request.headers['Content-Type']) { |   if ((request.method === 'post' || request.method === 'put') && !request.headers['Content-Type']) { | ||||||
|     request.headers['Content-Type'] = 'application/x-www-form-urlencoded' |     request.headers['Content-Type'] = 'application/json' | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   if (request.headers['Content-Type'] === 'application/x-www-form-urlencoded') { | ||||||
|     request.data = qs.stringify(request.data) |     request.data = qs.stringify(request.data) | ||||||
|   } |   } | ||||||
|  |    | ||||||
|   return request |   return request | ||||||
| }) | }) | ||||||
|  |  | ||||||
|  | const getCustomAttributes = (appliesTo) => | ||||||
|  |   http.get('/api/v1/custom-attributes', { | ||||||
|  |     params: { applies_to: appliesTo } | ||||||
|  |   }) | ||||||
|  | const createCustomAttribute = (data) => | ||||||
|  |   http.post('/api/v1/custom-attributes', data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const getCustomAttribute = (id) => http.get(`/api/v1/custom-attributes/${id}`) | ||||||
|  | const updateCustomAttribute = (id, data) => | ||||||
|  |   http.put(`/api/v1/custom-attributes/${id}`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const deleteCustomAttribute = (id) => http.delete(`/api/v1/custom-attributes/${id}`) | ||||||
| const searchConversations = (params) => http.get('/api/v1/conversations/search', { params }) | const searchConversations = (params) => http.get('/api/v1/conversations/search', { params }) | ||||||
| const searchMessages = (params) => http.get('/api/v1/messages/search', { params }) | const searchMessages = (params) => http.get('/api/v1/messages/search', { params }) | ||||||
| const searchContacts = (params) => http.get('/api/v1/contacts/search', { params }) | const searchContacts = (params) => http.get('/api/v1/contacts/search', { params }) | ||||||
| const resetPassword = (data) => http.post('/api/v1/users/reset-password', data) |  | ||||||
| const setPassword = (data) => http.post('/api/v1/users/set-password', data) |  | ||||||
| const deleteUser = (id) => http.delete(`/api/v1/users/${id}`) |  | ||||||
| const getEmailNotificationSettings = () => http.get('/api/v1/settings/notifications/email') | const getEmailNotificationSettings = () => http.get('/api/v1/settings/notifications/email') | ||||||
| const updateEmailNotificationSettings = (data) => http.put('/api/v1/settings/notifications/email', data) | const updateEmailNotificationSettings = (data) => | ||||||
|  |   http.put('/api/v1/settings/notifications/email', data) | ||||||
| const getPriorities = () => http.get('/api/v1/priorities') | const getPriorities = () => http.get('/api/v1/priorities') | ||||||
| const getStatuses = () => http.get('/api/v1/statuses') | const getStatuses = () => http.get('/api/v1/statuses') | ||||||
| const createStatus = (data) => http.post('/api/v1/statuses', data) | const createStatus = (data) => http.post('/api/v1/statuses', data) | ||||||
| @@ -67,11 +87,12 @@ const updateTemplate = (id, data) => | |||||||
|  |  | ||||||
| const getAllBusinessHours = () => http.get('/api/v1/business-hours') | const getAllBusinessHours = () => http.get('/api/v1/business-hours') | ||||||
| const getBusinessHours = (id) => http.get(`/api/v1/business-hours/${id}`) | const getBusinessHours = (id) => http.get(`/api/v1/business-hours/${id}`) | ||||||
| const createBusinessHours = (data) => http.post('/api/v1/business-hours', data, { | const createBusinessHours = (data) => | ||||||
|   headers: { |   http.post('/api/v1/business-hours', data, { | ||||||
|     'Content-Type': 'application/json' |     headers: { | ||||||
|   } |       'Content-Type': 'application/json' | ||||||
| }) |     } | ||||||
|  |   }) | ||||||
| const updateBusinessHours = (id, data) => | const updateBusinessHours = (id, data) => | ||||||
|   http.put(`/api/v1/business-hours/${id}`, data, { |   http.put(`/api/v1/business-hours/${id}`, data, { | ||||||
|     headers: { |     headers: { | ||||||
| @@ -82,8 +103,18 @@ const deleteBusinessHours = (id) => http.delete(`/api/v1/business-hours/${id}`) | |||||||
|  |  | ||||||
| const getAllSLAs = () => http.get('/api/v1/sla') | const getAllSLAs = () => http.get('/api/v1/sla') | ||||||
| const getSLA = (id) => http.get(`/api/v1/sla/${id}`) | const getSLA = (id) => http.get(`/api/v1/sla/${id}`) | ||||||
| const createSLA = (data) => http.post('/api/v1/sla', data) | const createSLA = (data) => | ||||||
| const updateSLA = (id, data) => http.put(`/api/v1/sla/${id}`, data) |   http.post('/api/v1/sla', data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const updateSLA = (id, data) => | ||||||
|  |   http.put(`/api/v1/sla/${id}`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
| const deleteSLA = (id) => http.delete(`/api/v1/sla/${id}`) | const deleteSLA = (id) => http.delete(`/api/v1/sla/${id}`) | ||||||
| const createOIDC = (data) => | const createOIDC = (data) => | ||||||
|   http.post('/api/v1/oidc', data, { |   http.post('/api/v1/oidc', data, { | ||||||
| @@ -91,8 +122,7 @@ const createOIDC = (data) => | |||||||
|       'Content-Type': 'application/json' |       'Content-Type': 'application/json' | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const testOIDC = (data) => http.post('/api/v1/oidc/test', data) | const getConfig = () => http.get('/api/v1/config') | ||||||
| const getAllEnabledOIDC = () => http.get('/api/v1/oidc/enabled') |  | ||||||
| const getAllOIDC = () => http.get('/api/v1/oidc') | const getAllOIDC = () => http.get('/api/v1/oidc') | ||||||
| const getOIDC = (id) => http.get(`/api/v1/oidc/${id}`) | const getOIDC = (id) => http.get(`/api/v1/oidc/${id}`) | ||||||
| const updateOIDC = (id, data) => | const updateOIDC = (id, data) => | ||||||
| @@ -109,33 +139,42 @@ const updateSettings = (key, data) => | |||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const getSettings = (key) => http.get(`/api/v1/settings/${key}`) | const getSettings = (key) => http.get(`/api/v1/settings/${key}`) | ||||||
| const login = (data) => http.post(`/api/v1/login`, data) | const login = (data) => http.post(`/api/v1/auth/login`, data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
| const getAutomationRules = (type) => | const getAutomationRules = (type) => | ||||||
|   http.get(`/api/v1/automation/rules`, { |   http.get(`/api/v1/automations/rules`, { | ||||||
|     params: { type: type } |     params: { type: type } | ||||||
|   }) |   }) | ||||||
| const toggleAutomationRule = (id) => http.put(`/api/v1/automation/rules/${id}/toggle`) | const toggleAutomationRule = (id) => http.put(`/api/v1/automations/rules/${id}/toggle`) | ||||||
| const getAutomationRule = (id) => http.get(`/api/v1/automation/rules/${id}`) | const getAutomationRule = (id) => http.get(`/api/v1/automations/rules/${id}`) | ||||||
| const updateAutomationRule = (id, data) => | const updateAutomationRule = (id, data) => | ||||||
|   http.put(`/api/v1/automation/rules/${id}`, data, { |   http.put(`/api/v1/automations/rules/${id}`, data, { | ||||||
|     headers: { |     headers: { | ||||||
|       'Content-Type': 'application/json' |       'Content-Type': 'application/json' | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const createAutomationRule = (data) => | const createAutomationRule = (data) => | ||||||
|   http.post(`/api/v1/automation/rules`, data, { |   http.post(`/api/v1/automations/rules`, data, { | ||||||
|     headers: { |     headers: { | ||||||
|       'Content-Type': 'application/json' |       'Content-Type': 'application/json' | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const deleteAutomationRule = (id) => http.delete(`/api/v1/automation/rules/${id}`) | const deleteAutomationRule = (id) => http.delete(`/api/v1/automations/rules/${id}`) | ||||||
| const updateAutomationRuleWeights = (data) => | const updateAutomationRuleWeights = (data) => | ||||||
|   http.put(`/api/v1/automation/rules/weights`, data, { |   http.put(`/api/v1/automations/rules/weights`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const updateAutomationRulesExecutionMode = (data) => | ||||||
|  |   http.put(`/api/v1/automations/rules/execution-mode`, data, { | ||||||
|     headers: { |     headers: { | ||||||
|       'Content-Type': 'application/json' |       'Content-Type': 'application/json' | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const updateAutomationRulesExecutionMode = (data) => http.put(`/api/v1/automation/rules/execution-mode`, data) |  | ||||||
| const getRoles = () => http.get('/api/v1/roles') | const getRoles = () => http.get('/api/v1/roles') | ||||||
| const getRole = (id) => http.get(`/api/v1/roles/${id}`) | const getRole = (id) => http.get(`/api/v1/roles/${id}`) | ||||||
| const createRole = (data) => | const createRole = (data) => | ||||||
| @@ -151,37 +190,124 @@ const updateRole = (id, data) => | |||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const deleteRole = (id) => http.delete(`/api/v1/roles/${id}`) | const deleteRole = (id) => http.delete(`/api/v1/roles/${id}`) | ||||||
| const getUser = (id) => http.get(`/api/v1/users/${id}`) | const getContacts = (params) => http.get('/api/v1/contacts', { params }) | ||||||
| const getTeam = (id) => http.get(`/api/v1/teams/${id}`) | const getContact = (id) => http.get(`/api/v1/contacts/${id}`) | ||||||
| const getTeams = () => http.get('/api/v1/teams') | const updateContact = (id, data) => | ||||||
| const updateTeam = (id, data) => http.put(`/api/v1/teams/${id}`, data) |   http.put(`/api/v1/contacts/${id}`, data, { | ||||||
| const createTeam = (data) => http.post('/api/v1/teams', data) |  | ||||||
| const getTeamsCompact = () => http.get('/api/v1/teams/compact') |  | ||||||
| const deleteTeam = (id) => http.delete(`/api/v1/teams/${id}`) |  | ||||||
|  |  | ||||||
| const getUsers = () => http.get('/api/v1/users') |  | ||||||
| const getUsersCompact = () => http.get('/api/v1/users/compact') |  | ||||||
| const updateCurrentUser = (data) => |  | ||||||
|   http.put('/api/v1/users/me', data, { |  | ||||||
|     headers: { |     headers: { | ||||||
|       'Content-Type': 'multipart/form-data' |       'Content-Type': 'multipart/form-data' | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const deleteUserAvatar = () => http.delete('/api/v1/users/me/avatar') | const blockContact = (id, data) => http.put(`/api/v1/contacts/${id}/block`, data, { | ||||||
| const getCurrentUser = () => http.get('/api/v1/users/me') |   headers: { | ||||||
| const getCurrentUserTeams = () => http.get('/api/v1/users/me/teams') |     'Content-Type': 'application/json' | ||||||
| const updateCurrentUserAvailability = (data) => http.put('/api/v1/users/me/availability', data) |   } | ||||||
|  | }) | ||||||
|  | const getTeam = (id) => http.get(`/api/v1/teams/${id}`) | ||||||
|  | const getTeams = () => http.get('/api/v1/teams') | ||||||
|  | const updateTeam = (id, data) => http.put(`/api/v1/teams/${id}`, data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const createTeam = (data) => http.post('/api/v1/teams', data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const getTeamsCompact = () => http.get('/api/v1/teams/compact') | ||||||
|  | const deleteTeam = (id) => http.delete(`/api/v1/teams/${id}`) | ||||||
|  | const updateUser = (id, data) => | ||||||
|  |   http.put(`/api/v1/agents/${id}`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const getUsers = () => http.get('/api/v1/agents') | ||||||
|  | const getUsersCompact = () => http.get('/api/v1/agents/compact') | ||||||
|  | const updateCurrentUser = (data) => | ||||||
|  |   http.put('/api/v1/agents/me', data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'multipart/form-data' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const getUser = (id) => http.get(`/api/v1/agents/${id}`) | ||||||
|  | const deleteUserAvatar = () => http.delete('/api/v1/agents/me/avatar') | ||||||
|  | const getCurrentUser = () => http.get('/api/v1/agents/me') | ||||||
|  | const getCurrentUserTeams = () => http.get('/api/v1/agents/me/teams') | ||||||
|  | const updateCurrentUserAvailability = (data) => http.put('/api/v1/agents/me/availability', data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const resetPassword = (data) => http.post('/api/v1/agents/reset-password', data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const setPassword = (data) => http.post('/api/v1/agents/set-password', data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const deleteUser = (id) => http.delete(`/api/v1/agents/${id}`) | ||||||
|  | const createUser = (data) => | ||||||
|  |   http.post('/api/v1/agents', data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
| const getTags = () => http.get('/api/v1/tags') | const getTags = () => http.get('/api/v1/tags') | ||||||
| const upsertTags = (uuid, data) => http.post(`/api/v1/conversations/${uuid}/tags`, data) | const upsertTags = (uuid, data) => http.post(`/api/v1/conversations/${uuid}/tags`, data, { | ||||||
| const updateAssignee = (uuid, assignee_type, data) => http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}`, data) |   headers: { | ||||||
| const removeAssignee = (uuid, assignee_type) => http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}/remove`) |     'Content-Type': 'application/json' | ||||||
| const createConversation = (data) => http.post('/api/v1/conversations', data) |   } | ||||||
| const updateConversationStatus = (uuid, data) => http.put(`/api/v1/conversations/${uuid}/status`, data) | }) | ||||||
| const updateConversationPriority = (uuid, data) => http.put(`/api/v1/conversations/${uuid}/priority`, data) | const updateAssignee = (uuid, assignee_type, data) => | ||||||
|  |   http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const removeAssignee = (uuid, assignee_type) => | ||||||
|  |   http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}/remove`) | ||||||
|  | const updateContactCustomAttribute = (uuid, data) => | ||||||
|  |   http.put(`/api/v1/conversations/${uuid}/contacts/custom-attributes`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const updateConversationCustomAttribute = (uuid, data) => | ||||||
|  |   http.put(`/api/v1/conversations/${uuid}/custom-attributes`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const createConversation = (data) => | ||||||
|  |   http.post('/api/v1/conversations', data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const updateConversationStatus = (uuid, data) => | ||||||
|  |   http.put(`/api/v1/conversations/${uuid}/status`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const updateConversationPriority = (uuid, data) => | ||||||
|  |   http.put(`/api/v1/conversations/${uuid}/priority`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
| const updateAssigneeLastSeen = (uuid) => http.put(`/api/v1/conversations/${uuid}/last-seen`) | const updateAssigneeLastSeen = (uuid) => http.put(`/api/v1/conversations/${uuid}/last-seen`) | ||||||
| const getConversationMessage = (cuuid, uuid) => http.get(`/api/v1/conversations/${cuuid}/messages/${uuid}`) | const getConversationMessage = (cuuid, uuid) => | ||||||
| const retryMessage = (cuuid, uuid) => http.put(`/api/v1/conversations/${cuuid}/messages/${uuid}/retry`) |   http.get(`/api/v1/conversations/${cuuid}/messages/${uuid}`) | ||||||
| const getConversationMessages = (uuid, params) => http.get(`/api/v1/conversations/${uuid}/messages`, { params }) | const retryMessage = (cuuid, uuid) => | ||||||
|  |   http.put(`/api/v1/conversations/${cuuid}/messages/${uuid}/retry`) | ||||||
|  | const getConversationMessages = (uuid, params) => | ||||||
|  |   http.get(`/api/v1/conversations/${uuid}/messages`, { params }) | ||||||
| const sendMessage = (uuid, data) => | const sendMessage = (uuid, data) => | ||||||
|   http.post(`/api/v1/conversations/${uuid}/messages`, data, { |   http.post(`/api/v1/conversations/${uuid}/messages`, data, { | ||||||
|     headers: { |     headers: { | ||||||
| @@ -192,28 +318,33 @@ const getConversation = (uuid) => http.get(`/api/v1/conversations/${uuid}`) | |||||||
| const getConversationParticipants = (uuid) => http.get(`/api/v1/conversations/${uuid}/participants`) | const getConversationParticipants = (uuid) => http.get(`/api/v1/conversations/${uuid}/participants`) | ||||||
| const getAllMacros = () => http.get('/api/v1/macros') | const getAllMacros = () => http.get('/api/v1/macros') | ||||||
| const getMacro = (id) => http.get(`/api/v1/macros/${id}`) | const getMacro = (id) => http.get(`/api/v1/macros/${id}`) | ||||||
| const createMacro = (data) => http.post('/api/v1/macros', data, { | const createMacro = (data) => | ||||||
|   headers: { |   http.post('/api/v1/macros', data, { | ||||||
|     'Content-Type': 'application/json' |     headers: { | ||||||
|   } |       'Content-Type': 'application/json' | ||||||
| }) |     } | ||||||
| const updateMacro = (id, data) => http.put(`/api/v1/macros/${id}`, data, { |   }) | ||||||
|   headers: { | const updateMacro = (id, data) => | ||||||
|     'Content-Type': 'application/json' |   http.put(`/api/v1/macros/${id}`, data, { | ||||||
|   } |     headers: { | ||||||
| }) |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
| const deleteMacro = (id) => http.delete(`/api/v1/macros/${id}`) | const deleteMacro = (id) => http.delete(`/api/v1/macros/${id}`) | ||||||
| const applyMacro = (uuid, id, data) => http.post(`/api/v1/conversations/${uuid}/macros/${id}/apply`, data, { | const applyMacro = (uuid, id, data) => | ||||||
|   headers: { |   http.post(`/api/v1/conversations/${uuid}/macros/${id}/apply`, data, { | ||||||
|     'Content-Type': 'application/json' |     headers: { | ||||||
|   } |       'Content-Type': 'application/json' | ||||||
| }) |     } | ||||||
|  |   }) | ||||||
| const getTeamUnassignedConversations = (teamID, params) => | const getTeamUnassignedConversations = (teamID, params) => | ||||||
|   http.get(`/api/v1/teams/${teamID}/conversations/unassigned`, { params }) |   http.get(`/api/v1/teams/${teamID}/conversations/unassigned`, { params }) | ||||||
| const getAssignedConversations = (params) => http.get('/api/v1/conversations/assigned', { params }) | const getAssignedConversations = (params) => http.get('/api/v1/conversations/assigned', { params }) | ||||||
| const getUnassignedConversations = (params) => http.get('/api/v1/conversations/unassigned', { params }) | const getUnassignedConversations = (params) => | ||||||
|  |   http.get('/api/v1/conversations/unassigned', { params }) | ||||||
| const getAllConversations = (params) => http.get('/api/v1/conversations/all', { params }) | const getAllConversations = (params) => http.get('/api/v1/conversations/all', { params }) | ||||||
| const getViewConversations = (id, params) => http.get(`/api/v1/views/${id}/conversations`, { params }) | const getViewConversations = (id, params) => | ||||||
|  |   http.get(`/api/v1/views/${id}/conversations`, { params }) | ||||||
| const uploadMedia = (data) => | const uploadMedia = (data) => | ||||||
|   http.post('/api/v1/media', data, { |   http.post('/api/v1/media', data, { | ||||||
|     headers: { |     headers: { | ||||||
| @@ -221,20 +352,9 @@ const uploadMedia = (data) => | |||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| const getOverviewCounts = () => http.get('/api/v1/reports/overview/counts') | const getOverviewCounts = () => http.get('/api/v1/reports/overview/counts') | ||||||
| const getOverviewCharts = () => http.get('/api/v1/reports/overview/charts') | const getOverviewCharts = (params) => http.get('/api/v1/reports/overview/charts', { params }) | ||||||
|  | const getOverviewSLA = (params) => http.get('/api/v1/reports/overview/sla', { params }) | ||||||
| const getLanguage = (lang) => http.get(`/api/v1/lang/${lang}`) | const getLanguage = (lang) => http.get(`/api/v1/lang/${lang}`) | ||||||
| const createUser = (data) => |  | ||||||
|   http.post('/api/v1/users', data, { |  | ||||||
|     headers: { |  | ||||||
|       'Content-Type': 'application/json' |  | ||||||
|     } |  | ||||||
|   }) |  | ||||||
| const updateUser = (id, data) => |  | ||||||
|   http.put(`/api/v1/users/${id}`, data, { |  | ||||||
|     headers: { |  | ||||||
|       'Content-Type': 'application/json' |  | ||||||
|     } |  | ||||||
|   }) |  | ||||||
| const createInbox = (data) => | const createInbox = (data) => | ||||||
|   http.post('/api/v1/inboxes', data, { |   http.post('/api/v1/inboxes', data, { | ||||||
|     headers: { |     headers: { | ||||||
| @@ -266,8 +386,50 @@ const updateView = (id, data) => | |||||||
|   }) |   }) | ||||||
| const deleteView = (id) => http.delete(`/api/v1/views/me/${id}`) | const deleteView = (id) => http.delete(`/api/v1/views/me/${id}`) | ||||||
| const getAiPrompts = () => http.get('/api/v1/ai/prompts') | const getAiPrompts = () => http.get('/api/v1/ai/prompts') | ||||||
| const aiCompletion = (data) => http.post('/api/v1/ai/completion', data) | const aiCompletion = (data) => http.post('/api/v1/ai/completion', data, { | ||||||
| const updateAIProvider = (data) => http.put('/api/v1/ai/provider', data) |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const updateAIProvider = (data) => http.put('/api/v1/ai/provider', data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const getContactNotes = (id) => http.get(`/api/v1/contacts/${id}/notes`) | ||||||
|  | const createContactNote = (id, data) => http.post(`/api/v1/contacts/${id}/notes`, data, { | ||||||
|  |   headers: { | ||||||
|  |     'Content-Type': 'application/json' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const deleteContactNote = (id, noteId) => http.delete(`/api/v1/contacts/${id}/notes/${noteId}`) | ||||||
|  | const getActivityLogs = (params) => http.get('/api/v1/activity-logs', { params }) | ||||||
|  | const getWebhooks = () => http.get('/api/v1/webhooks') | ||||||
|  | const getWebhook = (id) => http.get(`/api/v1/webhooks/${id}`) | ||||||
|  | const createWebhook = (data) => | ||||||
|  |   http.post('/api/v1/webhooks', data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const updateWebhook = (id, data) => | ||||||
|  |   http.put(`/api/v1/webhooks/${id}`, data, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | const deleteWebhook = (id) => http.delete(`/api/v1/webhooks/${id}`) | ||||||
|  | const toggleWebhook = (id) => http.put(`/api/v1/webhooks/${id}/toggle`) | ||||||
|  | const testWebhook = (id) => http.post(`/api/v1/webhooks/${id}/test`) | ||||||
|  |  | ||||||
|  | const generateAPIKey = (id) =>  | ||||||
|  |   http.post(`/api/v1/agents/${id}/api-key`, {}, { | ||||||
|  |     headers: { | ||||||
|  |       'Content-Type': 'application/json' | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  |  | ||||||
|  | const revokeAPIKey = (id) => http.delete(`/api/v1/agents/${id}/api-key`) | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|   login, |   login, | ||||||
| @@ -308,6 +470,7 @@ export default { | |||||||
|   getViewConversations, |   getViewConversations, | ||||||
|   getOverviewCharts, |   getOverviewCharts, | ||||||
|   getOverviewCounts, |   getOverviewCounts, | ||||||
|  |   getOverviewSLA, | ||||||
|   getConversationParticipants, |   getConversationParticipants, | ||||||
|   getConversationMessage, |   getConversationMessage, | ||||||
|   getConversationMessages, |   getConversationMessages, | ||||||
| @@ -324,6 +487,8 @@ export default { | |||||||
|   updateConversationStatus, |   updateConversationStatus, | ||||||
|   updateConversationPriority, |   updateConversationPriority, | ||||||
|   upsertTags, |   upsertTags, | ||||||
|  |   updateConversationCustomAttribute, | ||||||
|  |   updateContactCustomAttribute, | ||||||
|   uploadMedia, |   uploadMedia, | ||||||
|   updateAssigneeLastSeen, |   updateAssigneeLastSeen, | ||||||
|   updateUser, |   updateUser, | ||||||
| @@ -349,10 +514,9 @@ export default { | |||||||
|   updateSettings, |   updateSettings, | ||||||
|   createOIDC, |   createOIDC, | ||||||
|   getAllOIDC, |   getAllOIDC, | ||||||
|   getAllEnabledOIDC, |   getConfig, | ||||||
|   getOIDC, |   getOIDC, | ||||||
|   updateOIDC, |   updateOIDC, | ||||||
|   testOIDC, |  | ||||||
|   deleteOIDC, |   deleteOIDC, | ||||||
|   getTemplate, |   getTemplate, | ||||||
|   getTemplates, |   getTemplates, | ||||||
| @@ -382,4 +546,26 @@ export default { | |||||||
|   searchMessages, |   searchMessages, | ||||||
|   searchContacts, |   searchContacts, | ||||||
|   removeAssignee, |   removeAssignee, | ||||||
|  |   getContacts, | ||||||
|  |   getContact, | ||||||
|  |   updateContact, | ||||||
|  |   blockContact, | ||||||
|  |   getCustomAttributes, | ||||||
|  |   createCustomAttribute, | ||||||
|  |   updateCustomAttribute, | ||||||
|  |   deleteCustomAttribute, | ||||||
|  |   getCustomAttribute, | ||||||
|  |   getContactNotes, | ||||||
|  |   createContactNote, | ||||||
|  |   deleteContactNote, | ||||||
|  |   getActivityLogs, | ||||||
|  |   getWebhooks, | ||||||
|  |   getWebhook, | ||||||
|  |   createWebhook, | ||||||
|  |   updateWebhook, | ||||||
|  |   deleteWebhook, | ||||||
|  |   toggleWebhook, | ||||||
|  |   testWebhook, | ||||||
|  |   generateAPIKey, | ||||||
|  |   revokeAPIKey | ||||||
| } | } | ||||||
|   | |||||||
| @@ -13,12 +13,20 @@ | |||||||
|     min-height: 100%; |     min-height: 100%; | ||||||
|     overflow: hidden; |     overflow: hidden; | ||||||
|     margin: 0; |     margin: 0; | ||||||
|  |     @apply bg-background text-foreground; | ||||||
|  |   } | ||||||
|  |  | ||||||
|     @media (max-width: 768px) { |   @media (max-width: 768px) { | ||||||
|  |     html, | ||||||
|  |     body { | ||||||
|       overflow-x: auto; |       overflow-x: auto; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   * { | ||||||
|  |     @apply border-border; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   .native-html { |   .native-html { | ||||||
|     p { |     p { | ||||||
|       margin-bottom: 0.5rem; |       margin-bottom: 0.5rem; | ||||||
| @@ -61,10 +69,39 @@ | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } |   :root { | ||||||
|  |     --sidebar-background: 0 0% 100%; | ||||||
|  |     --sidebar-foreground: 240 5.9% 10%; | ||||||
|  |     --sidebar-primary: 240 5.9% 10%; | ||||||
|  |     --sidebar-primary-foreground: 0 0% 98%; | ||||||
|  |     --sidebar-accent: 240 4.8% 95.9%; | ||||||
|  |     --sidebar-accent-foreground: 240 5.9% 10%; | ||||||
|  |     --sidebar-border: 220 13% 91%; | ||||||
|  |     --sidebar-ring: 217.2 91.2% 59.8%; | ||||||
|  |   } | ||||||
|  |   .dark { | ||||||
|  |     --sidebar-background: 240 5.9% 10%; | ||||||
|  |     --sidebar-foreground: 240 4.8% 95.9%; | ||||||
|  |     --sidebar-primary: 224.3 76.3% 48%; | ||||||
|  |     --sidebar-primary-foreground: 0 0% 100%; | ||||||
|  |     --sidebar-accent: 240 3.7% 15.9%; | ||||||
|  |     --sidebar-accent-foreground: 240 4.8% 95.9%; | ||||||
|  |     --sidebar-border: 240 3.7% 15.9%; | ||||||
|  |     --sidebar-ring: 217.2 91.2% 59.8%; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   :root { | ||||||
|  |     --vis-tooltip-background-color: none !important; | ||||||
|  |     --vis-tooltip-border-color: none !important; | ||||||
|  |     --vis-tooltip-text-color: none !important; | ||||||
|  |     --vis-tooltip-shadow-color: none !important; | ||||||
|  |     --vis-tooltip-backdrop-filter: none !important; | ||||||
|  |     --vis-tooltip-padding: none !important; | ||||||
|  |     --vis-primary-color: var(--primary); | ||||||
|  |     --vis-secondary-color: 160 81% 40%; | ||||||
|  |     --vis-text-color: var(--muted-foreground); | ||||||
|  |   } | ||||||
|  |  | ||||||
| // Theme. |  | ||||||
| @layer base { |  | ||||||
|   :root { |   :root { | ||||||
|     --background: 0 0% 100%; |     --background: 0 0% 100%; | ||||||
|     --foreground: 240 10% 3.9%; |     --foreground: 240 10% 3.9%; | ||||||
| @@ -93,17 +130,17 @@ | |||||||
|     --border: 240 5.9% 90%; |     --border: 240 5.9% 90%; | ||||||
|     --input: 240 5.9% 90%; |     --input: 240 5.9% 90%; | ||||||
|     --ring: 240 5.9% 10%; |     --ring: 240 5.9% 10%; | ||||||
|     --radius: 0.75rem; |     --radius: 0.5rem; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   .dark { |   .dark { | ||||||
|     --background: 240 10% 3.9%; |     --background: 240 5.9% 10%; | ||||||
|     --foreground: 0 0% 98%; |     --foreground: 0 0% 98%; | ||||||
|  |  | ||||||
|     --card: 240 10% 3.9%; |     --card: 240 5.9% 10%; | ||||||
|     --card-foreground: 0 0% 98%; |     --card-foreground: 0 0% 98%; | ||||||
|  |  | ||||||
|     --popover: 240 10% 3.9%; |     --popover: 240 5.9% 10%; | ||||||
|     --popover-foreground: 0 0% 98%; |     --popover-foreground: 0 0% 98%; | ||||||
|  |  | ||||||
|     --primary: 0 0% 98%; |     --primary: 0 0% 98%; | ||||||
| @@ -127,72 +164,8 @@ | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| @layer base { |  | ||||||
|   :root { |  | ||||||
|     --vis-tooltip-background-color: none !important; |  | ||||||
|     --vis-tooltip-border-color: none !important; |  | ||||||
|     --vis-tooltip-text-color: none !important; |  | ||||||
|     --vis-tooltip-shadow-color: none !important; |  | ||||||
|     --vis-tooltip-backdrop-filter: none !important; |  | ||||||
|     --vis-tooltip-padding: none !important; |  | ||||||
|     --vis-primary-color: var(--primary); |  | ||||||
|     --vis-secondary-color: 160 81% 40%; |  | ||||||
|     --vis-text-color: var(--muted-foreground); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Shake animation |  | ||||||
| @keyframes shake { |  | ||||||
|   0% { |  | ||||||
|     transform: translateX(0); |  | ||||||
|   } |  | ||||||
|   15% { |  | ||||||
|     transform: translateX(-5px); |  | ||||||
|   } |  | ||||||
|   25% { |  | ||||||
|     transform: translateX(5px); |  | ||||||
|   } |  | ||||||
|   35% { |  | ||||||
|     transform: translateX(-5px); |  | ||||||
|   } |  | ||||||
|   45% { |  | ||||||
|     transform: translateX(5px); |  | ||||||
|   } |  | ||||||
|   55% { |  | ||||||
|     transform: translateX(-5px); |  | ||||||
|   } |  | ||||||
|   65% { |  | ||||||
|     transform: translateX(5px); |  | ||||||
|   } |  | ||||||
|   75% { |  | ||||||
|     transform: translateX(-5px); |  | ||||||
|   } |  | ||||||
|   85% { |  | ||||||
|     transform: translateX(5px); |  | ||||||
|   } |  | ||||||
|   95% { |  | ||||||
|     transform: translateX(-5px); |  | ||||||
|   } |  | ||||||
|   100% { |  | ||||||
|     transform: translateX(0); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .animate-shake { |  | ||||||
|   animation: shake 0.5s infinite; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .message-bubble { | .message-bubble { | ||||||
|   @apply flex |   @apply flex flex-col px-4 pt-2 pb-3 w-fit min-w-[30%] max-w-full border overflow-x-auto rounded shadow-sm; | ||||||
|   flex-col |  | ||||||
|   px-4 |  | ||||||
|   pt-2 |  | ||||||
|   pb-3 |  | ||||||
|   min-w-[30%] max-w-[70%] |  | ||||||
|   border |  | ||||||
|   overflow-x-auto |  | ||||||
|   rounded-xl; |  | ||||||
|   box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05); |  | ||||||
|   table { |   table { | ||||||
|     width: 100% !important; |     width: 100% !important; | ||||||
|     table-layout: fixed !important; |     table-layout: fixed !important; | ||||||
| @@ -208,7 +181,11 @@ | |||||||
| } | } | ||||||
|  |  | ||||||
| .box { | .box { | ||||||
|   @apply border shadow rounded-lg; |   @apply border shadow rounded; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | .loading-fade { | ||||||
|  |   @apply opacity-50 transition-opacity duration-300 | ||||||
| } | } | ||||||
|  |  | ||||||
| // Scrollbar start | // Scrollbar start | ||||||
| @@ -234,85 +211,6 @@ | |||||||
| } | } | ||||||
| // End Scrollbar | // End Scrollbar | ||||||
|  |  | ||||||
| .code-editor { |  | ||||||
|   @apply rounded-md border shadow h-[65vh] min-h-[250px] w-full relative; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .ql-container { |  | ||||||
|   margin: 0 !important; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .ql-container .ql-editor { |  | ||||||
|   height: 300px !important; |  | ||||||
|   border-radius: var(--radius) !important; |  | ||||||
|   @apply rounded-lg rounded-t-none; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .ql-toolbar { |  | ||||||
|   @apply rounded-t-lg; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .blinking-dot { |  | ||||||
|   display: inline-block; |  | ||||||
|   width: 8px; |  | ||||||
|   height: 8px; |  | ||||||
|   background-color: red; |  | ||||||
|   border-radius: 50%; |  | ||||||
|   animation: blink 2s infinite; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| @keyframes blink { |  | ||||||
|   0%, |  | ||||||
|   100% { |  | ||||||
|     opacity: 1; |  | ||||||
|   } |  | ||||||
|   50% { |  | ||||||
|     opacity: 0; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Sidebar start |  | ||||||
| @layer base { |  | ||||||
|   :root { |  | ||||||
|     --sidebar-background: 0 0% 96%; |  | ||||||
|     --sidebar-foreground: 240 5.3% 26.1%; |  | ||||||
|     --sidebar-primary: 240 5.9% 10%; |  | ||||||
|     --sidebar-primary-foreground: 0 0% 98%; |  | ||||||
|     --sidebar-accent: 240 4.8% 95.9%; |  | ||||||
|     --sidebar-accent-foreground: 240 5.9% 10%; |  | ||||||
|     --sidebar-border: 220 13% 91%; |  | ||||||
|     --sidebar-ring: 217.2 91.2% 59.8%; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   .dark { |  | ||||||
|     --sidebar-background: 240 5.9% 10%; |  | ||||||
|     --sidebar-foreground: 240 4.8% 95.9%; |  | ||||||
|     --sidebar-primary: 224.3 76.3% 48%; |  | ||||||
|     --sidebar-primary-foreground: 0 0% 100%; |  | ||||||
|     --sidebar-accent: 240 3.7% 15.9%; |  | ||||||
|     --sidebar-accent-foreground: 240 4.8% 95.9%; |  | ||||||
|     --sidebar-border: 240 3.7% 15.9%; |  | ||||||
|     --sidebar-ring: 217.2 91.2% 59.8%; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| a[data-active='true'] { |  | ||||||
|   background-color: hsl(var(--sidebar-background)) !important; |  | ||||||
|   color: hsl(var(--sidebar-accent-foreground)) !important; |  | ||||||
|   font-weight: 500; |  | ||||||
|   transition: |  | ||||||
|     background-color 0.2s, |  | ||||||
|     color 0.2s; |  | ||||||
| } |  | ||||||
| a[data-active='false']:hover { |  | ||||||
|   background-color: hsl(var(--sidebar-accent)) !important; |  | ||||||
|   color: hsl(var(--sidebar-accent-foreground)) !important; |  | ||||||
|   font-weight: 500; |  | ||||||
|   transition: |  | ||||||
|     background-color 0.2s, |  | ||||||
|     color 0.2s; |  | ||||||
| } |  | ||||||
| // Sidebar end |  | ||||||
|  |  | ||||||
| .show-quoted-text { | .show-quoted-text { | ||||||
|   blockquote { |   blockquote { | ||||||
|     @apply block; |     @apply block; | ||||||
| @@ -325,37 +223,13 @@ a[data-active='false']:hover { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| .dot-loader { |  | ||||||
|   display: inline-flex; |  | ||||||
|   align-items: center; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .dot { |  | ||||||
|   width: 4px; |  | ||||||
|   height: 4px; |  | ||||||
|   border-radius: 50%; |  | ||||||
|   background-color: currentColor; |  | ||||||
|   margin: 0 2px; |  | ||||||
|   animation: dot-flashing 1s infinite linear alternate; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .dot:nth-child(2) { |  | ||||||
|   animation-delay: 0.2s; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .dot:nth-child(3) { |  | ||||||
|   animation-delay: 0.4s; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| @keyframes dot-flashing { |  | ||||||
|   0% { |  | ||||||
|     opacity: 0.2; |  | ||||||
|   } |  | ||||||
|   100% { |  | ||||||
|     opacity: 1; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| [data-radix-popper-content-wrapper] { | [data-radix-popper-content-wrapper] { | ||||||
|   z-index: 9999 !important; |   z-index: 9999 !important; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // Components | ||||||
|  | @layer components { | ||||||
|  |   .link-style { | ||||||
|  |     @apply text-blue-500 hover:underline; | ||||||
|  |   } | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										63
									
								
								frontend/src/components/banner/AdminBanner.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								frontend/src/components/banner/AdminBanner.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | <template> | ||||||
|  |   <div class="border-b"> | ||||||
|  |     <!-- Update notification --> | ||||||
|  |     <div | ||||||
|  |       v-if="appSettingsStore.settings['app.update']?.update?.is_new" | ||||||
|  |       class="px-4 py-2.5 border-b border-border/50 last:border-b-0" | ||||||
|  |     > | ||||||
|  |       <div class="flex items-center gap-3"> | ||||||
|  |         <div class="flex-shrink-0"> | ||||||
|  |           <Download class="w-5 h-5 text-primary" /> | ||||||
|  |         </div> | ||||||
|  |         <div class="min-w-0 flex-1"> | ||||||
|  |           <div class="flex items-center gap-2 text-sm text-foreground"> | ||||||
|  |             <span>{{ $t('update.newUpdateAvailable') }}</span> | ||||||
|  |             <a | ||||||
|  |               :href="appSettingsStore.settings['app.update'].update.url" | ||||||
|  |               target="_blank" | ||||||
|  |               rel="nofollow noreferrer" | ||||||
|  |               class="font-semibold text-primary hover:text-primary/80 underline transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-1" | ||||||
|  |             > | ||||||
|  |               {{ appSettingsStore.settings['app.update'].update.release_version }} | ||||||
|  |             </a> | ||||||
|  |             <span class="text-muted-foreground">•</span> | ||||||
|  |             <span class="text-muted-foreground"> | ||||||
|  |               {{ appSettingsStore.settings['app.update'].update.release_date }} | ||||||
|  |             </span> | ||||||
|  |           </div> | ||||||
|  |  | ||||||
|  |           <!-- Update description --> | ||||||
|  |           <div | ||||||
|  |             v-if="appSettingsStore.settings['app.update'].update.description" | ||||||
|  |             class="mt-2 text-xs text-muted-foreground" | ||||||
|  |           > | ||||||
|  |             {{ appSettingsStore.settings['app.update'].update.description }} | ||||||
|  |           </div> | ||||||
|  |         </div> | ||||||
|  |       </div> | ||||||
|  |     </div> | ||||||
|  |  | ||||||
|  |     <!-- Restart required notification --> | ||||||
|  |     <div | ||||||
|  |       v-if="appSettingsStore.settings['app.restart_required']" | ||||||
|  |       class="px-4 py-2.5 border-b border-border/50 last:border-b-0" | ||||||
|  |     > | ||||||
|  |       <div class="flex items-center gap-3"> | ||||||
|  |         <div class="flex-shrink-0"> | ||||||
|  |           <Info class="w-5 h-5 text-primary" /> | ||||||
|  |         </div> | ||||||
|  |         <div class="min-w-0 flex-1"> | ||||||
|  |           <div class="text-sm text-foreground"> | ||||||
|  |             {{ $t('admin.banner.restartMessage') }} | ||||||
|  |           </div> | ||||||
|  |         </div> | ||||||
|  |       </div> | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  | </template> | ||||||
|  |  | ||||||
|  | <script setup> | ||||||
|  | import { Download, Info } from 'lucide-vue-next' | ||||||
|  | import { useAppSettingsStore } from '@/stores/appSettings' | ||||||
|  | const appSettingsStore = useAppSettingsStore() | ||||||
|  | </script> | ||||||
							
								
								
									
										24
									
								
								frontend/src/components/button/CloseButton.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								frontend/src/components/button/CloseButton.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | <template> | ||||||
|  |   <Button | ||||||
|  |     variant="ghost" | ||||||
|  |     @click.stop="onClose" | ||||||
|  |     size="xs" | ||||||
|  |     class="text-gray-400 dark:text-gray-500 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-gray-800 w-6 h-6 p-0" | ||||||
|  |   > | ||||||
|  |     <slot> | ||||||
|  |       <X size="16" /> | ||||||
|  |     </slot> | ||||||
|  |   </Button> | ||||||
|  | </template> | ||||||
|  |  | ||||||
|  | <script setup> | ||||||
|  | import { Button } from '@/components/ui/button' | ||||||
|  | import { X } from 'lucide-vue-next' | ||||||
|  |  | ||||||
|  | defineProps({ | ||||||
|  |   onClose: { | ||||||
|  |     type: Function, | ||||||
|  |     required: true | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | </script> | ||||||
							
								
								
									
										61
									
								
								frontend/src/components/combobox/SelectCombobox.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								frontend/src/components/combobox/SelectCombobox.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | |||||||
|  | <template> | ||||||
|  |   <ComboBox | ||||||
|  |     :model-value="normalizedValue" | ||||||
|  |     @update:model-value="$emit('update:modelValue', $event)" | ||||||
|  |     :items="items" | ||||||
|  |     :placeholder="placeholder" | ||||||
|  |   > | ||||||
|  |     <!-- Items --> | ||||||
|  |     <template #item="{ item }"> | ||||||
|  |       <div class="flex items-center gap-2"> | ||||||
|  |         <!--USER --> | ||||||
|  |         <Avatar v-if="type === 'user'" class="w-7 h-7"> | ||||||
|  |           <AvatarImage :src="item.avatar_url || ''" :alt="item.label.slice(0, 2)" /> | ||||||
|  |           <AvatarFallback>{{ item.label.slice(0, 2).toUpperCase() }}</AvatarFallback> | ||||||
|  |         </Avatar> | ||||||
|  |  | ||||||
|  |         <!-- Others --> | ||||||
|  |         <span v-else-if="item.emoji">{{ item.emoji }}</span> | ||||||
|  |         <span>{{ item.label }}</span> | ||||||
|  |       </div> | ||||||
|  |     </template> | ||||||
|  |  | ||||||
|  |     <!-- Selected --> | ||||||
|  |     <template #selected="{ selected }"> | ||||||
|  |       <div class="flex items-center gap-2"> | ||||||
|  |         <div v-if="selected" class="flex items-center gap-2"> | ||||||
|  |           <!--USER --> | ||||||
|  |           <Avatar v-if="type === 'user'" class="w-7 h-7"> | ||||||
|  |             <AvatarImage :src="selected.avatar_url || ''" :alt="selected.label.slice(0, 2)" /> | ||||||
|  |             <AvatarFallback>{{ selected.label.slice(0, 2).toUpperCase() }}</AvatarFallback> | ||||||
|  |           </Avatar> | ||||||
|  |  | ||||||
|  |           <!-- Others --> | ||||||
|  |           <span v-else-if="selected.emoji">{{ selected.emoji }}</span> | ||||||
|  |           <span>{{ selected.label }}</span> | ||||||
|  |         </div> | ||||||
|  |         <span v-else>{{ placeholder }}</span> | ||||||
|  |       </div> | ||||||
|  |     </template> | ||||||
|  |   </ComboBox> | ||||||
|  | </template> | ||||||
|  |  | ||||||
|  | <script setup> | ||||||
|  | import { computed } from 'vue' | ||||||
|  | import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar' | ||||||
|  | import ComboBox from '@/components/ui/combobox/ComboBox.vue' | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   modelValue: [String, Number, Object], | ||||||
|  |   placeholder: String, | ||||||
|  |   items: Array, | ||||||
|  |   type: { | ||||||
|  |     type: String | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | // Convert to str. | ||||||
|  | const normalizedValue = computed(() => String(props.modelValue || '')) | ||||||
|  |  | ||||||
|  | defineEmits(['update:modelValue']) | ||||||
|  | </script> | ||||||
| @@ -1,19 +1,26 @@ | |||||||
| <template> | <template> | ||||||
|   <div class="w-full"> |   <div class="w-full"> | ||||||
|     <div class="rounded-md border shadow"> |     <div class="rounded border shadow"> | ||||||
|       <Table> |       <Table> | ||||||
|         <TableHeader> |         <TableHeader> | ||||||
|           <TableRow v-for="headerGroup in table.getHeaderGroups()" :key="headerGroup.id"> |           <TableRow v-for="headerGroup in table.getHeaderGroups()" :key="headerGroup.id"> | ||||||
|             <TableHead v-for="header in headerGroup.headers" :key="header.id" class="font-semibold"> |             <TableHead v-for="header in headerGroup.headers" :key="header.id" class="font-semibold"> | ||||||
|               <FlexRender v-if="!header.isPlaceholder" :render="header.column.columnDef.header" |               <FlexRender | ||||||
|                 :props="header.getContext()" /> |                 v-if="!header.isPlaceholder" | ||||||
|  |                 :render="header.column.columnDef.header" | ||||||
|  |                 :props="header.getContext()" | ||||||
|  |               /> | ||||||
|             </TableHead> |             </TableHead> | ||||||
|           </TableRow> |           </TableRow> | ||||||
|         </TableHeader> |         </TableHeader> | ||||||
|         <TableBody> |         <TableBody> | ||||||
|           <template v-if="table.getRowModel().rows?.length"> |           <template v-if="table.getRowModel().rows?.length"> | ||||||
|             <TableRow v-for="row in table.getRowModel().rows" :key="row.id" |             <TableRow | ||||||
|               :data-state="row.getIsSelected() ? 'selected' : undefined" class="hover:bg-muted/50"> |               v-for="row in table.getRowModel().rows" | ||||||
|  |               :key="row.id" | ||||||
|  |               :data-state="row.getIsSelected() ? 'selected' : undefined" | ||||||
|  |               class="hover:bg-muted/50" | ||||||
|  |             > | ||||||
|               <TableCell v-for="cell in row.getVisibleCells()" :key="cell.id"> |               <TableCell v-for="cell in row.getVisibleCells()" :key="cell.id"> | ||||||
|                 <FlexRender :render="cell.column.columnDef.cell" :props="cell.getContext()" /> |                 <FlexRender :render="cell.column.columnDef.cell" :props="cell.getContext()" /> | ||||||
|               </TableCell> |               </TableCell> | ||||||
| @@ -32,9 +39,10 @@ | |||||||
|   </div> |   </div> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
|  |  | ||||||
| <script setup> | <script setup> | ||||||
| import { FlexRender, getCoreRowModel, useVueTable } from '@tanstack/vue-table' | import { FlexRender, getCoreRowModel, useVueTable } from '@tanstack/vue-table' | ||||||
|  | import { useI18n } from 'vue-i18n' | ||||||
|  | import { computed } from 'vue' | ||||||
|  |  | ||||||
| import { | import { | ||||||
|   Table, |   Table, | ||||||
| @@ -45,20 +53,30 @@ import { | |||||||
|   TableRow |   TableRow | ||||||
| } from '@/components/ui/table' | } from '@/components/ui/table' | ||||||
|  |  | ||||||
|  | const { t } = useI18n() | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   columns: Array, |   columns: Array, | ||||||
|   data: Array, |   data: Array, | ||||||
|   emptyText: { |   emptyText: { | ||||||
|     type: String, |     type: String, | ||||||
|     default: 'No results.' |     default: '' | ||||||
|   } |   } | ||||||
| }) | }) | ||||||
|  |  | ||||||
|  | // Set the default value for emptyText if it's empty | ||||||
|  | const emptyText = computed( | ||||||
|  |   () => | ||||||
|  |     props.emptyText || | ||||||
|  |     t('globals.messages.noResults', { | ||||||
|  |       name: t('globals.terms.result', 2).toLowerCase() | ||||||
|  |     }) | ||||||
|  | ) | ||||||
|  |  | ||||||
| const table = useVueTable({ | const table = useVueTable({ | ||||||
|   get data () { |   get data() { | ||||||
|     return props.data |     return props.data | ||||||
|   }, |   }, | ||||||
|   get columns () { |   get columns() { | ||||||
|     return props.columns |     return props.columns | ||||||
|   }, |   }, | ||||||
|   getCoreRowModel: getCoreRowModel() |   getCoreRowModel: getCoreRowModel() | ||||||
|   | |||||||
| @@ -1,10 +1,13 @@ | |||||||
| <template> | <template> | ||||||
|     <div ref="codeEditor" id="code-editor" class="code-editor" /> |     <div ref="codeEditor" @click="editorView?.focus()" class="w-full h-[28rem] border rounded-md" /> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
| <script setup> | <script setup> | ||||||
| import { ref, onMounted, watch, nextTick } from 'vue' | import { ref, onMounted, watch, nextTick, useTemplateRef } from 'vue' | ||||||
| import CodeFlask from 'codeflask' | import { EditorView, basicSetup } from 'codemirror' | ||||||
|  | import { html } from '@codemirror/lang-html' | ||||||
|  | import { oneDark } from '@codemirror/theme-one-dark' | ||||||
|  | import { useColorMode } from '@vueuse/core' | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|     modelValue: { type: String, default: '' }, |     modelValue: { type: String, default: '' }, | ||||||
| @@ -13,45 +16,38 @@ const props = defineProps({ | |||||||
| }) | }) | ||||||
|  |  | ||||||
| const emit = defineEmits(['update:modelValue']) | const emit = defineEmits(['update:modelValue']) | ||||||
| const codeEditor = ref(null) |  | ||||||
| const data = ref('') | const data = ref('') | ||||||
| const flask = ref(null) | let editorView = null  | ||||||
|  | const codeEditor = useTemplateRef('codeEditor') | ||||||
|  |  | ||||||
| const initCodeEditor = (body) => { | const initCodeEditor = (body) => { | ||||||
|     const el = document.createElement('code-flask') |     const isDark = useColorMode().value === 'dark' | ||||||
|     el.attachShadow({ mode: 'open' }) |  | ||||||
|     el.shadowRoot.innerHTML = ` |  | ||||||
|       <style> |  | ||||||
|         .codeflask .codeflask__flatten { |  | ||||||
|           font-size: 15px; |  | ||||||
|           white-space: pre-wrap; |  | ||||||
|           word-break: break-word; |  | ||||||
|         } |  | ||||||
|         .codeflask .codeflask__lines { background: #fafafa; z-index: 10; } |  | ||||||
|         .codeflask .token.tag { font-weight: bold; } |  | ||||||
|         .codeflask .token.attr-name { color: #111; } |  | ||||||
|         .codeflask .token.attr-value { color: #000 !important; } |  | ||||||
|       </style> |  | ||||||
|       <div id="area"></div> |  | ||||||
|     ` |  | ||||||
|     codeEditor.value.appendChild(el) |  | ||||||
|  |  | ||||||
|     flask.value = new CodeFlask(el.shadowRoot.getElementById('area'), { |     editorView = new EditorView({ | ||||||
|         language: props.language, |         doc: body, | ||||||
|         lineNumbers: false, |         extensions: [ | ||||||
|         styleParent: el.shadowRoot, |             basicSetup, | ||||||
|         readonly: props.disabled |             html(), | ||||||
|  |             ...(isDark ? [oneDark] : []), | ||||||
|  |             EditorView.editable.of(!props.disabled), | ||||||
|  |             EditorView.theme({ | ||||||
|  |                 '&': { height: '100%' }, | ||||||
|  |                 '.cm-editor': { height: '100%' }, | ||||||
|  |                 '.cm-scroller': { overflow: 'auto' } | ||||||
|  |             }), | ||||||
|  |             EditorView.updateListener.of((update) => { | ||||||
|  |                 if (!update.docChanged) return | ||||||
|  |                 const v = update.state.doc.toString() | ||||||
|  |                 emit('update:modelValue', v) | ||||||
|  |                 data.value = v | ||||||
|  |                  | ||||||
|  |             }) | ||||||
|  |         ], | ||||||
|  |         parent: codeEditor.value | ||||||
|     }) |     }) | ||||||
|  |  | ||||||
|     flask.value.onUpdate((v) => { |  | ||||||
|         emit('update:modelValue', v) |  | ||||||
|         data.value = v |  | ||||||
|     }) |  | ||||||
|  |  | ||||||
|     flask.value.updateCode(body) |  | ||||||
|  |  | ||||||
|     nextTick(() => { |     nextTick(() => { | ||||||
|         document.querySelector('code-flask').shadowRoot.querySelector('textarea').focus() |         editorView?.focus() | ||||||
|     }) |     }) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -61,7 +57,9 @@ onMounted(() => { | |||||||
|  |  | ||||||
| watch(() => props.modelValue, (newVal) => { | watch(() => props.modelValue, (newVal) => { | ||||||
|     if (newVal !== data.value) { |     if (newVal !== data.value) { | ||||||
|         flask.value.updateCode(newVal) |         editorView?.dispatch({ | ||||||
|  |             changes: { from: 0, to: editorView.state.doc.length, insert: newVal } | ||||||
|  |         }) | ||||||
|     } |     } | ||||||
| }) | }) | ||||||
| </script> | </script> | ||||||
							
								
								
									
										309
									
								
								frontend/src/components/editor/TextEditor.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										309
									
								
								frontend/src/components/editor/TextEditor.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,309 @@ | |||||||
|  | <template> | ||||||
|  |   <div class="editor-wrapper h-full overflow-y-auto"> | ||||||
|  |     <BubbleMenu | ||||||
|  |       :editor="editor" | ||||||
|  |       :tippy-options="{ duration: 100 }" | ||||||
|  |       v-if="editor" | ||||||
|  |       class="bg-background p-1 box will-change-transform" | ||||||
|  |     > | ||||||
|  |       <div class="flex space-x-1 items-center"> | ||||||
|  |         <DropdownMenu v-if="aiPrompts.length > 0"> | ||||||
|  |           <DropdownMenuTrigger> | ||||||
|  |             <Button size="sm" variant="ghost" class="flex items-center justify-center"> | ||||||
|  |               <span class="flex items-center"> | ||||||
|  |                 <span class="text-medium">AI</span> | ||||||
|  |                 <Bot size="14" class="ml-1" /> | ||||||
|  |                 <ChevronDown class="w-4 h-4 ml-2" /> | ||||||
|  |               </span> | ||||||
|  |             </Button> | ||||||
|  |           </DropdownMenuTrigger> | ||||||
|  |           <DropdownMenuContent> | ||||||
|  |             <DropdownMenuItem | ||||||
|  |               v-for="prompt in aiPrompts" | ||||||
|  |               :key="prompt.key" | ||||||
|  |               @select="emitPrompt(prompt.key)" | ||||||
|  |             > | ||||||
|  |               {{ prompt.title }} | ||||||
|  |             </DropdownMenuItem> | ||||||
|  |           </DropdownMenuContent> | ||||||
|  |         </DropdownMenu> | ||||||
|  |         <Button | ||||||
|  |           size="sm" | ||||||
|  |           variant="ghost" | ||||||
|  |           @click.prevent="editor?.chain().focus().toggleBold().run()" | ||||||
|  |           :class="{ 'bg-gray-200 dark:bg-secondary': editor?.isActive('bold') }" | ||||||
|  |         > | ||||||
|  |           <Bold size="14" /> | ||||||
|  |         </Button> | ||||||
|  |         <Button | ||||||
|  |           size="sm" | ||||||
|  |           variant="ghost" | ||||||
|  |           @click.prevent="editor?.chain().focus().toggleItalic().run()" | ||||||
|  |           :class="{ 'bg-gray-200 dark:bg-secondary': editor?.isActive('italic') }" | ||||||
|  |         > | ||||||
|  |           <Italic size="14" /> | ||||||
|  |         </Button> | ||||||
|  |         <Button | ||||||
|  |           size="sm" | ||||||
|  |           variant="ghost" | ||||||
|  |           @click.prevent="editor?.chain().focus().toggleBulletList().run()" | ||||||
|  |           :class="{ 'bg-gray-200 dark:bg-secondary': editor?.isActive('bulletList') }" | ||||||
|  |         > | ||||||
|  |           <List size="14" /> | ||||||
|  |         </Button> | ||||||
|  |  | ||||||
|  |         <Button | ||||||
|  |           size="sm" | ||||||
|  |           variant="ghost" | ||||||
|  |           @click.prevent="editor?.chain().focus().toggleOrderedList().run()" | ||||||
|  |           :class="{ 'bg-gray-200 dark:bg-secondary': editor?.isActive('orderedList') }" | ||||||
|  |         > | ||||||
|  |           <ListOrdered size="14" /> | ||||||
|  |         </Button> | ||||||
|  |         <Button | ||||||
|  |           size="sm" | ||||||
|  |           variant="ghost" | ||||||
|  |           @click.prevent="openLinkModal" | ||||||
|  |           :class="{ 'bg-gray-200 dark:bg-secondary': editor?.isActive('link') }" | ||||||
|  |         > | ||||||
|  |           <LinkIcon size="14" /> | ||||||
|  |         </Button> | ||||||
|  |         <div v-if="showLinkInput" class="flex space-x-2 p-2 bg-background border rounded"> | ||||||
|  |           <Input | ||||||
|  |             v-model="linkUrl" | ||||||
|  |             type="text" | ||||||
|  |             placeholder="Enter link URL" | ||||||
|  |             class="border p-1 text-sm w-[200px]" | ||||||
|  |           /> | ||||||
|  |           <Button size="sm" @click="setLink"> | ||||||
|  |             <Check size="14" /> | ||||||
|  |           </Button> | ||||||
|  |           <Button size="sm" @click="unsetLink"> | ||||||
|  |             <X size="14" /> | ||||||
|  |           </Button> | ||||||
|  |         </div> | ||||||
|  |       </div> | ||||||
|  |     </BubbleMenu> | ||||||
|  |     <EditorContent :editor="editor" class="native-html" /> | ||||||
|  |   </div> | ||||||
|  | </template> | ||||||
|  |  | ||||||
|  | <script setup> | ||||||
|  | import { ref, watch, onUnmounted } from 'vue' | ||||||
|  | import { useEditor, EditorContent, BubbleMenu } from '@tiptap/vue-3' | ||||||
|  | import { | ||||||
|  |   ChevronDown, | ||||||
|  |   Bold, | ||||||
|  |   Italic, | ||||||
|  |   Bot, | ||||||
|  |   List, | ||||||
|  |   ListOrdered, | ||||||
|  |   Link as LinkIcon, | ||||||
|  |   Check, | ||||||
|  |   X | ||||||
|  | } from 'lucide-vue-next' | ||||||
|  | import { Button } from '@/components/ui/button' | ||||||
|  | import { | ||||||
|  |   DropdownMenu, | ||||||
|  |   DropdownMenuContent, | ||||||
|  |   DropdownMenuItem, | ||||||
|  |   DropdownMenuTrigger | ||||||
|  | } from '@/components/ui/dropdown-menu' | ||||||
|  | import { Input } from '@/components/ui/input' | ||||||
|  | import Placeholder from '@tiptap/extension-placeholder' | ||||||
|  | import Image from '@tiptap/extension-image' | ||||||
|  | import StarterKit from '@tiptap/starter-kit' | ||||||
|  | import Link from '@tiptap/extension-link' | ||||||
|  | import Table from '@tiptap/extension-table' | ||||||
|  | import TableRow from '@tiptap/extension-table-row' | ||||||
|  | import TableCell from '@tiptap/extension-table-cell' | ||||||
|  | import TableHeader from '@tiptap/extension-table-header' | ||||||
|  |  | ||||||
|  | const textContent = defineModel('textContent', { default: '' }) | ||||||
|  | const htmlContent = defineModel('htmlContent', { default: '' }) | ||||||
|  | const showLinkInput = ref(false) | ||||||
|  | const linkUrl = ref('') | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   placeholder: String, | ||||||
|  |   insertContent: String, | ||||||
|  |   autoFocus: { | ||||||
|  |     type: Boolean, | ||||||
|  |     default: true | ||||||
|  |   }, | ||||||
|  |   aiPrompts: { | ||||||
|  |     type: Array, | ||||||
|  |     default: () => [] | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const emit = defineEmits(['send', 'aiPromptSelected']) | ||||||
|  |  | ||||||
|  | const emitPrompt = (key) => emit('aiPromptSelected', key) | ||||||
|  |  | ||||||
|  | // To preseve the table styling in emails, need to set the table style inline. | ||||||
|  | // Created these custom extensions to set the table style inline. | ||||||
|  | const CustomTable = Table.extend({ | ||||||
|  |   addAttributes() { | ||||||
|  |     return { | ||||||
|  |       ...this.parent?.(), | ||||||
|  |       style: { | ||||||
|  |         parseHTML: (element) => | ||||||
|  |           (element.getAttribute('style') || '') + '; border: 1px solid #dee2e6 !important; width: 100%; margin:0; table-layout: fixed; border-collapse: collapse; position:relative; border-radius: 0.25rem;' | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const CustomTableCell = TableCell.extend({ | ||||||
|  |   addAttributes() { | ||||||
|  |     return { | ||||||
|  |       ...this.parent?.(), | ||||||
|  |       style: { | ||||||
|  |         parseHTML: (element) => | ||||||
|  |           (element.getAttribute('style') || '') + | ||||||
|  |           '; border: 1px solid #dee2e6 !important; box-sizing: border-box !important; min-width: 1em !important; padding: 6px 8px !important; vertical-align: top !important;' | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const CustomTableHeader = TableHeader.extend({ | ||||||
|  |   addAttributes() { | ||||||
|  |     return { | ||||||
|  |       ...this.parent?.(), | ||||||
|  |       style: { | ||||||
|  |         parseHTML: (element) => | ||||||
|  |           (element.getAttribute('style') || '') + | ||||||
|  |           '; background-color: #f8f9fa !important; color: #212529 !important; font-weight: bold !important; text-align: left !important; border: 1px solid #dee2e6 !important; padding: 6px 8px !important;' | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const isInternalUpdate = ref(false) | ||||||
|  |  | ||||||
|  | const editor = useEditor({ | ||||||
|  |   extensions: [ | ||||||
|  |     StarterKit.configure(), | ||||||
|  |     Image.configure({ HTMLAttributes: { class: 'inline-image' } }), | ||||||
|  |     Placeholder.configure({ placeholder: () => props.placeholder }), | ||||||
|  |     Link, | ||||||
|  |     CustomTable.configure({ resizable: false }), | ||||||
|  |     TableRow, | ||||||
|  |     CustomTableCell, | ||||||
|  |     CustomTableHeader | ||||||
|  |   ], | ||||||
|  |   autofocus: props.autoFocus, | ||||||
|  |   content: htmlContent.value, | ||||||
|  |   editorProps: { | ||||||
|  |     attributes: { class: 'outline-none' }, | ||||||
|  |     handleKeyDown: (view, event) => { | ||||||
|  |       if (event.ctrlKey && event.key === 'Enter') { | ||||||
|  |         emit('send') | ||||||
|  |         return true | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |   // To update state when user types. | ||||||
|  |   onUpdate: ({ editor }) => { | ||||||
|  |     isInternalUpdate.value = true | ||||||
|  |     htmlContent.value = editor.getHTML() | ||||||
|  |     textContent.value = editor.getText() | ||||||
|  |     isInternalUpdate.value = false | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | watch( | ||||||
|  |   htmlContent, | ||||||
|  |   (newContent) => { | ||||||
|  |     if (!isInternalUpdate.value && editor.value && newContent !== editor.value.getHTML()) { | ||||||
|  |       editor.value.commands.setContent(newContent || '', false) | ||||||
|  |       textContent.value = editor.value.getText() | ||||||
|  |       editor.value.commands.focus() | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |   { immediate: true } | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Insert content at cursor position when insertContent prop changes. | ||||||
|  | watch( | ||||||
|  |   () => props.insertContent, | ||||||
|  |   (val) => { | ||||||
|  |     if (val) editor.value?.commands.insertContent(val) | ||||||
|  |   } | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | onUnmounted(() => { | ||||||
|  |   editor.value?.destroy() | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const openLinkModal = () => { | ||||||
|  |   if (editor.value?.isActive('link')) { | ||||||
|  |     linkUrl.value = editor.value.getAttributes('link').href | ||||||
|  |   } else { | ||||||
|  |     linkUrl.value = '' | ||||||
|  |   } | ||||||
|  |   showLinkInput.value = true | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const setLink = () => { | ||||||
|  |   if (linkUrl.value) { | ||||||
|  |     editor.value?.chain().focus().extendMarkRange('link').setLink({ href: linkUrl.value }).run() | ||||||
|  |   } | ||||||
|  |   showLinkInput.value = false | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const unsetLink = () => { | ||||||
|  |   editor.value?.chain().focus().unsetLink().run() | ||||||
|  |   showLinkInput.value = false | ||||||
|  | } | ||||||
|  | </script> | ||||||
|  |  | ||||||
|  | <style lang="scss"> | ||||||
|  | // Moving placeholder to the top. | ||||||
|  | .tiptap p.is-editor-empty:first-child::before { | ||||||
|  |   content: attr(data-placeholder); | ||||||
|  |   float: left; | ||||||
|  |   color: #adb5bd; | ||||||
|  |   pointer-events: none; | ||||||
|  |   height: 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Ensure the parent div has a proper height | ||||||
|  | .editor-wrapper div[aria-expanded='false'] { | ||||||
|  |   display: flex; | ||||||
|  |   flex-direction: column; | ||||||
|  |   height: 100%; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Ensure the editor content has a proper height and breaks words | ||||||
|  | .tiptap.ProseMirror { | ||||||
|  |   flex: 1; | ||||||
|  |   min-height: 70px; | ||||||
|  |   overflow-y: auto; | ||||||
|  |   word-wrap: break-word !important; | ||||||
|  |   overflow-wrap: break-word !important; | ||||||
|  |   word-break: break-word; | ||||||
|  |   white-space: pre-wrap; | ||||||
|  |   max-width: 100%; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | .tiptap { | ||||||
|  |   // Table styling | ||||||
|  |   .tableWrapper { | ||||||
|  |     margin: 1.5rem 0; | ||||||
|  |     overflow-x: auto; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   // Anchor tag styling | ||||||
|  |   a { | ||||||
|  |     color: #0066cc; | ||||||
|  |     cursor: pointer; | ||||||
|  |  | ||||||
|  |     &:hover { | ||||||
|  |       color: #003d7a; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | </style> | ||||||
							
								
								
									
										258
									
								
								frontend/src/components/filter/FilterBuilder.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										258
									
								
								frontend/src/components/filter/FilterBuilder.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,258 @@ | |||||||
|  | <template> | ||||||
|  |   <div class="space-y-4"> | ||||||
|  |     <div class="w-[27rem]" v-if="modelValue.length === 0"></div> | ||||||
|  |  | ||||||
|  |     <div | ||||||
|  |       v-for="(modelFilter, index) in modelValue" | ||||||
|  |       :key="index" | ||||||
|  |       class="group flex items-center gap-3" | ||||||
|  |     > | ||||||
|  |       <div class="flex gap-2 w-full"> | ||||||
|  |         <!-- Field --> | ||||||
|  |         <div class="flex-1"> | ||||||
|  |           <Select v-model="modelFilter.field"> | ||||||
|  |             <SelectTrigger> | ||||||
|  |               <SelectValue | ||||||
|  |                 :placeholder=" | ||||||
|  |                   t('globals.messages.select', { name: t('globals.terms.field').toLowerCase() }) | ||||||
|  |                 " | ||||||
|  |               /> | ||||||
|  |             </SelectTrigger> | ||||||
|  |             <SelectContent> | ||||||
|  |               <SelectGroup> | ||||||
|  |                 <SelectItem v-for="field in fields" :key="field.field" :value="field.field"> | ||||||
|  |                   {{ field.label }} | ||||||
|  |                 </SelectItem> | ||||||
|  |               </SelectGroup> | ||||||
|  |             </SelectContent> | ||||||
|  |           </Select> | ||||||
|  |         </div> | ||||||
|  |  | ||||||
|  |         <!-- Operator --> | ||||||
|  |         <div class="flex-1"> | ||||||
|  |           <Select v-model="modelFilter.operator" v-if="modelFilter.field"> | ||||||
|  |             <SelectTrigger> | ||||||
|  |               <SelectValue | ||||||
|  |                 :placeholder=" | ||||||
|  |                   t('globals.messages.select', { name: t('globals.terms.operator').toLowerCase() }) | ||||||
|  |                 " | ||||||
|  |               /> | ||||||
|  |             </SelectTrigger> | ||||||
|  |             <SelectContent> | ||||||
|  |               <SelectGroup> | ||||||
|  |                 <SelectItem v-for="op in getFieldOperators(modelFilter)" :key="op" :value="op"> | ||||||
|  |                   {{ op }} | ||||||
|  |                 </SelectItem> | ||||||
|  |               </SelectGroup> | ||||||
|  |             </SelectContent> | ||||||
|  |           </Select> | ||||||
|  |         </div> | ||||||
|  |  | ||||||
|  |         <!-- Value --> | ||||||
|  |         <div class="flex-1"> | ||||||
|  |           <div v-if="modelFilter.field && modelFilter.operator"> | ||||||
|  |             <template v-if="modelFilter.operator !== 'set' && modelFilter.operator !== 'not set'"> | ||||||
|  |               <SelectTag | ||||||
|  |                 v-if="getFieldType(modelFilter) === FIELD_TYPE.MULTI_SELECT" | ||||||
|  |                 v-model="modelFilter.value" | ||||||
|  |                 :items="getFieldOptions(modelFilter)" | ||||||
|  |                 :placeholder="t('globals.messages.select', { name: t('globals.terms.tag', 2) })" | ||||||
|  |               /> | ||||||
|  |  | ||||||
|  |               <SelectComboBox | ||||||
|  |                 v-else-if=" | ||||||
|  |                   getFieldOptions(modelFilter).length > 0 && | ||||||
|  |                   modelFilter.field === 'assigned_user_id' | ||||||
|  |                 " | ||||||
|  |                 v-model="modelFilter.value" | ||||||
|  |                 :items="getFieldOptions(modelFilter)" | ||||||
|  |                 :placeholder="t('globals.messages.select', { name: '' })" | ||||||
|  |                 type="user" | ||||||
|  |               /> | ||||||
|  |  | ||||||
|  |               <SelectComboBox | ||||||
|  |                 v-else-if=" | ||||||
|  |                   getFieldOptions(modelFilter).length > 0 && | ||||||
|  |                   modelFilter.field === 'assigned_team_id' | ||||||
|  |                 " | ||||||
|  |                 v-model="modelFilter.value" | ||||||
|  |                 :items="getFieldOptions(modelFilter)" | ||||||
|  |                 :placeholder="t('globals.messages.select', { name: '' })" | ||||||
|  |                 type="team" | ||||||
|  |               /> | ||||||
|  |  | ||||||
|  |               <SelectComboBox | ||||||
|  |                 v-else-if="getFieldOptions(modelFilter).length > 0" | ||||||
|  |                 v-model="modelFilter.value" | ||||||
|  |                 :items="getFieldOptions(modelFilter)" | ||||||
|  |                 :placeholder="t('globals.messages.select', { name: '' })" | ||||||
|  |               /> | ||||||
|  |  | ||||||
|  |               <Input | ||||||
|  |                 v-else | ||||||
|  |                 v-model="modelFilter.value" | ||||||
|  |                 :placeholder="t('globals.terms.value')" | ||||||
|  |                 type="text" | ||||||
|  |               /> | ||||||
|  |             </template> | ||||||
|  |           </div> | ||||||
|  |         </div> | ||||||
|  |       </div> | ||||||
|  |       <CloseButton :onClose="() => removeFilter(index)" /> | ||||||
|  |     </div> | ||||||
|  |  | ||||||
|  |     <!-- Button Container --> | ||||||
|  |     <div class="flex items-center justify-between pt-3"> | ||||||
|  |       <Button variant="ghost" size="sm" @click.stop="addFilter" class="text-slate-600"> | ||||||
|  |         <Plus class="w-3 h-3 mr-1" /> | ||||||
|  |         {{ | ||||||
|  |           $t('globals.messages.add', { | ||||||
|  |             name: $t('globals.terms.filter') | ||||||
|  |           }) | ||||||
|  |         }} | ||||||
|  |       </Button> | ||||||
|  |       <div class="flex gap-2" v-if="showButtons"> | ||||||
|  |         <Button variant="ghost" @click.stop="clearFilters"> | ||||||
|  |           {{ $t('globals.messages.reset') }} | ||||||
|  |         </Button> | ||||||
|  |         <Button @click.stop="applyFilters">{{ $t('globals.messages.apply') }}</Button> | ||||||
|  |       </div> | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  | </template> | ||||||
|  |  | ||||||
|  | <script setup> | ||||||
|  | import { computed, onMounted, onUnmounted, watch } from 'vue' | ||||||
|  | import { | ||||||
|  |   Select, | ||||||
|  |   SelectContent, | ||||||
|  |   SelectGroup, | ||||||
|  |   SelectItem, | ||||||
|  |   SelectTrigger, | ||||||
|  |   SelectValue | ||||||
|  | } from '@/components/ui/select' | ||||||
|  | import { Plus } from 'lucide-vue-next' | ||||||
|  | import { Button } from '@/components/ui/button' | ||||||
|  | import { Input } from '@/components/ui/input' | ||||||
|  | import { useI18n } from 'vue-i18n' | ||||||
|  | import { FIELD_TYPE } from '@/constants/filterConfig' | ||||||
|  | import CloseButton from '@/components/button/CloseButton.vue' | ||||||
|  | import SelectComboBox from '@/components/combobox/SelectCombobox.vue' | ||||||
|  | import SelectTag from '@/components/ui/select/SelectTag.vue' | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   fields: { | ||||||
|  |     type: Array, | ||||||
|  |     required: true | ||||||
|  |   }, | ||||||
|  |   showButtons: { | ||||||
|  |     type: Boolean, | ||||||
|  |     default: true | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | const { t } = useI18n() | ||||||
|  | const emit = defineEmits(['apply', 'clear']) | ||||||
|  | const modelValue = defineModel('modelValue', { required: false, default: () => [] }) | ||||||
|  |  | ||||||
|  | const createFilter = () => ({ field: '', operator: '', value: '' }) | ||||||
|  |  | ||||||
|  | onMounted(() => { | ||||||
|  |   if (modelValue.value.length === 0) { | ||||||
|  |     modelValue.value = [createFilter()] | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | onUnmounted(() => { | ||||||
|  |   // On unmounted set valid filters | ||||||
|  |   modelValue.value = validFilters.value | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const getModel = (field) => { | ||||||
|  |   const fieldConfig = props.fields.find((f) => f.field === field) | ||||||
|  |   return fieldConfig?.model || '' | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Set model for each filter and the default value | ||||||
|  | watch( | ||||||
|  |   () => modelValue.value, | ||||||
|  |   (filters) => { | ||||||
|  |     filters.forEach((filter) => { | ||||||
|  |       if (filter.field && !filter.model) { | ||||||
|  |         filter.model = getModel(filter.field) | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Multi select need arrays as their default value | ||||||
|  |       if ( | ||||||
|  |         filter.field && | ||||||
|  |         getFieldType(filter) === FIELD_TYPE.MULTI_SELECT && | ||||||
|  |         !Array.isArray(filter.value) | ||||||
|  |       ) { | ||||||
|  |         filter.value = [] | ||||||
|  |       } | ||||||
|  |     }) | ||||||
|  |   }, | ||||||
|  |   { deep: true } | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Reset operator and value when field changes for a filter at a given index | ||||||
|  | watch( | ||||||
|  |   modelValue, | ||||||
|  |   (newFilters, oldFilters) => { | ||||||
|  |     // Skip first run | ||||||
|  |     if (!oldFilters) return | ||||||
|  |  | ||||||
|  |     newFilters.forEach((filter, index) => { | ||||||
|  |       const oldFilter = oldFilters[index] | ||||||
|  |       if (oldFilter && filter.field !== oldFilter.field) { | ||||||
|  |         filter.operator = '' | ||||||
|  |         filter.value = '' | ||||||
|  |       } | ||||||
|  |     }) | ||||||
|  |   }, | ||||||
|  |   { deep: true } | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | const addFilter = () => { | ||||||
|  |   modelValue.value = [...modelValue.value, createFilter()] | ||||||
|  | } | ||||||
|  | const removeFilter = (index) => { | ||||||
|  |   modelValue.value = modelValue.value.filter((_, i) => i !== index) | ||||||
|  | } | ||||||
|  | const applyFilters = () => { | ||||||
|  |   modelValue.value = validFilters.value | ||||||
|  |   emit('apply', modelValue.value) | ||||||
|  | } | ||||||
|  | const clearFilters = () => { | ||||||
|  |   modelValue.value = [] | ||||||
|  |   emit('clear') | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const validFilters = computed(() => { | ||||||
|  |   return modelValue.value.filter((filter) => { | ||||||
|  |     // For multi-select field type, allow empty array as a valid value | ||||||
|  |     const field = props.fields.find((f) => f.field === filter.field) | ||||||
|  |     const isMultiSelectField = field?.type === FIELD_TYPE.MULTI_SELECT | ||||||
|  |  | ||||||
|  |     if (isMultiSelectField) { | ||||||
|  |       return filter.field && filter.operator && filter.value !== undefined && filter.value !== null | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return filter.field && filter.operator && filter.value | ||||||
|  |   }) | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const getFieldOptions = (fieldValue) => { | ||||||
|  |   const field = props.fields.find((f) => f.field === fieldValue.field) | ||||||
|  |   return field?.options || [] | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const getFieldOperators = (modelFilter) => { | ||||||
|  |   const field = props.fields.find((f) => f.field === modelFilter.field) | ||||||
|  |   return field?.operators || [] | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const getFieldType = (modelFilter) => { | ||||||
|  |   const field = props.fields.find((f) => f.field === modelFilter.field) | ||||||
|  |   return field?.type || '' | ||||||
|  | } | ||||||
|  | </script> | ||||||
| @@ -1,17 +1,17 @@ | |||||||
| <template> | <template> | ||||||
|   <div |   <div | ||||||
|     class="flex flex-col p-4 border rounded-lg shadow-sm hover:shadow transition-colors cursor-pointer max-w-xs" |     class="flex flex-col p-4 border rounded shadow-sm hover:shadow transition-colors cursor-pointer max-w-xs" | ||||||
|     @click="handleClick"> |     @click="handleClick"> | ||||||
|     <div class="flex items-center mb-2"> |     <div class="flex items-center mb-2"> | ||||||
|       <component :is="icon" size="24" class="mr-2 text-primary" /> |       <component :is="icon" size="24" class="mr-2 text-primary" /> | ||||||
|       <h3 class="text-lg font-medium text-gray-800">{{ title }}</h3> |       <h3 class="text-lg font-medium">{{ title }}</h3> | ||||||
|     </div> |     </div> | ||||||
|     <p class="text-sm text-gray-600">{{ subTitle }}</p> |     <p class="text-sm text-gray-600 dark:text-gray-400">{{ subTitle }}</p> | ||||||
|   </div> |   </div> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
| <script setup> | <script setup> | ||||||
| import { defineProps, defineEmits } from 'vue' | import { defineEmits } from 'vue' | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   title: String, |   title: String, | ||||||
|   | |||||||
| @@ -1,8 +1,8 @@ | |||||||
| <template> | <template> | ||||||
|   <div v-if="!isHidden"> |   <div v-if="!isHidden"> | ||||||
|     <div class="flex items-center space-x-4 h-12 px-2"> |     <div class="flex items-center space-x-4 h-12 px-2"> | ||||||
|       <SidebarTrigger class="cursor-pointer w-4 h-4" /> |       <SidebarTrigger class="cursor-pointer" /> | ||||||
|       <span class="text-xl font-semibold text-gray-800"> |       <span class="text-xl font-semibold"> | ||||||
|         {{ title }} |         {{ title }} | ||||||
|       </span> |       </span> | ||||||
|     </div> |     </div> | ||||||
|   | |||||||
| @@ -1,6 +1,11 @@ | |||||||
| <script setup> | <script setup> | ||||||
| import { adminNavItems, reportsNavItems, accountNavItems } from '@/constants/navigation' | import { | ||||||
| import { RouterLink, useRoute } from 'vue-router' |   adminNavItems, | ||||||
|  |   reportsNavItems, | ||||||
|  |   accountNavItems, | ||||||
|  |   contactNavItems | ||||||
|  | } from '@/constants/navigation' | ||||||
|  | import { RouterLink, useRoute, useRouter } from 'vue-router' | ||||||
| import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible' | import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible' | ||||||
| import { | import { | ||||||
|   Sidebar, |   Sidebar, | ||||||
| @@ -9,7 +14,6 @@ import { | |||||||
|   SidebarHeader, |   SidebarHeader, | ||||||
|   SidebarInset, |   SidebarInset, | ||||||
|   SidebarMenu, |   SidebarMenu, | ||||||
|   SidebarSeparator, |  | ||||||
|   SidebarMenuAction, |   SidebarMenuAction, | ||||||
|   SidebarMenuButton, |   SidebarMenuButton, | ||||||
|   SidebarMenuItem, |   SidebarMenuItem, | ||||||
| @@ -22,11 +26,11 @@ import { useAppSettingsStore } from '@/stores/appSettings' | |||||||
| import { | import { | ||||||
|   ChevronRight, |   ChevronRight, | ||||||
|   EllipsisVertical, |   EllipsisVertical, | ||||||
|  |   User, | ||||||
|  |   Search, | ||||||
|   Plus, |   Plus, | ||||||
|   CircleUserRound, |   CircleDashed, | ||||||
|   UserSearch, |   List | ||||||
|   UsersRound, |  | ||||||
|   Search |  | ||||||
| } from 'lucide-vue-next' | } from 'lucide-vue-next' | ||||||
| import { | import { | ||||||
|   DropdownMenu, |   DropdownMenu, | ||||||
| @@ -34,35 +38,35 @@ import { | |||||||
|   DropdownMenuItem, |   DropdownMenuItem, | ||||||
|   DropdownMenuTrigger |   DropdownMenuTrigger | ||||||
| } from '@/components/ui/dropdown-menu' | } from '@/components/ui/dropdown-menu' | ||||||
|  | import { | ||||||
|  |   AlertDialog, | ||||||
|  |   AlertDialogAction, | ||||||
|  |   AlertDialogCancel, | ||||||
|  |   AlertDialogContent, | ||||||
|  |   AlertDialogDescription, | ||||||
|  |   AlertDialogFooter, | ||||||
|  |   AlertDialogHeader, | ||||||
|  |   AlertDialogTitle | ||||||
|  | } from '@/components/ui/alert-dialog' | ||||||
| import { filterNavItems } from '@/utils/nav-permissions' | import { filterNavItems } from '@/utils/nav-permissions' | ||||||
| import { useStorage } from '@vueuse/core' | import { useStorage } from '@vueuse/core' | ||||||
| import { computed } from 'vue' | import { computed, ref, watch } from 'vue' | ||||||
|  | import { useI18n } from 'vue-i18n' | ||||||
| import { useUserStore } from '@/stores/user' | import { useUserStore } from '@/stores/user' | ||||||
|  | import { useConversationStore } from '@/stores/conversation' | ||||||
|  |  | ||||||
| defineProps({ | defineProps({ | ||||||
|   userTeams: { type: Array, default: () => [] }, |   userTeams: { type: Array, default: () => [] }, | ||||||
|   userViews: { type: Array, default: () => [] } |   userViews: { type: Array, default: () => [] } | ||||||
| }) | }) | ||||||
| const userStore = useUserStore() | const userStore = useUserStore() | ||||||
|  | const conversationStore = useConversationStore() | ||||||
| const settingsStore = useAppSettingsStore() | const settingsStore = useAppSettingsStore() | ||||||
| const route = useRoute() | const route = useRoute() | ||||||
|  | const router = useRouter() | ||||||
|  | const { t } = useI18n() | ||||||
| const emit = defineEmits(['createView', 'editView', 'deleteView', 'createConversation']) | const emit = defineEmits(['createView', 'editView', 'deleteView', 'createConversation']) | ||||||
|  |  | ||||||
| const openCreateViewDialog = () => { |  | ||||||
|   emit('createView') |  | ||||||
| } |  | ||||||
|  |  | ||||||
| const editView = (view) => { |  | ||||||
|   emit('editView', view) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| const deleteView = (view) => { |  | ||||||
|   emit('deleteView', view) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| const filteredAdminNavItems = computed(() => filterNavItems(adminNavItems, userStore.can)) |  | ||||||
| const filteredReportsNavItems = computed(() => filterNavItems(reportsNavItems, userStore.can)) |  | ||||||
|  |  | ||||||
| const isActiveParent = (parentHref) => { | const isActiveParent = (parentHref) => { | ||||||
|   return route.path.startsWith(parentHref) |   return route.path.startsWith(parentHref) | ||||||
| } | } | ||||||
| @@ -71,9 +75,114 @@ const isInboxRoute = (path) => { | |||||||
|   return path.startsWith('/inboxes') |   return path.startsWith('/inboxes') | ||||||
| } | } | ||||||
|  |  | ||||||
|  | const openCreateViewDialog = () => { | ||||||
|  |   emit('createView') | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const editView = (view) => { | ||||||
|  |   emit('editView', view) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const openDeleteConfirmation = (view) => { | ||||||
|  |   viewToDelete.value = view | ||||||
|  |   isDeleteOpen.value = true | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const handleDeleteView = () => { | ||||||
|  |   if (viewToDelete.value) { | ||||||
|  |     emit('deleteView', viewToDelete.value) | ||||||
|  |     isDeleteOpen.value = false | ||||||
|  |     viewToDelete.value = null | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Navigation methods with conversation retention | ||||||
|  | const navigateToInbox = (type) => { | ||||||
|  |   if (conversationStore.hasConversationOpen && conversationStore.conversation.data?.uuid) { | ||||||
|  |     router.push({ | ||||||
|  |       name: 'inbox-conversation', | ||||||
|  |       params: { | ||||||
|  |         type, | ||||||
|  |         uuid: conversationStore.conversation.data.uuid | ||||||
|  |       } | ||||||
|  |     }) | ||||||
|  |   } else { | ||||||
|  |     router.push({ | ||||||
|  |       name: 'inbox', | ||||||
|  |       params: { type } | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const navigateToTeamInbox = (teamID) => { | ||||||
|  |   if (conversationStore.hasConversationOpen && conversationStore.conversation.data?.uuid) { | ||||||
|  |     router.push({ | ||||||
|  |       name: 'team-inbox-conversation', | ||||||
|  |       params: { | ||||||
|  |         teamID, | ||||||
|  |         uuid: conversationStore.conversation.data.uuid | ||||||
|  |       } | ||||||
|  |     }) | ||||||
|  |   } else { | ||||||
|  |     router.push({ | ||||||
|  |       name: 'team-inbox', | ||||||
|  |       params: { teamID } | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const navigateToViewInbox = (viewID) => { | ||||||
|  |   if (conversationStore.hasConversationOpen && conversationStore.conversation.data?.uuid) { | ||||||
|  |     router.push({ | ||||||
|  |       name: 'view-inbox-conversation', | ||||||
|  |       params: { | ||||||
|  |         viewID, | ||||||
|  |         uuid: conversationStore.conversation.data.uuid | ||||||
|  |       } | ||||||
|  |     }) | ||||||
|  |   } else { | ||||||
|  |     router.push({ | ||||||
|  |       name: 'view-inbox', | ||||||
|  |       params: { viewID } | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const filteredAdminNavItems = computed(() => filterNavItems(adminNavItems, userStore.can)) | ||||||
|  | const filteredReportsNavItems = computed(() => filterNavItems(reportsNavItems, userStore.can)) | ||||||
|  | const filteredContactsNavItems = computed(() => filterNavItems(contactNavItems, userStore.can)) | ||||||
|  |  | ||||||
|  | // For auto opening admin collapsibles when a child route is active | ||||||
|  | const openAdminCollapsible = ref(null) | ||||||
|  | const toggleAdminCollapsible = (titleKey) => { | ||||||
|  |   openAdminCollapsible.value = openAdminCollapsible.value === titleKey ? null : titleKey | ||||||
|  | } | ||||||
|  | // Watch for route changes and update the active collapsible | ||||||
|  | watch( | ||||||
|  |   [() => route.path, filteredAdminNavItems], | ||||||
|  |   () => { | ||||||
|  |     const activeItem = filteredAdminNavItems.value.find((item) => { | ||||||
|  |       if (!item.children) return isActiveParent(item.href) | ||||||
|  |       return item.children.some((child) => isActiveParent(child.href)) | ||||||
|  |     }) | ||||||
|  |     if (activeItem) { | ||||||
|  |       openAdminCollapsible.value = activeItem.titleKey | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |   { immediate: true } | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Sidebar open state in local storage | ||||||
| const sidebarOpen = useStorage('mainSidebarOpen', true) | const sidebarOpen = useStorage('mainSidebarOpen', true) | ||||||
| const teamInboxOpen = useStorage('teamInboxOpen', true) | const teamInboxOpen = useStorage('teamInboxOpen', true) | ||||||
| const viewInboxOpen = useStorage('viewInboxOpen', true) | const viewInboxOpen = useStorage('viewInboxOpen', true) | ||||||
|  |  | ||||||
|  | // Track which view is being hovered for ellipsis menu visibility | ||||||
|  | const hoveredViewId = ref(null) | ||||||
|  |  | ||||||
|  | // Track delete confirmation dialog state | ||||||
|  | const isDeleteOpen = ref(false) | ||||||
|  | const viewToDelete = ref(null) | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
| <template> | <template> | ||||||
| @@ -82,6 +191,43 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|     :default-open="sidebarOpen" |     :default-open="sidebarOpen" | ||||||
|     v-on:update:open="sidebarOpen = $event" |     v-on:update:open="sidebarOpen = $event" | ||||||
|   > |   > | ||||||
|  |     <!-- Contacts sidebar --> | ||||||
|  |     <template | ||||||
|  |       v-if="route.matched.some((record) => record.name && record.name.startsWith('contact'))" | ||||||
|  |     > | ||||||
|  |       <Sidebar collapsible="offcanvas" class="border-r ml-12"> | ||||||
|  |         <SidebarHeader> | ||||||
|  |           <SidebarMenu> | ||||||
|  |             <SidebarMenuItem> | ||||||
|  |               <div class="px-1"> | ||||||
|  |                 <span class="font-semibold text-xl"> | ||||||
|  |                   {{ t('globals.terms.contact', 2) }} | ||||||
|  |                 </span> | ||||||
|  |               </div> | ||||||
|  |             </SidebarMenuItem> | ||||||
|  |           </SidebarMenu> | ||||||
|  |         </SidebarHeader> | ||||||
|  |         <SidebarContent> | ||||||
|  |           <SidebarGroup> | ||||||
|  |             <SidebarMenu> | ||||||
|  |               <SidebarMenuItem v-for="item in filteredContactsNavItems" :key="item.titleKey"> | ||||||
|  |                 <SidebarMenuButton :isActive="isActiveParent(item.href)" asChild> | ||||||
|  |                   <router-link :to="item.href"> | ||||||
|  |                     <span>{{ | ||||||
|  |                       t('globals.messages.all', { | ||||||
|  |                         name: t(item.titleKey, 2).toLowerCase() | ||||||
|  |                       }) | ||||||
|  |                     }}</span> | ||||||
|  |                   </router-link> | ||||||
|  |                 </SidebarMenuButton> | ||||||
|  |               </SidebarMenuItem> | ||||||
|  |             </SidebarMenu> | ||||||
|  |           </SidebarGroup> | ||||||
|  |         </SidebarContent> | ||||||
|  |         <SidebarRail /> | ||||||
|  |       </Sidebar> | ||||||
|  |     </template> | ||||||
|  |  | ||||||
|     <!-- Reports sidebar --> |     <!-- Reports sidebar --> | ||||||
|     <template |     <template | ||||||
|       v-if=" |       v-if=" | ||||||
| @@ -93,22 +239,21 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|         <SidebarHeader> |         <SidebarHeader> | ||||||
|           <SidebarMenu> |           <SidebarMenu> | ||||||
|             <SidebarMenuItem> |             <SidebarMenuItem> | ||||||
|               <SidebarMenuButton :isActive="isActiveParent('/reports/overview')" asChild> |               <div class="px-1"> | ||||||
|                 <div> |                 <span class="font-semibold text-xl"> | ||||||
|                   <span class="font-semibold text-xl">Reports</span> |                   {{ t('globals.terms.report', 2) }} | ||||||
|                 </div> |                 </span> | ||||||
|               </SidebarMenuButton> |               </div> | ||||||
|             </SidebarMenuItem> |             </SidebarMenuItem> | ||||||
|           </SidebarMenu> |           </SidebarMenu> | ||||||
|         </SidebarHeader> |         </SidebarHeader> | ||||||
|         <SidebarSeparator /> |  | ||||||
|         <SidebarContent> |         <SidebarContent> | ||||||
|           <SidebarGroup> |           <SidebarGroup> | ||||||
|             <SidebarMenu> |             <SidebarMenu> | ||||||
|               <SidebarMenuItem v-for="item in filteredReportsNavItems" :key="item.title"> |               <SidebarMenuItem v-for="item in filteredReportsNavItems" :key="item.titleKey"> | ||||||
|                 <SidebarMenuButton :isActive="isActiveParent(item.href)" asChild> |                 <SidebarMenuButton :isActive="isActiveParent(item.href)" asChild> | ||||||
|                   <router-link :to="item.href"> |                   <router-link :to="item.href"> | ||||||
|                     <span>{{ item.title }}</span> |                     <span>{{ t(item.titleKey) }}</span> | ||||||
|                   </router-link> |                   </router-link> | ||||||
|                 </SidebarMenuButton> |                 </SidebarMenuButton> | ||||||
|               </SidebarMenuItem> |               </SidebarMenuItem> | ||||||
| @@ -125,41 +270,41 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|         <SidebarHeader> |         <SidebarHeader> | ||||||
|           <SidebarMenu> |           <SidebarMenu> | ||||||
|             <SidebarMenuItem> |             <SidebarMenuItem> | ||||||
|               <SidebarMenuButton :isActive="isActiveParent('/admin')" asChild> |               <div class="flex flex-col items-start justify-between w-full px-1"> | ||||||
|                 <div class="flex items-center justify-between w-full"> |                 <span class="font-semibold text-xl"> | ||||||
|                   <span class="font-semibold text-xl">Admin</span> |                   {{ t('globals.terms.admin') }} | ||||||
|                 </div> |                 </span> | ||||||
|                 <!-- App version --> |                 <!-- App version --> | ||||||
|                 <div class="text-xs text-muted-foreground ml-2"> |                 <div class="text-xs text-muted-foreground"> | ||||||
|                   ({{ settingsStore.settings['app.version'] }}) |                   ({{ settingsStore.settings['app.version'] }}) | ||||||
|                 </div> |                 </div> | ||||||
|               </SidebarMenuButton> |               </div> | ||||||
|             </SidebarMenuItem> |             </SidebarMenuItem> | ||||||
|           </SidebarMenu> |           </SidebarMenu> | ||||||
|         </SidebarHeader> |         </SidebarHeader> | ||||||
|         <SidebarSeparator /> |  | ||||||
|         <SidebarContent> |         <SidebarContent> | ||||||
|           <SidebarGroup> |           <SidebarGroup> | ||||||
|             <SidebarMenu> |             <SidebarMenu> | ||||||
|               <SidebarMenuItem v-for="item in filteredAdminNavItems" :key="item.title"> |               <SidebarMenuItem v-for="item in filteredAdminNavItems" :key="item.titleKey"> | ||||||
|                 <SidebarMenuButton |                 <SidebarMenuButton | ||||||
|                   v-if="!item.children" |                   v-if="!item.children" | ||||||
|                   :isActive="isActiveParent(item.href)" |                   :isActive="isActiveParent(item.href)" | ||||||
|                   asChild |                   asChild | ||||||
|                 > |                 > | ||||||
|                   <router-link :to="item.href"> |                   <router-link :to="item.href"> | ||||||
|                     <span>{{ item.title }}</span> |                     <span>{{ t(item.titleKey) }}</span> | ||||||
|                   </router-link> |                   </router-link> | ||||||
|                 </SidebarMenuButton> |                 </SidebarMenuButton> | ||||||
|  |  | ||||||
|                 <Collapsible |                 <Collapsible | ||||||
|                   v-else |                   v-else | ||||||
|                   class="group/collapsible" |                   class="group/collapsible" | ||||||
|                   :default-open="isActiveParent(item.href)" |                   :open="openAdminCollapsible === item.titleKey" | ||||||
|  |                   @update:open="toggleAdminCollapsible(item.titleKey)" | ||||||
|                 > |                 > | ||||||
|                   <CollapsibleTrigger as-child> |                   <CollapsibleTrigger as-child> | ||||||
|                     <SidebarMenuButton :isActive="isActiveParent(item.href)"> |                     <SidebarMenuButton :isActive="isActiveParent(item.href)"> | ||||||
|                       <span>{{ item.title }}</span> |                       <span>{{ t(item.titleKey, item.isTitleKeyPlural === true ? 2 : 1) }}</span> | ||||||
|                       <ChevronRight |                       <ChevronRight | ||||||
|                         class="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" |                         class="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" | ||||||
|                       /> |                       /> | ||||||
| @@ -167,10 +312,10 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|                   </CollapsibleTrigger> |                   </CollapsibleTrigger> | ||||||
|                   <CollapsibleContent> |                   <CollapsibleContent> | ||||||
|                     <SidebarMenuSub> |                     <SidebarMenuSub> | ||||||
|                       <SidebarMenuSubItem v-for="child in item.children" :key="child.title"> |                       <SidebarMenuSubItem v-for="child in item.children" :key="child.titleKey"> | ||||||
|                         <SidebarMenuButton size="sm" :isActive="isActiveParent(child.href)" asChild> |                         <SidebarMenuButton size="sm" :isActive="isActiveParent(child.href)" asChild> | ||||||
|                           <router-link :to="child.href"> |                           <router-link :to="child.href"> | ||||||
|                             <span>{{ child.title }}</span> |                             <span>{{ t(child.titleKey) }}</span> | ||||||
|                           </router-link> |                           </router-link> | ||||||
|                         </SidebarMenuButton> |                         </SidebarMenuButton> | ||||||
|                       </SidebarMenuSubItem> |                       </SidebarMenuSubItem> | ||||||
| @@ -191,22 +336,21 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|         <SidebarHeader> |         <SidebarHeader> | ||||||
|           <SidebarMenu> |           <SidebarMenu> | ||||||
|             <SidebarMenuItem> |             <SidebarMenuItem> | ||||||
|               <SidebarMenuButton :isActive="isActiveParent('/account/profile')" asChild> |               <div class="px-1"> | ||||||
|                 <div> |                 <span class="font-semibold text-xl"> | ||||||
|                   <span class="font-semibold text-xl">Account</span> |                   {{ t('globals.terms.account') }} | ||||||
|                 </div> |                 </span> | ||||||
|               </SidebarMenuButton> |               </div> | ||||||
|             </SidebarMenuItem> |             </SidebarMenuItem> | ||||||
|           </SidebarMenu> |           </SidebarMenu> | ||||||
|         </SidebarHeader> |         </SidebarHeader> | ||||||
|         <SidebarSeparator /> |  | ||||||
|         <SidebarContent> |         <SidebarContent> | ||||||
|           <SidebarGroup> |           <SidebarGroup> | ||||||
|             <SidebarMenu> |             <SidebarMenu> | ||||||
|               <SidebarMenuItem v-for="item in accountNavItems" :key="item.title"> |               <SidebarMenuItem v-for="item in accountNavItems" :key="item.titleKey"> | ||||||
|                 <SidebarMenuButton :isActive="isActiveParent(item.href)" asChild> |                 <SidebarMenuButton :isActive="isActiveParent(item.href)" asChild> | ||||||
|                   <router-link :to="item.href"> |                   <router-link :to="item.href"> | ||||||
|                     <span>{{ item.title }}</span> |                     <span>{{ t(item.titleKey) }}</span> | ||||||
|                   </router-link> |                   </router-link> | ||||||
|                 </SidebarMenuButton> |                 </SidebarMenuButton> | ||||||
|                 <SidebarMenuAction> |                 <SidebarMenuAction> | ||||||
| @@ -226,65 +370,65 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|         <SidebarHeader> |         <SidebarHeader> | ||||||
|           <SidebarMenu> |           <SidebarMenu> | ||||||
|             <SidebarMenuItem> |             <SidebarMenuItem> | ||||||
|               <SidebarMenuButton asChild> |               <div class="flex items-center justify-between w-full px-1"> | ||||||
|                 <div class="flex items-center justify-between w-full"> |                 <div class="font-semibold text-xl"> | ||||||
|                   <div class="font-semibold text-xl">Inbox</div> |                   <span>{{ t('globals.terms.inbox') }}</span> | ||||||
|                   <div class="ml-auto"> |  | ||||||
|                     <div class="flex items-center space-x-2"> |  | ||||||
|                       <div |  | ||||||
|                         class="flex items-center bg-accent p-2 rounded-full cursor-pointer" |  | ||||||
|                         @click="emit('createConversation')" |  | ||||||
|                       > |  | ||||||
|                         <Plus |  | ||||||
|                           class="transition-transform duration-200 hover:scale-110" |  | ||||||
|                           size="15" |  | ||||||
|                           stroke-width="2.5" |  | ||||||
|                         /> |  | ||||||
|                       </div> |  | ||||||
|                       <router-link :to="{ name: 'search' }"> |  | ||||||
|                         <div class="flex items-center bg-accent p-2 rounded-full"> |  | ||||||
|                           <Search |  | ||||||
|                             class="transition-transform duration-200 hover:scale-110 cursor-pointer" |  | ||||||
|                             size="15" |  | ||||||
|                             stroke-width="2.5" |  | ||||||
|                           /> |  | ||||||
|                         </div> |  | ||||||
|                       </router-link> |  | ||||||
|                     </div> |  | ||||||
|                   </div> |  | ||||||
|                 </div> |                 </div> | ||||||
|               </SidebarMenuButton> |                 <div class="mr-1 mt-1 hover:scale-110 transition-transform"> | ||||||
|  |                   <router-link :to="{ name: 'search' }"> | ||||||
|  |                     <Search size="18" stroke-width="2.5" /> | ||||||
|  |                   </router-link> | ||||||
|  |                 </div> | ||||||
|  |               </div> | ||||||
|             </SidebarMenuItem> |             </SidebarMenuItem> | ||||||
|           </SidebarMenu> |           </SidebarMenu> | ||||||
|         </SidebarHeader> |         </SidebarHeader> | ||||||
|         <SidebarSeparator /> |  | ||||||
|         <SidebarContent> |         <SidebarContent> | ||||||
|           <SidebarGroup> |           <SidebarGroup> | ||||||
|             <SidebarMenu> |             <SidebarMenu> | ||||||
|  |               <SidebarMenuItem> | ||||||
|  |                 <SidebarMenuButton asChild> | ||||||
|  |                   <a href="#" @click="emit('createConversation')"> | ||||||
|  |                     <Plus /> | ||||||
|  |                     <span | ||||||
|  |                       >{{ | ||||||
|  |                         t('globals.messages.new', { | ||||||
|  |                           name: t('globals.terms.conversation').toLowerCase() | ||||||
|  |                         }) | ||||||
|  |                       }} | ||||||
|  |                     </span> | ||||||
|  |                   </a> | ||||||
|  |                 </SidebarMenuButton> | ||||||
|  |               </SidebarMenuItem> | ||||||
|               <SidebarMenuItem> |               <SidebarMenuItem> | ||||||
|                 <SidebarMenuButton asChild :isActive="isActiveParent('/inboxes/assigned')"> |                 <SidebarMenuButton asChild :isActive="isActiveParent('/inboxes/assigned')"> | ||||||
|                   <router-link :to="{ name: 'inbox', params: { type: 'assigned' } }"> |                   <a href="#" @click.prevent="navigateToInbox('assigned')"> | ||||||
|                     <CircleUserRound /> |                     <User /> | ||||||
|                     <span>My inbox</span> |                     <span>{{ t('globals.terms.myInbox') }}</span> | ||||||
|                   </router-link> |                   </a> | ||||||
|                 </SidebarMenuButton> |                 </SidebarMenuButton> | ||||||
|               </SidebarMenuItem> |               </SidebarMenuItem> | ||||||
|  |  | ||||||
|               <SidebarMenuItem> |               <SidebarMenuItem> | ||||||
|                 <SidebarMenuButton asChild :isActive="isActiveParent('/inboxes/unassigned')"> |                 <SidebarMenuButton asChild :isActive="isActiveParent('/inboxes/unassigned')"> | ||||||
|                   <router-link :to="{ name: 'inbox', params: { type: 'unassigned' } }"> |                   <a href="#" @click.prevent="navigateToInbox('unassigned')"> | ||||||
|                     <UserSearch /> |                     <CircleDashed /> | ||||||
|                     <span>Unassigned</span> |                     <span> | ||||||
|                   </router-link> |                       {{ t('globals.terms.unassigned') }} | ||||||
|  |                     </span> | ||||||
|  |                   </a> | ||||||
|                 </SidebarMenuButton> |                 </SidebarMenuButton> | ||||||
|               </SidebarMenuItem> |               </SidebarMenuItem> | ||||||
|  |  | ||||||
|               <SidebarMenuItem> |               <SidebarMenuItem> | ||||||
|                 <SidebarMenuButton asChild :isActive="isActiveParent('/inboxes/all')"> |                 <SidebarMenuButton asChild :isActive="isActiveParent('/inboxes/all')"> | ||||||
|                   <router-link :to="{ name: 'inbox', params: { type: 'all' } }"> |                   <a href="#" @click.prevent="navigateToInbox('all')"> | ||||||
|                     <UsersRound /> |                     <List /> | ||||||
|                     <span>All</span> |                     <span> | ||||||
|                   </router-link> |                       {{ t('globals.messages.all') }} | ||||||
|  |                     </span> | ||||||
|  |                   </a> | ||||||
|                 </SidebarMenuButton> |                 </SidebarMenuButton> | ||||||
|               </SidebarMenuItem> |               </SidebarMenuItem> | ||||||
|  |  | ||||||
| @@ -300,7 +444,9 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|                     <SidebarMenuButton asChild> |                     <SidebarMenuButton asChild> | ||||||
|                       <router-link to="#"> |                       <router-link to="#"> | ||||||
|                         <!-- <Users /> --> |                         <!-- <Users /> --> | ||||||
|                         <span>Team inboxes</span> |                         <span> | ||||||
|  |                           {{ t('globals.terms.teamInbox', 2) }} | ||||||
|  |                         </span> | ||||||
|                         <ChevronRight |                         <ChevronRight | ||||||
|                           class="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" |                           class="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" | ||||||
|                         /> |                         /> | ||||||
| @@ -315,9 +461,9 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|                           :is-active="route.params.teamID == team.id" |                           :is-active="route.params.teamID == team.id" | ||||||
|                           asChild |                           asChild | ||||||
|                         > |                         > | ||||||
|                           <router-link :to="{ name: 'team-inbox', params: { teamID: team.id } }"> |                           <a href="#" @click.prevent="navigateToTeamInbox(team.id)"> | ||||||
|                             {{ team.emoji }}<span>{{ team.name }}</span> |                             {{ team.emoji }}<span>{{ team.name }}</span> | ||||||
|                           </router-link> |                           </a> | ||||||
|                         </SidebarMenuButton> |                         </SidebarMenuButton> | ||||||
|                       </SidebarMenuSubItem> |                       </SidebarMenuSubItem> | ||||||
|                     </SidebarMenuSub> |                     </SidebarMenuSub> | ||||||
| @@ -328,16 +474,18 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|               <!-- Views --> |               <!-- Views --> | ||||||
|               <Collapsible class="group/collapsible" defaultOpen v-model:open="viewInboxOpen"> |               <Collapsible class="group/collapsible" defaultOpen v-model:open="viewInboxOpen"> | ||||||
|                 <SidebarMenuItem> |                 <SidebarMenuItem> | ||||||
|                   <CollapsibleTrigger as-child> |                   <CollapsibleTrigger asChild> | ||||||
|                     <SidebarMenuButton asChild> |                     <SidebarMenuButton asChild> | ||||||
|                       <router-link to="#"> |                       <router-link to="#" class="group/item !p-2"> | ||||||
|                         <!-- <SlidersHorizontal /> --> |                         <!-- <SlidersHorizontal /> --> | ||||||
|                         <span>Views</span> |                         <span> | ||||||
|  |                           {{ t('globals.terms.view', 2) }} | ||||||
|  |                         </span> | ||||||
|                         <div> |                         <div> | ||||||
|                           <Plus |                           <Plus | ||||||
|                             size="18" |                             size="18" | ||||||
|                             @click.stop="openCreateViewDialog" |                             @click.stop="openCreateViewDialog" | ||||||
|                             class="rounded-lg cursor-pointer opacity-0 transition-all duration-200 group-hover:opacity-100 hover:bg-gray-200 hover:shadow-sm text-gray-600 hover:text-gray-800 transform hover:scale-105 active:scale-100 p-1" |                             class="rounded cursor-pointer opacity-0 transition-all duration-200 group-hover/item:opacity-100 hover:bg-gray-200 hover:shadow-sm text-gray-600 hover:text-gray-800 transform hover:scale-105 active:scale-100 p-1" | ||||||
|                           /> |                           /> | ||||||
|                         </div> |                         </div> | ||||||
|                         <ChevronRight |                         <ChevronRight | ||||||
| @@ -350,30 +498,41 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|  |  | ||||||
|                   <CollapsibleContent> |                   <CollapsibleContent> | ||||||
|                     <SidebarMenuSub v-for="view in userViews" :key="view.id"> |                     <SidebarMenuSub v-for="view in userViews" :key="view.id"> | ||||||
|                       <SidebarMenuSubItem> |                       <SidebarMenuSubItem | ||||||
|  |                         @mouseenter="hoveredViewId = view.id" | ||||||
|  |                         @mouseleave="hoveredViewId = null" | ||||||
|  |                       > | ||||||
|                         <SidebarMenuButton |                         <SidebarMenuButton | ||||||
|                           size="sm" |                           size="sm" | ||||||
|                           :isActive="route.params.viewID == view.id" |                           :isActive="route.params.viewID == view.id" | ||||||
|                           asChild |                           asChild | ||||||
|                         > |                         > | ||||||
|                           <router-link :to="{ name: 'view-inbox', params: { viewID: view.id } }"> |                           <a href="#" @click.prevent="navigateToViewInbox(view.id)"> | ||||||
|                             <span class="break-words w-32 truncate">{{ view.name }}</span> |                             <span class="break-words w-32 truncate" :title="view.name">{{ view.name }}</span> | ||||||
|                             <SidebarMenuAction :showOnHover="true" class="mr-3"> |                             <SidebarMenuAction | ||||||
|  |                               @click.stop | ||||||
|  |                               :class="[ | ||||||
|  |                                 'mr-3', | ||||||
|  |                                 'md:opacity-0', | ||||||
|  |                                 'data-[state=open]:opacity-100', | ||||||
|  |                                 { 'md:opacity-100': hoveredViewId === view.id } | ||||||
|  |                               ]" | ||||||
|  |                             > | ||||||
|                               <DropdownMenu> |                               <DropdownMenu> | ||||||
|                                 <DropdownMenuTrigger asChild> |                                 <DropdownMenuTrigger asChild @click.prevent> | ||||||
|                                   <EllipsisVertical /> |                                   <EllipsisVertical /> | ||||||
|                                 </DropdownMenuTrigger> |                                 </DropdownMenuTrigger> | ||||||
|                                 <DropdownMenuContent> |                                 <DropdownMenuContent> | ||||||
|                                   <DropdownMenuItem @click="() => editView(view)"> |                                   <DropdownMenuItem @click="() => editView(view)"> | ||||||
|                                     <span>Edit</span> |                                     <span>{{ t('globals.messages.edit') }}</span> | ||||||
|                                   </DropdownMenuItem> |                                   </DropdownMenuItem> | ||||||
|                                   <DropdownMenuItem @click="() => deleteView(view)"> |                                   <DropdownMenuItem @click="() => openDeleteConfirmation(view)"> | ||||||
|                                     <span>Delete</span> |                                     <span>{{ t('globals.messages.delete') }}</span> | ||||||
|                                   </DropdownMenuItem> |                                   </DropdownMenuItem> | ||||||
|                                 </DropdownMenuContent> |                                 </DropdownMenuContent> | ||||||
|                               </DropdownMenu> |                               </DropdownMenu> | ||||||
|                             </SidebarMenuAction> |                             </SidebarMenuAction> | ||||||
|                           </router-link> |                           </a> | ||||||
|                         </SidebarMenuButton> |                         </SidebarMenuButton> | ||||||
|                       </SidebarMenuSubItem> |                       </SidebarMenuSubItem> | ||||||
|                     </SidebarMenuSub> |                     </SidebarMenuSub> | ||||||
| @@ -391,4 +550,22 @@ const viewInboxOpen = useStorage('viewInboxOpen', true) | |||||||
|       <slot></slot> |       <slot></slot> | ||||||
|     </SidebarInset> |     </SidebarInset> | ||||||
|   </SidebarProvider> |   </SidebarProvider> | ||||||
|  |  | ||||||
|  |   <!-- View Delete Confirmation Dialog --> | ||||||
|  |   <AlertDialog v-model:open="isDeleteOpen"> | ||||||
|  |     <AlertDialogContent> | ||||||
|  |       <AlertDialogHeader> | ||||||
|  |         <AlertDialogTitle>{{ t('globals.messages.areYouAbsolutelySure') }}</AlertDialogTitle> | ||||||
|  |         <AlertDialogDescription> | ||||||
|  |           {{ t('globals.messages.deletionConfirmation', { name: t('globals.terms.view') }) }} | ||||||
|  |         </AlertDialogDescription> | ||||||
|  |       </AlertDialogHeader> | ||||||
|  |       <AlertDialogFooter> | ||||||
|  |         <AlertDialogCancel>{{ t('globals.messages.cancel') }}</AlertDialogCancel> | ||||||
|  |         <AlertDialogAction @click="handleDeleteView"> | ||||||
|  |           {{ t('globals.messages.delete') }} | ||||||
|  |         </AlertDialogAction> | ||||||
|  |       </AlertDialogFooter> | ||||||
|  |     </AlertDialogContent> | ||||||
|  |   </AlertDialog> | ||||||
| </template> | </template> | ||||||
|   | |||||||
| @@ -2,12 +2,12 @@ | |||||||
|   <DropdownMenu> |   <DropdownMenu> | ||||||
|     <DropdownMenuTrigger as-child> |     <DropdownMenuTrigger as-child> | ||||||
|       <SidebarMenuButton |       <SidebarMenuButton | ||||||
|         size="lg" |         size="md" | ||||||
|         class="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground p-0" |         class="p-0" | ||||||
|       > |       > | ||||||
|         <Avatar class="h-8 w-8 rounded-lg relative overflow-visible"> |         <Avatar class="h-8 w-8 rounded relative overflow-visible"> | ||||||
|           <AvatarImage :src="userStore.avatar" alt="" class="rounded-lg" /> |           <AvatarImage :src="userStore.avatar" alt="U" class="rounded" /> | ||||||
|           <AvatarFallback class="rounded-lg"> |           <AvatarFallback class="rounded"> | ||||||
|             {{ userStore.getInitials }} |             {{ userStore.getInitials }} | ||||||
|           </AvatarFallback> |           </AvatarFallback> | ||||||
|           <div |           <div | ||||||
| @@ -16,7 +16,8 @@ | |||||||
|               'bg-green-500': userStore.user.availability_status === 'online', |               'bg-green-500': userStore.user.availability_status === 'online', | ||||||
|               'bg-amber-500': |               'bg-amber-500': | ||||||
|                 userStore.user.availability_status === 'away' || |                 userStore.user.availability_status === 'away' || | ||||||
|                 userStore.user.availability_status === 'away_manual', |                 userStore.user.availability_status === 'away_manual' || | ||||||
|  |                 userStore.user.availability_status === 'away_and_reassigning', | ||||||
|               'bg-gray-400': userStore.user.availability_status === 'offline' |               'bg-gray-400': userStore.user.availability_status === 'offline' | ||||||
|             }" |             }" | ||||||
|           ></div> |           ></div> | ||||||
| @@ -29,51 +30,86 @@ | |||||||
|       </SidebarMenuButton> |       </SidebarMenuButton> | ||||||
|     </DropdownMenuTrigger> |     </DropdownMenuTrigger> | ||||||
|     <DropdownMenuContent |     <DropdownMenuContent | ||||||
|       class="w-[--radix-dropdown-menu-trigger-width] min-w-56 rounded-lg" |       class="w-[--radix-dropdown-menu-trigger-width] min-w-56" | ||||||
|       side="bottom" |       side="bottom" | ||||||
|       :side-offset="4" |       :side-offset="4" | ||||||
|     > |     > | ||||||
|       <DropdownMenuLabel class="p-0 font-normal space-y-1"> |       <DropdownMenuLabel class="font-normal space-y-2 px-2"> | ||||||
|         <div class="flex items-center gap-2 px-1 py-1.5 text-left text-sm"> |         <!-- User header --> | ||||||
|           <Avatar class="h-8 w-8 rounded-lg"> |         <div class="flex items-center gap-2 py-1.5 text-left text-sm"> | ||||||
|             <AvatarImage :src="userStore.avatar" alt="Abhinav" /> |           <Avatar class="h-8 w-8 rounded"> | ||||||
|             <AvatarFallback class="rounded-lg"> |             <AvatarImage :src="userStore.avatar" alt="U" /> | ||||||
|  |             <AvatarFallback class="rounded"> | ||||||
|               {{ userStore.getInitials }} |               {{ userStore.getInitials }} | ||||||
|             </AvatarFallback> |             </AvatarFallback> | ||||||
|           </Avatar> |           </Avatar> | ||||||
|           <div class="grid flex-1 text-left text-sm leading-tight"> |           <div class="flex-1 flex flex-col leading-tight"> | ||||||
|             <span class="truncate font-semibold">{{ userStore.getFullName }}</span> |             <span class="truncate font-semibold">{{ userStore.getFullName }}</span> | ||||||
|             <span class="truncate text-xs">{{ userStore.email }}</span> |             <span class="truncate text-xs text-muted-foreground">{{ userStore.email }}</span> | ||||||
|           </div> |           </div> | ||||||
|         </div> |         </div> | ||||||
|         <div class="flex items-center gap-2 px-1 py-1.5 text-left text-sm justify-between"> |  | ||||||
|           <span class="text-muted-foreground">Away</span> |         <div class="space-y-2"> | ||||||
|           <Switch |           <!-- Dark-mode toggle --> | ||||||
|             :checked=" |           <div class="flex items-center justify-between text-sm"> | ||||||
|               userStore.user.availability_status === 'away' || |             <div class="flex items-center gap-2"> | ||||||
|               userStore.user.availability_status === 'away_manual' |               <Moon v-if="mode === 'dark'" size="16" class="text-muted-foreground" /> | ||||||
|             " |               <Sun v-else size="16" class="text-muted-foreground" /> | ||||||
|             @update:checked="(val) => userStore.updateUserAvailability(val ? 'away' : 'online')" |               <span class="text-muted-foreground">{{ t('navigation.darkMode') }}</span> | ||||||
|           /> |             </div> | ||||||
|  |             <Switch | ||||||
|  |               :checked="mode === 'dark'" | ||||||
|  |               @update:checked="(val) => (mode = val ? 'dark' : 'light')" | ||||||
|  |             /> | ||||||
|  |           </div> | ||||||
|  |  | ||||||
|  |           <div class="border-t border-gray-200 dark:border-gray-700 pt-3 space-y-3"> | ||||||
|  |             <!-- Away toggle --> | ||||||
|  |             <div class="flex items-center justify-between text-sm"> | ||||||
|  |               <span class="text-muted-foreground">{{ t('navigation.away') }}</span> | ||||||
|  |               <Switch | ||||||
|  |                 :checked=" | ||||||
|  |                   ['away_manual', 'away_and_reassigning'].includes( | ||||||
|  |                     userStore.user.availability_status | ||||||
|  |                   ) | ||||||
|  |                 " | ||||||
|  |                 @update:checked=" | ||||||
|  |                   (val) => userStore.updateUserAvailability(val ? 'away_manual' : 'online') | ||||||
|  |                 " | ||||||
|  |               /> | ||||||
|  |             </div> | ||||||
|  |             <!-- Reassign toggle --> | ||||||
|  |             <div class="flex items-center justify-between text-sm"> | ||||||
|  |               <span class="text-muted-foreground">{{ t('navigation.reassignReplies') }}</span> | ||||||
|  |               <Switch | ||||||
|  |                 :checked="userStore.user.availability_status === 'away_and_reassigning'" | ||||||
|  |                 @update:checked=" | ||||||
|  |                   (val) => | ||||||
|  |                     userStore.updateUserAvailability(val ? 'away_and_reassigning' : 'away_manual') | ||||||
|  |                 " | ||||||
|  |               /> | ||||||
|  |             </div> | ||||||
|  |           </div> | ||||||
|         </div> |         </div> | ||||||
|       </DropdownMenuLabel> |       </DropdownMenuLabel> | ||||||
|       <DropdownMenuSeparator /> |       <DropdownMenuSeparator /> | ||||||
|       <DropdownMenuGroup> |       <DropdownMenuGroup> | ||||||
|         <DropdownMenuItem @click.prevent="router.push({ name: 'account' })"> |         <DropdownMenuItem @click.prevent="router.push({ name: 'account' })"> | ||||||
|           <CircleUserRound size="18" class="mr-2" /> |           <CircleUserRound size="18" class="mr-2" /> | ||||||
|           Account |           {{ t('globals.terms.account') }} | ||||||
|         </DropdownMenuItem> |         </DropdownMenuItem> | ||||||
|       </DropdownMenuGroup> |       </DropdownMenuGroup> | ||||||
|       <DropdownMenuSeparator /> |       <DropdownMenuSeparator /> | ||||||
|       <DropdownMenuItem @click="logout"> |       <DropdownMenuItem @click="logout"> | ||||||
|         <LogOut size="18" class="mr-2" /> |         <LogOut size="18" class="mr-2" /> | ||||||
|         Log out |         {{ t('navigation.logout') }} | ||||||
|       </DropdownMenuItem> |       </DropdownMenuItem> | ||||||
|     </DropdownMenuContent> |     </DropdownMenuContent> | ||||||
|   </DropdownMenu> |   </DropdownMenu> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
| <script setup> | <script setup> | ||||||
|  | import { useI18n } from 'vue-i18n' | ||||||
| import { | import { | ||||||
|   DropdownMenu, |   DropdownMenu, | ||||||
|   DropdownMenuContent, |   DropdownMenuContent, | ||||||
| @@ -86,12 +122,16 @@ import { | |||||||
| import { SidebarMenuButton } from '@/components/ui/sidebar' | import { SidebarMenuButton } from '@/components/ui/sidebar' | ||||||
| import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' | import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' | ||||||
| import { Switch } from '@/components/ui/switch' | import { Switch } from '@/components/ui/switch' | ||||||
| import { ChevronsUpDown, CircleUserRound, LogOut } from 'lucide-vue-next' | import { ChevronsUpDown, CircleUserRound, LogOut, Moon, Sun } from 'lucide-vue-next' | ||||||
| import { useUserStore } from '@/stores/user' | import { useUserStore } from '@/stores/user' | ||||||
| import { useRouter } from 'vue-router' | import { useRouter } from 'vue-router' | ||||||
|  |  | ||||||
|  | import { useColorMode } from '@vueuse/core' | ||||||
|  |  | ||||||
|  | const mode = useColorMode() | ||||||
| const userStore = useUserStore() | const userStore = useUserStore() | ||||||
| const router = useRouter() | const router = useRouter() | ||||||
|  | const { t } = useI18n() | ||||||
|  |  | ||||||
| const logout = () => { | const logout = () => { | ||||||
|   window.location.href = '/logout' |   window.location.href = '/logout' | ||||||
|   | |||||||
| @@ -1,53 +1,112 @@ | |||||||
| <template> | <template> | ||||||
|     <table class="min-w-full divide-y divide-gray-200"> |   <table class="min-w-full table-fixed divide-y divide-border"> | ||||||
|         <thead class="bg-gray-50"> |     <thead class="bg-muted"> | ||||||
|             <tr> |       <tr> | ||||||
|                 <th v-for="(header, index) in headers" :key="index" scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"> |         <th | ||||||
|                     {{ header }} |           v-for="(header, index) in headers" | ||||||
|                 </th> |           :key="index" | ||||||
|                 <th scope="col" class="relative px-6 py-3"></th> |           scope="col" | ||||||
|             </tr> |           class="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase tracking-wider" | ||||||
|         </thead> |         > | ||||||
|         <tbody class="bg-white divide-y divide-gray-200"> |           {{ header }} | ||||||
|             <tr v-for="(item, index) in data" :key="index"> |         </th> | ||||||
|                 <td v-for="key in keys" :key="key" class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"> |         <th v-if="showDelete" scope="col" class="relative px-6 py-3"></th> | ||||||
|                     {{ item[key] }} |       </tr> | ||||||
|                 </td> |     </thead> | ||||||
|                 <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> |     <tbody class="bg-background divide-y divide-border"> | ||||||
|                     <Button size="xs" variant="ghost" @click.prevent="deleteItem(item)"> |       <!-- Loading State --> | ||||||
|                         <Trash2 class="h-4 w-4" /> |       <template v-if="loading"> | ||||||
|                     </Button> |         <tr v-for="i in skeletonRows" :key="`skeleton-${i}`" class="hover:bg-accent"> | ||||||
|                 </td> |           <td | ||||||
|             </tr> |             v-for="(header, index) in headers" | ||||||
|         </tbody> |             :key="`skeleton-cell-${index}`" | ||||||
|     </table> |             class="px-6 py-3 text-sm font-medium text-foreground whitespace-normal break-words" | ||||||
|  |           > | ||||||
|  |             <Skeleton class="h-4 w-[85%]" /> | ||||||
|  |           </td> | ||||||
|  |           <td v-if="showDelete" class="px-6 py-4 text-sm text-muted-foreground"> | ||||||
|  |             <Skeleton class="h-8 w-8 rounded" /> | ||||||
|  |           </td> | ||||||
|  |         </tr> | ||||||
|  |       </template> | ||||||
|  |  | ||||||
|  |       <!-- No Results State --> | ||||||
|  |       <template v-else-if="data.length === 0"> | ||||||
|  |         <tr> | ||||||
|  |           <td :colspan="headers.length + (showDelete ? 1 : 0)" class="px-6 py-12 text-center"> | ||||||
|  |             <div class="flex flex-col items-center space-y-4"> | ||||||
|  |               <span class="text-md text-muted-foreground"> | ||||||
|  |                 {{ | ||||||
|  |                   $t('globals.messages.noResults', { | ||||||
|  |                     name: $t('globals.terms.result', 2).toLowerCase() | ||||||
|  |                   }) | ||||||
|  |                 }} | ||||||
|  |               </span> | ||||||
|  |             </div> | ||||||
|  |           </td> | ||||||
|  |         </tr> | ||||||
|  |       </template> | ||||||
|  |  | ||||||
|  |       <!-- Data Rows --> | ||||||
|  |       <template v-else> | ||||||
|  |         <tr v-for="(item, index) in data" :key="index" class="hover:bg-accent"> | ||||||
|  |           <td | ||||||
|  |             v-for="key in keys" | ||||||
|  |             :key="key" | ||||||
|  |             class="px-6 py-4 text-sm font-medium text-foreground whitespace-normal break-words" | ||||||
|  |           > | ||||||
|  |             {{ item[key] }} | ||||||
|  |           </td> | ||||||
|  |           <td v-if="showDelete" class="px-6 py-4 text-sm text-muted-foreground"> | ||||||
|  |             <Button size="xs" variant="ghost" @click.prevent="deleteItem(item)"> | ||||||
|  |               <Trash2 class="h-4 w-4" /> | ||||||
|  |             </Button> | ||||||
|  |           </td> | ||||||
|  |         </tr> | ||||||
|  |       </template> | ||||||
|  |     </tbody> | ||||||
|  |   </table> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
| <script setup> | <script setup> | ||||||
| import { Trash2 } from 'lucide-vue-next'; | import { Trash2 } from 'lucide-vue-next' | ||||||
| import { defineProps, defineEmits } from 'vue'; | import { defineEmits } from 'vue' | ||||||
|  | import { Button } from '@/components/ui/button' | ||||||
|  | import { Skeleton } from '@/components/ui/skeleton' | ||||||
|  |  | ||||||
| defineProps({ | defineProps({ | ||||||
|     headers: { |   headers: { | ||||||
|         type: Array, |     type: Array, | ||||||
|         required: true, |     required: true, | ||||||
|         default: () => [] |     default: () => [] | ||||||
|     }, |   }, | ||||||
|     keys: { |   keys: { | ||||||
|         type: Array, |     type: Array, | ||||||
|         required: true, |     required: true, | ||||||
|         default: () => [] |     default: () => [] | ||||||
|     }, |   }, | ||||||
|     data: { |   data: { | ||||||
|         type: Array, |     type: Array, | ||||||
|         required: true, |     required: true, | ||||||
|         default: () => [] |     default: () => [] | ||||||
|     } |   }, | ||||||
| }); |   showDelete: { | ||||||
|  |     type: Boolean, | ||||||
|  |     default: true | ||||||
|  |   }, | ||||||
|  |   loading: { | ||||||
|  |     type: Boolean, | ||||||
|  |     default: false | ||||||
|  |   }, | ||||||
|  |   skeletonRows: { | ||||||
|  |     type: Number, | ||||||
|  |     default: 5 | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
| const emit = defineEmits(['deleteItem']); | const emit = defineEmits(['deleteItem']) | ||||||
|  |  | ||||||
| function deleteItem(item) { | function deleteItem(item) { | ||||||
|     emit('deleteItem', item); |   emit('deleteItem', item) | ||||||
| } | } | ||||||
| </script> | </script> | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
| import { AvatarImage } from 'radix-vue' | import { AvatarImage } from 'radix-vue' | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   src: { type: String, required: true }, |   src: { type: String, required: false, default: '' }, | ||||||
|   asChild: { type: Boolean, required: false }, |   asChild: { type: Boolean, required: false }, | ||||||
|   as: { type: null, required: false } |   as: { type: null, required: false } | ||||||
| }) | }) | ||||||
|   | |||||||
							
								
								
									
										59
									
								
								frontend/src/components/ui/avatar/AvatarUpload.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								frontend/src/components/ui/avatar/AvatarUpload.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | |||||||
|  | <template> | ||||||
|  |   <div class="relative group w-28 h-28 cursor-pointer" @click="triggerFileInput"> | ||||||
|  |     <Avatar class="size-28"> | ||||||
|  |       <AvatarImage :src="src || ''" /> | ||||||
|  |       <AvatarFallback>{{ initials }}</AvatarFallback> | ||||||
|  |     </Avatar> | ||||||
|  |  | ||||||
|  |     <!-- Hover Overlay --> | ||||||
|  |     <div | ||||||
|  |       class="absolute inset-0 bg-black bg-opacity-50 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity cursor-pointer rounded-full" | ||||||
|  |     > | ||||||
|  |       <span class="text-white font-semibold">{{ label }}</span> | ||||||
|  |     </div> | ||||||
|  |  | ||||||
|  |     <!-- Delete Icon --> | ||||||
|  |     <X | ||||||
|  |       class="absolute top-1 right-1 rounded-full p-1 shadow-md z-10 opacity-0 group-hover:opacity-100 transition-opacity" | ||||||
|  |       size="20" | ||||||
|  |       @click.stop="emit('remove')" | ||||||
|  |       v-if="src" | ||||||
|  |     /> | ||||||
|  |  | ||||||
|  |     <!-- File Input --> | ||||||
|  |     <input | ||||||
|  |       ref="fileInput" | ||||||
|  |       type="file" | ||||||
|  |       class="hidden" | ||||||
|  |       accept="image/png,image/jpeg,image/jpg" | ||||||
|  |       @change="handleChange" | ||||||
|  |     /> | ||||||
|  |   </div> | ||||||
|  | </template> | ||||||
|  |  | ||||||
|  | <script setup> | ||||||
|  | import { ref } from 'vue' | ||||||
|  | import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar' | ||||||
|  | import { X } from 'lucide-vue-next' | ||||||
|  |  | ||||||
|  | defineProps({ | ||||||
|  |   src: String, | ||||||
|  |   initials: String, | ||||||
|  |   label: { | ||||||
|  |     type: String, | ||||||
|  |     default: 'Upload' | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const emit = defineEmits(['upload', 'remove']) | ||||||
|  | const fileInput = ref(null) | ||||||
|  |  | ||||||
|  | function triggerFileInput() { | ||||||
|  |   fileInput.value?.click() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function handleChange(e) { | ||||||
|  |   const file = e.target.files[0] | ||||||
|  |   if (file) emit('upload', file) | ||||||
|  | } | ||||||
|  | </script> | ||||||
| @@ -3,6 +3,7 @@ import { cva } from 'class-variance-authority' | |||||||
| export { default as Avatar } from './Avatar.vue' | export { default as Avatar } from './Avatar.vue' | ||||||
| export { default as AvatarImage } from './AvatarImage.vue' | export { default as AvatarImage } from './AvatarImage.vue' | ||||||
| export { default as AvatarFallback } from './AvatarFallback.vue' | export { default as AvatarFallback } from './AvatarFallback.vue' | ||||||
|  | export { default as AvatarUpload } from './AvatarUpload.vue' | ||||||
|  |  | ||||||
| export const avatarVariant = cva( | export const avatarVariant = cva( | ||||||
|   'inline-flex items-center justify-center font-normal text-foreground select-none shrink-0 bg-secondary overflow-hidden', |   'inline-flex items-center justify-center font-normal text-foreground select-none shrink-0 bg-secondary overflow-hidden', | ||||||
|   | |||||||
| @@ -1,25 +1,16 @@ | |||||||
| <script setup> | <script setup> | ||||||
| import { Primitive } from 'radix-vue' | import { Primitive } from 'reka-ui' | ||||||
| import { buttonVariants } from '.' |  | ||||||
| import { cn } from '@/lib/utils' | import { cn } from '@/lib/utils' | ||||||
| import { ref, computed } from 'vue' | import { buttonVariants } from '.' | ||||||
| import { DotLoader } from '@/components/ui/loader' | import { Loader2 } from 'lucide-vue-next' | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   variant: { type: null, required: false }, |   variant: { type: null, required: false }, | ||||||
|   size: { type: null, required: false }, |   size: { type: null, required: false }, | ||||||
|   class: { type: null, required: false }, |   class: { type: null, required: false }, | ||||||
|   asChild: { type: Boolean, required: false }, |   asChild: { type: Boolean, required: false }, | ||||||
|   as: { type: null, required: false, default: 'button' }, |   as: { type: null, required: false, default: 'button' }, | ||||||
|   isLoading: { type: Boolean, required: false, default: false } |   isLoading: { type: Boolean, required: false, default: false }, | ||||||
| }) |   disabled: { type: Boolean, required: false, default: false } | ||||||
|  |  | ||||||
| const isDisabled = ref(false) |  | ||||||
|  |  | ||||||
| const computedClass = computed(() => { |  | ||||||
|   return cn(buttonVariants({ variant: props.variant, size: props.size }), props.class, { |  | ||||||
|     'cursor-not-allowed opacity-50': props.isLoading || isDisabled.value |  | ||||||
|   }) |  | ||||||
| }) | }) | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
| @@ -27,10 +18,22 @@ const computedClass = computed(() => { | |||||||
|   <Primitive |   <Primitive | ||||||
|     :as="as" |     :as="as" | ||||||
|     :as-child="asChild" |     :as-child="asChild" | ||||||
|     :class="computedClass" |     :class=" | ||||||
|     :disabled="isLoading || isDisabled" |       cn( | ||||||
|  |         buttonVariants({ variant, size }), | ||||||
|  |         'relative', | ||||||
|  |         { 'text-transparent': isLoading }, | ||||||
|  |         props.class | ||||||
|  |       ) | ||||||
|  |     " | ||||||
|  |     :disabled="isLoading || disabled" | ||||||
|   > |   > | ||||||
|     <DotLoader v-if="isLoading" /> |     <slot /> | ||||||
|     <slot v-else /> |     <span | ||||||
|  |       v-if="isLoading" | ||||||
|  |       class="absolute inset-0 flex items-center justify-center pointer-events-none text-background" | ||||||
|  |     > | ||||||
|  |       <Loader2 class="h-5 w-5 animate-spin" /> | ||||||
|  |     </span> | ||||||
|   </Primitive> |   </Primitive> | ||||||
| </template> | </template> | ||||||
|   | |||||||
| @@ -1,31 +1,34 @@ | |||||||
| import { cva } from 'class-variance-authority' | import { cva } from 'class-variance-authority'; | ||||||
|  |  | ||||||
| export { default as Button } from './Button.vue' | export { default as Button } from './Button.vue'; | ||||||
|  |  | ||||||
| export const buttonVariants = cva( | export const buttonVariants = cva( | ||||||
|   'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50', |   'inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0', | ||||||
|   { |   { | ||||||
|     variants: { |     variants: { | ||||||
|       variant: { |       variant: { | ||||||
|         default: 'bg-primary text-primary-foreground shadow hover:bg-primary/90', |         default: | ||||||
|         destructive: 'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90', |           'bg-primary text-primary-foreground shadow hover:bg-primary/90', | ||||||
|  |         destructive: | ||||||
|  |           'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90', | ||||||
|         outline: |         outline: | ||||||
|           'border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground', |           'border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground', | ||||||
|         secondary: 'bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80', |         secondary: | ||||||
|  |           'bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80', | ||||||
|         ghost: 'hover:bg-accent hover:text-accent-foreground', |         ghost: 'hover:bg-accent hover:text-accent-foreground', | ||||||
|         link: 'text-primary underline-offset-4 hover:underline' |         link: 'text-primary underline-offset-4 hover:underline', | ||||||
|       }, |       }, | ||||||
|       size: { |       size: { | ||||||
|         default: 'h-9 px-4 py-2', |         default: 'h-9 px-4 py-2', | ||||||
|         xs: 'h-7 rounded px-2', |         xs: 'h-7 rounded px-2', | ||||||
|         sm: 'h-8 rounded-md px-3 text-xs', |         sm: 'h-8 rounded-md px-3 text-xs', | ||||||
|         lg: 'h-10 rounded-md px-8', |         lg: 'h-10 rounded-md px-8', | ||||||
|         icon: 'h-9 w-9' |         icon: 'h-9 w-9', | ||||||
|       } |       }, | ||||||
|     }, |     }, | ||||||
|     defaultVariants: { |     defaultVariants: { | ||||||
|       variant: 'default', |       variant: 'default', | ||||||
|       size: 'default' |       size: 'default', | ||||||
|     } |     }, | ||||||
|   } |   }, | ||||||
| ) | ); | ||||||
|   | |||||||
| @@ -5,10 +5,10 @@ | |||||||
|         variant="outline" |         variant="outline" | ||||||
|         role="combobox" |         role="combobox" | ||||||
|         :aria-expanded="open" |         :aria-expanded="open" | ||||||
|         class="w-full justify-between" |         :class="['w-full justify-between', buttonClass]" | ||||||
|       > |       > | ||||||
|         <slot name="selected" :selected="selectedItem">{{ selectedLabel }}</slot> |         <slot name="selected" :selected="selectedItem">{{ selectedLabel }}</slot> | ||||||
|         <CaretSortIcon class="ml-2 h-4 w-4 shrink-0 opacity-50" /> |         <CaretSortIcon class="h-4 w-4 shrink-0 opacity-50" /> | ||||||
|       </Button> |       </Button> | ||||||
|     </PopoverTrigger> |     </PopoverTrigger> | ||||||
|     <PopoverContent class="p-0"> |     <PopoverContent class="p-0"> | ||||||
| @@ -58,7 +58,11 @@ const props = defineProps({ | |||||||
|     required: true |     required: true | ||||||
|   }, |   }, | ||||||
|   placeholder: String, |   placeholder: String, | ||||||
|   defaultLabel: String |   defaultLabel: String, | ||||||
|  |   buttonClass: { | ||||||
|  |     type: String, | ||||||
|  |     default: '' | ||||||
|  |   } | ||||||
| }) | }) | ||||||
|  |  | ||||||
| const emit = defineEmits(['select']) | const emit = defineEmits(['select']) | ||||||
|   | |||||||
							
								
								
									
										116
									
								
								frontend/src/components/ui/date-filter/DateFilter.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								frontend/src/components/ui/date-filter/DateFilter.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,116 @@ | |||||||
|  | <template> | ||||||
|  |   <div class="flex items-center gap-2"> | ||||||
|  |     <Select v-model="selectedDays" @update:model-value="handleFilterChange"> | ||||||
|  |       <SelectTrigger class="w-[140px] h-8 text-xs"> | ||||||
|  |         <SelectValue | ||||||
|  |           :placeholder=" | ||||||
|  |             t('globals.messages.select', { | ||||||
|  |               name: t('globals.terms.day', 2) | ||||||
|  |             }) | ||||||
|  |           " | ||||||
|  |         /> | ||||||
|  |       </SelectTrigger> | ||||||
|  |       <SelectContent class="text-xs"> | ||||||
|  |         <SelectItem value="0">{{ $t('globals.terms.today') }}</SelectItem> | ||||||
|  |         <SelectItem value="1"> | ||||||
|  |           {{ | ||||||
|  |             $t('globals.messages.lastNItems', { | ||||||
|  |               n: 1, | ||||||
|  |               name: t('globals.terms.day', 1).toLowerCase() | ||||||
|  |             }) | ||||||
|  |           }} | ||||||
|  |         </SelectItem> | ||||||
|  |         <SelectItem value="2"> | ||||||
|  |           {{ | ||||||
|  |             $t('globals.messages.lastNItems', { | ||||||
|  |               n: 2, | ||||||
|  |               name: t('globals.terms.day', 2).toLowerCase() | ||||||
|  |             }) | ||||||
|  |           }} | ||||||
|  |         </SelectItem> | ||||||
|  |         <SelectItem value="7"> | ||||||
|  |           {{ | ||||||
|  |             $t('globals.messages.lastNItems', { | ||||||
|  |               n: 7, | ||||||
|  |               name: t('globals.terms.day', 2).toLowerCase() | ||||||
|  |             }) | ||||||
|  |           }} | ||||||
|  |         </SelectItem> | ||||||
|  |         <SelectItem value="30"> | ||||||
|  |           {{ | ||||||
|  |             $t('globals.messages.lastNItems', { | ||||||
|  |               n: 30, | ||||||
|  |               name: t('globals.terms.day', 2).toLowerCase() | ||||||
|  |             }) | ||||||
|  |           }} | ||||||
|  |         </SelectItem> | ||||||
|  |         <SelectItem value="90"> | ||||||
|  |           {{ | ||||||
|  |             $t('globals.messages.lastNItems', { | ||||||
|  |               n: 90, | ||||||
|  |               name: t('globals.terms.day', 2).toLowerCase() | ||||||
|  |             }) | ||||||
|  |           }} | ||||||
|  |         </SelectItem> | ||||||
|  |         <SelectItem value="custom"> | ||||||
|  |           {{ | ||||||
|  |             $t('globals.messages.custom', { | ||||||
|  |               name: t('globals.terms.day', 2).toLowerCase() | ||||||
|  |             }) | ||||||
|  |           }} | ||||||
|  |         </SelectItem> | ||||||
|  |       </SelectContent> | ||||||
|  |     </Select> | ||||||
|  |     <div v-if="selectedDays === 'custom'" class="flex items-center gap-2"> | ||||||
|  |       <Input | ||||||
|  |         v-model="customDaysInput" | ||||||
|  |         type="number" | ||||||
|  |         min="1" | ||||||
|  |         max="365" | ||||||
|  |         class="w-20 h-8" | ||||||
|  |         @blur="handleCustomDaysChange" | ||||||
|  |         @keyup.enter="handleCustomDaysChange" | ||||||
|  |       /> | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  | </template> | ||||||
|  |  | ||||||
|  | <script setup> | ||||||
|  | import { ref } from 'vue' | ||||||
|  | import { useI18n } from 'vue-i18n' | ||||||
|  | import { | ||||||
|  |   Select, | ||||||
|  |   SelectContent, | ||||||
|  |   SelectItem, | ||||||
|  |   SelectTrigger, | ||||||
|  |   SelectValue | ||||||
|  | } from '@/components/ui/select' | ||||||
|  | import { Input } from '@/components/ui/input' | ||||||
|  |  | ||||||
|  | const { t } = useI18n() | ||||||
|  |  | ||||||
|  | const emit = defineEmits(['filterChange']) | ||||||
|  | const selectedDays = ref('30') | ||||||
|  | const customDaysInput = ref('') | ||||||
|  |  | ||||||
|  | const handleFilterChange = (value) => { | ||||||
|  |   if (value === 'custom') { | ||||||
|  |     customDaysInput.value = '30' | ||||||
|  |     emit('filterChange', 30) | ||||||
|  |   } else { | ||||||
|  |     emit('filterChange', parseInt(value)) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const handleCustomDaysChange = () => { | ||||||
|  |   const days = parseInt(customDaysInput.value) | ||||||
|  |   if (days && days > 0 && days <= 365) { | ||||||
|  |     emit('filterChange', days) | ||||||
|  |   } else { | ||||||
|  |     customDaysInput.value = '30' | ||||||
|  |     emit('filterChange', 30) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | handleFilterChange(selectedDays.value) | ||||||
|  | </script> | ||||||
							
								
								
									
										1
									
								
								frontend/src/components/ui/date-filter/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								frontend/src/components/ui/date-filter/index.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | export { default as DateFilter } from './DateFilter.vue' | ||||||
| @@ -1,19 +1,19 @@ | |||||||
| <script setup> | <script setup> | ||||||
| import { useVModel } from '@vueuse/core' | import { useVModel } from '@vueuse/core'; | ||||||
| import { cn } from '@/lib/utils' | import { cn } from '@/lib/utils'; | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   defaultValue: { type: [String, Number], required: false }, |   defaultValue: { type: [String, Number], required: false }, | ||||||
|   modelValue: { type: [String, Number], required: false }, |   modelValue: { type: [String, Number], required: false }, | ||||||
|   class: { type: null, required: false } |   class: { type: null, required: false }, | ||||||
| }) | }); | ||||||
|  |  | ||||||
| const emits = defineEmits(['update:modelValue']) | const emits = defineEmits(['update:modelValue']); | ||||||
|  |  | ||||||
| const modelValue = useVModel(props, 'modelValue', emits, { | const modelValue = useVModel(props, 'modelValue', emits, { | ||||||
|   passive: true, |   passive: true, | ||||||
|   defaultValue: props.defaultValue |   defaultValue: props.defaultValue, | ||||||
| }) | }); | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
| <template> | <template> | ||||||
| @@ -22,7 +22,7 @@ const modelValue = useVModel(props, 'modelValue', emits, { | |||||||
|     :class=" |     :class=" | ||||||
|       cn( |       cn( | ||||||
|         'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50', |         'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50', | ||||||
|         props.class |         props.class, | ||||||
|       ) |       ) | ||||||
|     " |     " | ||||||
|   /> |   /> | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| export { default as Input } from './Input.vue' | export { default as Input } from './Input.vue'; | ||||||
|   | |||||||
| @@ -1,7 +1,11 @@ | |||||||
| <template> | <template> | ||||||
|   <span class="dot-loader"> |   <span class="inline-flex items-center"> | ||||||
|     <span class="dot"></span> |     <span class="w-1 h-1 rounded-full bg-current mx-0.5 animate-dot-flashing"></span> | ||||||
|     <span class="dot"></span> |     <span | ||||||
|     <span class="dot"></span> |       class="w-1 h-1 rounded-full bg-current mx-0.5 animate-dot-flashing [animation-delay:0.2s]" | ||||||
|  |     ></span> | ||||||
|  |     <span | ||||||
|  |       class="w-1 h-1 rounded-full bg-current mx-0.5 animate-dot-flashing [animation-delay:0.4s]" | ||||||
|  |     ></span> | ||||||
|   </span> |   </span> | ||||||
| </template> | </template> | ||||||
|   | |||||||
							
								
								
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationEllipsis.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationEllipsis.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | <script setup> | ||||||
|  | import { cn } from '@/lib/utils'; | ||||||
|  | import { DotsHorizontalIcon } from '@radix-icons/vue'; | ||||||
|  | import { PaginationEllipsis } from 'reka-ui'; | ||||||
|  | import { computed } from 'vue'; | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   asChild: { type: Boolean, required: false }, | ||||||
|  |   as: { type: null, required: false }, | ||||||
|  |   class: { type: null, required: false }, | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | const delegatedProps = computed(() => { | ||||||
|  |   const { class: _, ...delegated } = props; | ||||||
|  |  | ||||||
|  |   return delegated; | ||||||
|  | }); | ||||||
|  | </script> | ||||||
|  |  | ||||||
|  | <template> | ||||||
|  |   <PaginationEllipsis | ||||||
|  |     v-bind="delegatedProps" | ||||||
|  |     :class="cn('w-9 h-9 flex items-center justify-center', props.class)" | ||||||
|  |   > | ||||||
|  |     <slot> | ||||||
|  |       <DotsHorizontalIcon /> | ||||||
|  |     </slot> | ||||||
|  |   </PaginationEllipsis> | ||||||
|  | </template> | ||||||
							
								
								
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationFirst.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationFirst.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | <script setup> | ||||||
|  | import { cn } from '@/lib/utils'; | ||||||
|  | import { Button } from '@/components/ui/button'; | ||||||
|  | import { ChevronsLeft } from 'lucide-vue-next'; | ||||||
|  | import { PaginationFirst } from 'reka-ui'; | ||||||
|  | import { computed } from 'vue'; | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   asChild: { type: Boolean, required: false, default: true }, | ||||||
|  |   as: { type: null, required: false }, | ||||||
|  |   class: { type: null, required: false }, | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | const delegatedProps = computed(() => { | ||||||
|  |   const { class: _, ...delegated } = props; | ||||||
|  |  | ||||||
|  |   return delegated; | ||||||
|  | }); | ||||||
|  | </script> | ||||||
|  |  | ||||||
|  | <template> | ||||||
|  |   <PaginationFirst v-bind="delegatedProps"> | ||||||
|  |     <Button :class="cn('w-9 h-9 p-0', props.class)" variant="outline"> | ||||||
|  |       <slot> | ||||||
|  |         <ChevronsLeft /> | ||||||
|  |       </slot> | ||||||
|  |     </Button> | ||||||
|  |   </PaginationFirst> | ||||||
|  | </template> | ||||||
							
								
								
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationLast.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationLast.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | <script setup> | ||||||
|  | import { cn } from '@/lib/utils'; | ||||||
|  | import { Button } from '@/components/ui/button'; | ||||||
|  | import { ChevronsRight } from 'lucide-vue-next'; | ||||||
|  | import { PaginationLast } from 'reka-ui'; | ||||||
|  | import { computed } from 'vue'; | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   asChild: { type: Boolean, required: false, default: true }, | ||||||
|  |   as: { type: null, required: false }, | ||||||
|  |   class: { type: null, required: false }, | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | const delegatedProps = computed(() => { | ||||||
|  |   const { class: _, ...delegated } = props; | ||||||
|  |  | ||||||
|  |   return delegated; | ||||||
|  | }); | ||||||
|  | </script> | ||||||
|  |  | ||||||
|  | <template> | ||||||
|  |   <PaginationLast v-bind="delegatedProps"> | ||||||
|  |     <Button :class="cn('w-9 h-9 p-0', props.class)" variant="outline"> | ||||||
|  |       <slot> | ||||||
|  |         <ChevronsRight /> | ||||||
|  |       </slot> | ||||||
|  |     </Button> | ||||||
|  |   </PaginationLast> | ||||||
|  | </template> | ||||||
							
								
								
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationNext.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationNext.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | <script setup> | ||||||
|  | import { cn } from '@/lib/utils'; | ||||||
|  | import { Button } from '@/components/ui/button'; | ||||||
|  | import { ChevronRightIcon } from '@radix-icons/vue'; | ||||||
|  | import { PaginationNext } from 'reka-ui'; | ||||||
|  | import { computed } from 'vue'; | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   asChild: { type: Boolean, required: false, default: true }, | ||||||
|  |   as: { type: null, required: false }, | ||||||
|  |   class: { type: null, required: false }, | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | const delegatedProps = computed(() => { | ||||||
|  |   const { class: _, ...delegated } = props; | ||||||
|  |  | ||||||
|  |   return delegated; | ||||||
|  | }); | ||||||
|  | </script> | ||||||
|  |  | ||||||
|  | <template> | ||||||
|  |   <PaginationNext v-bind="delegatedProps"> | ||||||
|  |     <Button :class="cn('w-9 h-9 p-0', props.class)" variant="outline"> | ||||||
|  |       <slot> | ||||||
|  |         <ChevronRightIcon /> | ||||||
|  |       </slot> | ||||||
|  |     </Button> | ||||||
|  |   </PaginationNext> | ||||||
|  | </template> | ||||||
							
								
								
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationPrev.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								frontend/src/components/ui/pagination/PaginationPrev.vue
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | <script setup> | ||||||
|  | import { cn } from '@/lib/utils'; | ||||||
|  | import { Button } from '@/components/ui/button'; | ||||||
|  | import { ChevronLeftIcon } from '@radix-icons/vue'; | ||||||
|  | import { PaginationPrev } from 'reka-ui'; | ||||||
|  | import { computed } from 'vue'; | ||||||
|  |  | ||||||
|  | const props = defineProps({ | ||||||
|  |   asChild: { type: Boolean, required: false, default: true }, | ||||||
|  |   as: { type: null, required: false }, | ||||||
|  |   class: { type: null, required: false }, | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | const delegatedProps = computed(() => { | ||||||
|  |   const { class: _, ...delegated } = props; | ||||||
|  |  | ||||||
|  |   return delegated; | ||||||
|  | }); | ||||||
|  | </script> | ||||||
|  |  | ||||||
|  | <template> | ||||||
|  |   <PaginationPrev v-bind="delegatedProps"> | ||||||
|  |     <Button :class="cn('w-9 h-9 p-0', props.class)" variant="outline"> | ||||||
|  |       <slot> | ||||||
|  |         <ChevronLeftIcon /> | ||||||
|  |       </slot> | ||||||
|  |     </Button> | ||||||
|  |   </PaginationPrev> | ||||||
|  | </template> | ||||||
							
								
								
									
										10
									
								
								frontend/src/components/ui/pagination/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								frontend/src/components/ui/pagination/index.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | export { default as PaginationEllipsis } from './PaginationEllipsis.vue'; | ||||||
|  | export { default as PaginationFirst } from './PaginationFirst.vue'; | ||||||
|  | export { default as PaginationLast } from './PaginationLast.vue'; | ||||||
|  | export { default as PaginationNext } from './PaginationNext.vue'; | ||||||
|  | export { default as PaginationPrev } from './PaginationPrev.vue'; | ||||||
|  | export { | ||||||
|  |   PaginationRoot as Pagination, | ||||||
|  |   PaginationList, | ||||||
|  |   PaginationListItem, | ||||||
|  | } from 'reka-ui'; | ||||||
| @@ -4,8 +4,8 @@ import { RadioGroupRoot, useForwardPropsEmits } from 'radix-vue' | |||||||
| import { cn } from '@/lib/utils' | import { cn } from '@/lib/utils' | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   modelValue: { type: String, required: false }, |   modelValue: { type: [String, Boolean], required: false }, | ||||||
|   defaultValue: { type: String, required: false }, |   defaultValue: { type: [String, Boolean], required: false }, | ||||||
|   disabled: { type: Boolean, required: false }, |   disabled: { type: Boolean, required: false }, | ||||||
|   name: { type: String, required: false }, |   name: { type: String, required: false }, | ||||||
|   required: { type: Boolean, required: false }, |   required: { type: Boolean, required: false }, | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ import { cn } from '@/lib/utils' | |||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   id: { type: String, required: false }, |   id: { type: String, required: false }, | ||||||
|   value: { type: String, required: false }, |   value: { type: [String, Boolean], required: false }, | ||||||
|   disabled: { type: Boolean, required: false }, |   disabled: { type: Boolean, required: false }, | ||||||
|   required: { type: Boolean, required: false }, |   required: { type: Boolean, required: false }, | ||||||
|   name: { type: String, required: false }, |   name: { type: String, required: false }, | ||||||
|   | |||||||
| @@ -4,8 +4,8 @@ import { SelectRoot, useForwardPropsEmits } from 'radix-vue' | |||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   open: { type: Boolean, required: false }, |   open: { type: Boolean, required: false }, | ||||||
|   defaultOpen: { type: Boolean, required: false }, |   defaultOpen: { type: Boolean, required: false }, | ||||||
|   defaultValue: { type: String, required: false }, |   defaultValue: { type: [String, Number], required: false }, | ||||||
|   modelValue: { type: String, required: false }, |   modelValue: { type: [String, Number], required: false }, | ||||||
|   dir: { type: String, required: false }, |   dir: { type: String, required: false }, | ||||||
|   name: { type: String, required: false }, |   name: { type: String, required: false }, | ||||||
|   autocomplete: { type: String, required: false }, |   autocomplete: { type: String, required: false }, | ||||||
|   | |||||||
| @@ -1,26 +1,49 @@ | |||||||
| <template> | <template> | ||||||
|   <TagsInput v-model="tags" class="px-0 gap-0"> |   <!-- idk why I named this select tag, should be named multi-select --> | ||||||
|  |   <TagsInput v-model="tags" class="px-0 gap-0" :displayValue="getLabel"> | ||||||
|  |     <!-- Tags visible to the user --> | ||||||
|     <div class="flex gap-2 flex-wrap items-center px-3"> |     <div class="flex gap-2 flex-wrap items-center px-3"> | ||||||
|       <TagsInputItem v-for="tag in tags" :key="tag" :value="tag"> |       <TagsInputItem v-for="tagValue in tags" :key="tagValue" :value="tagValue"> | ||||||
|         <TagsInputItemText>{{ tag }}</TagsInputItemText> |         <TagsInputItemText /> | ||||||
|         <TagsInputItemDelete /> |         <TagsInputItemDelete /> | ||||||
|       </TagsInputItem> |       </TagsInputItem> | ||||||
|     </div> |     </div> | ||||||
|     <ComboboxRoot :model-value="tags" v-model:open="open" v-model:search-term="searchTerm" class="w-full"> |  | ||||||
|  |     <!-- Combobox for selecting new tags --> | ||||||
|  |     <ComboboxRoot | ||||||
|  |       :model-value="tags" | ||||||
|  |       v-model:open="open" | ||||||
|  |       v-model:search-term="searchTerm" | ||||||
|  |       :filterFunction="filterFunc" | ||||||
|  |       class="w-full" | ||||||
|  |     > | ||||||
|       <ComboboxAnchor as-child> |       <ComboboxAnchor as-child> | ||||||
|         <ComboboxInput :placeholder="placeholder" as-child> |         <ComboboxInput :placeholder="placeholder" as-child> | ||||||
|           <TagsInputInput class="w-full px-3" :class="tags.length > 0 ? 'mt-2' : ''" @keydown.enter.prevent |           <TagsInputInput | ||||||
|             @blur="handleBlur" /> |             class="w-full px-3" | ||||||
|  |             :class="tags.length > 0 ? 'mt-2' : ''" | ||||||
|  |             @keydown.enter.prevent | ||||||
|  |             @blur="handleBlur" | ||||||
|  |             @click="open = true" | ||||||
|  |             @input.stop | ||||||
|  |           /> | ||||||
|         </ComboboxInput> |         </ComboboxInput> | ||||||
|       </ComboboxAnchor> |       </ComboboxAnchor> | ||||||
|       <ComboboxPortal> |       <ComboboxPortal> | ||||||
|         <ComboboxContent> |         <ComboboxContent> | ||||||
|           <CommandList position="popper" |           <CommandList | ||||||
|             class="w-[--radix-popper-anchor-width] rounded-md mt-2 border bg-popover text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2"> |             position="popper" | ||||||
|             <CommandEmpty /> |             class="w-[--radix-popper-anchor-width] rounded-md mt-2 border bg-popover text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2" | ||||||
|  |           > | ||||||
|  |             <CommandEmpty> No results found </CommandEmpty> | ||||||
|             <CommandGroup> |             <CommandGroup> | ||||||
|               <CommandItem v-for="item in filteredOptions" :key="item" :value="item" @select="handleSelect"> |               <CommandItem | ||||||
|                 {{ item }} |                 v-for="item in filteredOptions" | ||||||
|  |                 :key="item.value" | ||||||
|  |                 :value="item.value" | ||||||
|  |                 @select="handleSelect" | ||||||
|  |               > | ||||||
|  |                 {{ item.label }} | ||||||
|               </CommandItem> |               </CommandItem> | ||||||
|             </CommandGroup> |             </CommandGroup> | ||||||
|           </CommandList> |           </CommandList> | ||||||
| @@ -32,8 +55,20 @@ | |||||||
|  |  | ||||||
| <script setup> | <script setup> | ||||||
| import { CommandEmpty, CommandGroup, CommandItem, CommandList } from '@/components/ui/command' | import { CommandEmpty, CommandGroup, CommandItem, CommandList } from '@/components/ui/command' | ||||||
| import { TagsInput, TagsInputInput, TagsInputItem, TagsInputItemDelete, TagsInputItemText } from '@/components/ui/tags-input' | import { | ||||||
| import { ComboboxAnchor, ComboboxContent, ComboboxInput, ComboboxPortal, ComboboxRoot } from 'radix-vue' |   TagsInput, | ||||||
|  |   TagsInputInput, | ||||||
|  |   TagsInputItem, | ||||||
|  |   TagsInputItemDelete, | ||||||
|  |   TagsInputItemText | ||||||
|  | } from '@/components/ui/tags-input' | ||||||
|  | import { | ||||||
|  |   ComboboxAnchor, | ||||||
|  |   ComboboxContent, | ||||||
|  |   ComboboxInput, | ||||||
|  |   ComboboxPortal, | ||||||
|  |   ComboboxRoot | ||||||
|  | } from 'radix-vue' | ||||||
| import { computed, ref } from 'vue' | import { computed, ref } from 'vue' | ||||||
| import { useField } from 'vee-validate' | import { useField } from 'vee-validate' | ||||||
|  |  | ||||||
| @@ -54,7 +89,8 @@ const props = defineProps({ | |||||||
|   }, |   }, | ||||||
|   items: { |   items: { | ||||||
|     type: Array, |     type: Array, | ||||||
|     required: true |     required: true, | ||||||
|  |     validator: (value) => value.every((item) => 'label' in item && 'value' in item) | ||||||
|   } |   } | ||||||
| }) | }) | ||||||
|  |  | ||||||
| @@ -65,20 +101,40 @@ const { handleBlur } = useField(() => props.name, undefined, { | |||||||
| const open = ref(false) | const open = ref(false) | ||||||
| const searchTerm = ref('') | const searchTerm = ref('') | ||||||
|  |  | ||||||
| const filteredOptions = computed(() => | // Get all options that are not already selected and match the search term | ||||||
|   props.items.filter(item => !tags.value.includes(item)) | // If not search term is provided, return all available options | ||||||
| ) | const filteredOptions = computed(() => { | ||||||
|  |   const available = props.items.filter((item) => !tags.value.includes(item.value)) | ||||||
|  |  | ||||||
|  |   if (!searchTerm.value) return available | ||||||
|  |  | ||||||
|  |   return available.filter((item) => | ||||||
|  |     item.label.toLowerCase().includes(searchTerm.value.toLowerCase()) | ||||||
|  |   ) | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const getLabel = (value) => { | ||||||
|  |   const item = props.items.find((item) => item.value === value) | ||||||
|  |   return item?.label || value | ||||||
|  | } | ||||||
|  |  | ||||||
| const handleSelect = (event) => { | const handleSelect = (event) => { | ||||||
|   if (event.detail.value) { |   const selectedValue = event.detail.value | ||||||
|  |   if (selectedValue) { | ||||||
|  |     tags.value = [...tags.value, selectedValue] | ||||||
|     searchTerm.value = '' |     searchTerm.value = '' | ||||||
|     const newTags = [...tags.value] |  | ||||||
|     newTags.push(event.detail.value) |  | ||||||
|     tags.value = newTags |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   if (filteredOptions.value.length === 0) { |   if (filteredOptions.value.length === 0) { | ||||||
|     open.value = false |     open.value = false | ||||||
|   } |   } | ||||||
| } | } | ||||||
| </script> |  | ||||||
|  | // Custom filter function to filter items based on the search term | ||||||
|  | const filterFunc = (remainingItemValues, term) => { | ||||||
|  |   const remainingItems = props.items.filter((item) => remainingItemValues.includes(item.value)) | ||||||
|  |   return remainingItems | ||||||
|  |     .filter((item) => item.label.toLowerCase().includes(term.toLowerCase())) | ||||||
|  |     .map((item) => item.value) | ||||||
|  | } | ||||||
|  | </script> | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
| import { SelectValue } from 'radix-vue' | import { SelectValue } from 'radix-vue' | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   placeholder: { type: String, required: false }, |   placeholder: { type: [String, Number], required: false }, | ||||||
|   asChild: { type: Boolean, required: false }, |   asChild: { type: Boolean, required: false }, | ||||||
|   as: { type: null, required: false } |   as: { type: null, required: false } | ||||||
| }) | }) | ||||||
|   | |||||||
| @@ -1,21 +1,17 @@ | |||||||
| <script setup> | <script setup> | ||||||
| import { computed } from 'vue' | import { reactiveOmit } from '@vueuse/core'; | ||||||
| import { Separator } from 'radix-vue' | import { Separator } from 'reka-ui'; | ||||||
| import { cn } from '@/lib/utils' | import { cn } from '@/lib/utils'; | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   orientation: { type: String, required: false }, |   orientation: { type: String, required: false, default: 'horizontal' }, | ||||||
|   decorative: { type: Boolean, required: false }, |   decorative: { type: Boolean, required: false, default: true }, | ||||||
|   asChild: { type: Boolean, required: false }, |   asChild: { type: Boolean, required: false }, | ||||||
|   as: { type: null, required: false }, |   as: { type: null, required: false }, | ||||||
|   class: { type: null, required: false } |   class: { type: null, required: false }, | ||||||
| }) | }); | ||||||
|  |  | ||||||
| const delegatedProps = computed(() => { | const delegatedProps = reactiveOmit(props, 'class'); | ||||||
|   const { class: _, ...delegated } = props |  | ||||||
|  |  | ||||||
|   return delegated |  | ||||||
| }) |  | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
| <template> | <template> | ||||||
| @@ -24,8 +20,8 @@ const delegatedProps = computed(() => { | |||||||
|     :class=" |     :class=" | ||||||
|       cn( |       cn( | ||||||
|         'shrink-0 bg-border', |         'shrink-0 bg-border', | ||||||
|         props.orientation === 'vertical' ? 'w-px h-full' : 'h-px w-full', |         props.orientation === 'horizontal' ? 'h-px w-full' : 'w-px h-full', | ||||||
|         props.class |         props.class, | ||||||
|       ) |       ) | ||||||
|     " |     " | ||||||
|   /> |   /> | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| export { default as Separator } from './Separator.vue' | export { default as Separator } from './Separator.vue'; | ||||||
|   | |||||||
| @@ -1,14 +1,14 @@ | |||||||
| <script setup> | <script setup> | ||||||
| import { DialogRoot, useForwardPropsEmits } from 'radix-vue' | import { DialogRoot, useForwardPropsEmits } from 'reka-ui'; | ||||||
|  |  | ||||||
| const props = defineProps({ | const props = defineProps({ | ||||||
|   open: { type: Boolean, required: false }, |   open: { type: Boolean, required: false }, | ||||||
|   defaultOpen: { type: Boolean, required: false }, |   defaultOpen: { type: Boolean, required: false }, | ||||||
|   modal: { type: Boolean, required: false } |   modal: { type: Boolean, required: false }, | ||||||
| }) | }); | ||||||
| const emits = defineEmits(['update:open']) | const emits = defineEmits(['update:open']); | ||||||
|  |  | ||||||
| const forwarded = useForwardPropsEmits(props, emits) | const forwarded = useForwardPropsEmits(props, emits); | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
| <template> | <template> | ||||||
|   | |||||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user