-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram-Module-Doc
More file actions
193 lines (128 loc) · 4.83 KB
/
Program-Module-Doc
File metadata and controls
193 lines (128 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Program Management Module – Functional Documentation
## Overview
This module implements the **Program management logic** inside the system.
It is responsible for handling all operations related to `ProgramCourse` records, including:
* Creating programs
* Updating programs
* Deleting programs
* Retrieving programs
* Searching programs with pagination (Technically used by GetAllPrograms)
This module is consumed internally by controllers and is **not exposed as a standalone public API**.
---
### Core Program Services and Usage
| Function | Used By / Entry Point | Actual Behavior in System |
| -------------- | ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| getProgramById | Program detail view (GET /program/:id) | Fetches a single ProgramCourse record by primary key. Used when viewing or editing a specific program. |
| createProgram | Program creation flow (POST /program) | Inserts a new ProgramCourse record into the database. Relies on DB unique constraints for validation. |
| updateProgram | Program edit flow (PUT /program/:id) | Updates existing ProgramCourse fields based on provided input. Throws error if program does not exist. |
| deleteProgram | Program management (DELETE /program/:id) | Removes a ProgramCourse record. Delete logic handled in controller, service executes DB operation. |
| searchPrograms | Employee listing (GET /program) | Performs wildcard search on `name` and `code`, supports pagination using `page` and `limit`, and supports `top` to limit result size. Used to power listing screens and search filters. |
---
## Search Programs Service
### Function
```
searchPrograms(top,page,limit,search)
```
### Input
```js
{
search: "CS",
page: 1,
limit: 10
}
```
### Behavior
* Performs wildcard search on:
* `name`
* `code`
* Uses pagination logic:
```
offset = (page - 1) * limit
```
* Queries database using Sequelize `Op.like`
### Output
```js
{
programs: [...],
}
Above Code Return Programs, But It lacked response code and it must be standardized
```
---
## Program Creation Logic
### Function
```
createProgram(data)
```
### Input
```js
{
code: "AI01",
name: "Artificial Intelligence",
level: "Degree",
type: "PROGRAM"
}
```
### Behavior
* Directly inserts record into database.
* No manual duplicate checking.
* Database enforces uniqueness using:
```
UNIQUE(code, level, type)
```
### Failure Case
* If duplicate exists, database throws constraint error.
* Error propagates to controller.
---
## Delete Program Logic
### Function
```
deleteProgram(id)
```
### Behavior
* Delete logic is handled in `ProgramService`.
* Controller only calls Delete Method In Program Saervice.
* If no rows affected, controller throws error.
---
## Controller Simplification
### What was changed
Removed logic like:
> “Only return response if addProgram is successful”
### Reason
* Service throws exceptions automatically.
* Controllers rely on try/catch.
* Global error handler formats failure.
This results in:
* Linear flow
* Less defensive code
* Proper exception-based design
---
## Database as Single Source of Truth
The service layer no longer performs:
* Duplicate code validation
* Manual integrity checks
Because:
* Database already enforces business rules.
* This avoids logic duplication.
* Prevents race conditions.
---
## Execution Flow (Internal)
```
Controller → ProgramService → Sequelize → Database
```
No external consumers.
No network boundary.
Pure internal business logic.
---
## Why This Matters (Engineering Perspective)
This refactor achieved:
* Removal of redundant validations
* Trust in DB constraints
* Clean service contracts
* Predictable error flow
* Maintainable architecture
This is **how real backend systems are written**:
> Services enforce behavior, database enforces truth, controllers orchestrate.
---
## One-line Summary (Internal Docs / CV)
> Refactored Program management module by introducing search with pagination, simplifying controller logic, removing redundant validations, and relying on database constraints for data integrity.
> This is **service-level engineering**, not API design — and that’s actually more impressive technically.