schema.resolvers.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. package graph
  2. // This file will be automatically regenerated based on the schema, any resolver
  3. // implementations
  4. // will be copied through when generating and any unknown code will be moved to the end.
  5. // Code generated by github.com/99designs/gqlgen version v0.17.87
  6. import (
  7. "context"
  8. "errors"
  9. "fmt"
  10. "time"
  11. "gogs.dmsc.dev/arp/auth"
  12. "gogs.dmsc.dev/arp/graph/model"
  13. "gogs.dmsc.dev/arp/logging"
  14. "gogs.dmsc.dev/arp/models"
  15. )
  16. // Login is the resolver for the login field.
  17. func (r *mutationResolver) Login(ctx context.Context, email string, password string) (*model.AuthPayload, error) {
  18. var user models.User
  19. if err := r.DB.Preload("Roles.Permissions").Where("email = ?", email).First(&user).Error; err != nil {
  20. return nil, errors.New("invalid credentials")
  21. }
  22. // Check password
  23. if !auth.CheckPassword(password, user.Password) {
  24. return nil, errors.New("invalid credentials")
  25. }
  26. token, err := auth.GenerateToken(user)
  27. if err != nil {
  28. return nil, fmt.Errorf("failed to generate token: %w", err)
  29. }
  30. return &model.AuthPayload{
  31. Token: token,
  32. User: convertUser(user),
  33. }, nil
  34. }
  35. // CreateUser is the resolver for the createUser field.
  36. func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) (*model.User, error) {
  37. // Auth check
  38. if !auth.IsAuthenticated(ctx) {
  39. return nil, errors.New("unauthorized: authentication required")
  40. }
  41. roles := make([]models.Role, len(input.Roles))
  42. for i, roleIDStr := range input.Roles {
  43. roleID, err := toID(roleIDStr)
  44. if err != nil {
  45. return nil, fmt.Errorf("invalid role ID: %w", err)
  46. }
  47. var role models.Role
  48. if err := r.DB.First(&role, roleID).Error; err != nil {
  49. return nil, fmt.Errorf("role not found: %w", err)
  50. }
  51. roles[i] = role
  52. }
  53. // Hash the password before storing
  54. hashedPassword, err := auth.HashPassword(input.Password)
  55. if err != nil {
  56. return nil, fmt.Errorf("failed to hash password: %w", err)
  57. }
  58. user := models.User{
  59. Email: input.Email,
  60. Password: hashedPassword,
  61. Roles: roles,
  62. }
  63. if err := r.DB.Create(&user).Error; err != nil {
  64. return nil, fmt.Errorf("failed to create user: %w", err)
  65. }
  66. logging.LogMutation(ctx, "CREATE", "USER", user.Email)
  67. return convertUser(user), nil
  68. }
  69. // UpdateUser is the resolver for the updateUser field.
  70. func (r *mutationResolver) UpdateUser(ctx context.Context, id string, input model.UpdateUserInput) (*model.User, error) {
  71. // Auth check
  72. if !auth.IsAuthenticated(ctx) {
  73. return nil, errors.New("unauthorized: authentication required")
  74. }
  75. if !auth.HasPermission(ctx, "user:update") {
  76. return nil, errors.New("unauthorized: missing user:update permission")
  77. }
  78. userID, err := toID(id)
  79. if err != nil {
  80. return nil, fmt.Errorf("invalid user ID: %w", err)
  81. }
  82. var existing models.User
  83. if err := r.DB.First(&existing, userID).Error; err != nil {
  84. return nil, fmt.Errorf("user not found: %w", err)
  85. }
  86. if input.Email != nil {
  87. existing.Email = *input.Email
  88. }
  89. if input.Password != nil {
  90. // Hash the new password
  91. hashedPassword, err := auth.HashPassword(*input.Password)
  92. if err != nil {
  93. return nil, fmt.Errorf("failed to hash password: %w", err)
  94. }
  95. existing.Password = hashedPassword
  96. }
  97. if len(input.Roles) > 0 {
  98. roles := make([]models.Role, len(input.Roles))
  99. for i, roleIDStr := range input.Roles {
  100. roleID, err := toID(roleIDStr)
  101. if err != nil {
  102. return nil, fmt.Errorf("invalid role ID: %w", err)
  103. }
  104. var role models.Role
  105. if err := r.DB.First(&role, roleID).Error; err != nil {
  106. return nil, fmt.Errorf("role not found: %w", err)
  107. }
  108. roles[i] = role
  109. }
  110. existing.Roles = roles
  111. }
  112. if err := r.DB.Save(&existing).Error; err != nil {
  113. return nil, fmt.Errorf("failed to update user: %w", err)
  114. }
  115. logging.LogMutation(ctx, "UPDATE", "USER", existing.Email)
  116. return convertUser(existing), nil
  117. }
  118. // DeleteUser is the resolver for the deleteUser field.
  119. func (r *mutationResolver) DeleteUser(ctx context.Context, id string) (bool, error) {
  120. // Auth check
  121. if !auth.IsAuthenticated(ctx) {
  122. return false, errors.New("unauthorized: authentication required")
  123. }
  124. if !auth.HasPermission(ctx, "user:delete") {
  125. return false, errors.New("unauthorized: missing user:delete permission")
  126. }
  127. userID, err := toID(id)
  128. if err != nil {
  129. return false, fmt.Errorf("invalid user ID: %w", err)
  130. }
  131. result := r.DB.Delete(&models.User{}, userID)
  132. if result.Error != nil {
  133. return false, fmt.Errorf("failed to delete user: %w", result.Error)
  134. }
  135. logging.LogMutation(ctx, "DELETE", "USER", id)
  136. return result.RowsAffected > 0, nil
  137. }
  138. // CreateNote is the resolver for the createNote field.
  139. func (r *mutationResolver) CreateNote(ctx context.Context, input model.NewNote) (*model.Note, error) {
  140. // Auth check
  141. if !auth.IsAuthenticated(ctx) {
  142. return nil, errors.New("unauthorized: authentication required")
  143. }
  144. userID, err := toID(input.UserID)
  145. if err != nil {
  146. return nil, fmt.Errorf("invalid user ID: %w", err)
  147. }
  148. serviceID, err := toID(input.ServiceID)
  149. if err != nil {
  150. return nil, fmt.Errorf("invalid service ID: %w", err)
  151. }
  152. note := models.Note{
  153. Title: input.Title,
  154. Content: input.Content,
  155. UserID: userID,
  156. ServiceID: serviceID,
  157. }
  158. if err := r.DB.Create(&note).Error; err != nil {
  159. return nil, fmt.Errorf("failed to create note: %w", err)
  160. }
  161. logging.LogMutation(ctx, "CREATE", "NOTE", note.Title)
  162. return convertNote(note), nil
  163. }
  164. // UpdateNote is the resolver for the updateNote field.
  165. func (r *mutationResolver) UpdateNote(ctx context.Context, id string, input model.UpdateNoteInput) (*model.Note, error) {
  166. // Auth check
  167. if !auth.IsAuthenticated(ctx) {
  168. return nil, errors.New("unauthorized: authentication required")
  169. }
  170. if !auth.HasPermission(ctx, "note:update") {
  171. return nil, errors.New("unauthorized: missing note:update permission")
  172. }
  173. noteID, err := toID(id)
  174. if err != nil {
  175. return nil, fmt.Errorf("invalid note ID: %w", err)
  176. }
  177. var existing models.Note
  178. if err := r.DB.First(&existing, noteID).Error; err != nil {
  179. return nil, fmt.Errorf("note not found: %w", err)
  180. }
  181. if input.Title != nil {
  182. existing.Title = *input.Title
  183. }
  184. if input.Content != nil {
  185. existing.Content = *input.Content
  186. }
  187. if input.UserID != nil {
  188. userID, err := toID(*input.UserID)
  189. if err != nil {
  190. return nil, fmt.Errorf("invalid user ID: %w", err)
  191. }
  192. existing.UserID = userID
  193. }
  194. if input.ServiceID != nil {
  195. serviceID, err := toID(*input.ServiceID)
  196. if err != nil {
  197. return nil, fmt.Errorf("invalid service ID: %w", err)
  198. }
  199. existing.ServiceID = serviceID
  200. }
  201. if err := r.DB.Save(&existing).Error; err != nil {
  202. return nil, fmt.Errorf("failed to update note: %w", err)
  203. }
  204. logging.LogMutation(ctx, "UPDATE", "NOTE", existing.Title)
  205. return convertNote(existing), nil
  206. }
  207. // DeleteNote is the resolver for the deleteNote field.
  208. func (r *mutationResolver) DeleteNote(ctx context.Context, id string) (bool, error) {
  209. // Auth check
  210. if !auth.IsAuthenticated(ctx) {
  211. return false, errors.New("unauthorized: authentication required")
  212. }
  213. if !auth.HasPermission(ctx, "note:delete") {
  214. return false, errors.New("unauthorized: missing note:delete permission")
  215. }
  216. noteID, err := toID(id)
  217. if err != nil {
  218. return false, fmt.Errorf("invalid note ID: %w", err)
  219. }
  220. result := r.DB.Delete(&models.Note{}, noteID)
  221. if result.Error != nil {
  222. return false, fmt.Errorf("failed to delete note: %w", result.Error)
  223. }
  224. logging.LogMutation(ctx, "DELETE", "NOTE", id)
  225. return result.RowsAffected > 0, nil
  226. }
  227. // CreateRole is the resolver for the createRole field.
  228. func (r *mutationResolver) CreateRole(ctx context.Context, input model.NewRole) (*model.Role, error) {
  229. // Auth check
  230. if !auth.IsAuthenticated(ctx) {
  231. return nil, errors.New("unauthorized: authentication required")
  232. }
  233. permissions := make([]models.Permission, len(input.Permissions))
  234. for i, permIDStr := range input.Permissions {
  235. permID, err := toID(permIDStr)
  236. if err != nil {
  237. return nil, fmt.Errorf("invalid permission ID: %w", err)
  238. }
  239. var perm models.Permission
  240. if err := r.DB.First(&perm, permID).Error; err != nil {
  241. return nil, fmt.Errorf("permission not found: %w", err)
  242. }
  243. permissions[i] = perm
  244. }
  245. role := models.Role{
  246. Name: input.Name,
  247. Description: input.Description,
  248. Permissions: permissions,
  249. }
  250. if err := r.DB.Create(&role).Error; err != nil {
  251. return nil, fmt.Errorf("failed to create role: %w", err)
  252. }
  253. logging.LogMutation(ctx, "CREATE", "ROLE", role.Name)
  254. return convertRole(role), nil
  255. }
  256. // UpdateRole is the resolver for the updateRole field.
  257. func (r *mutationResolver) UpdateRole(ctx context.Context, id string, input model.UpdateRoleInput) (*model.Role, error) {
  258. // Auth check
  259. if !auth.IsAuthenticated(ctx) {
  260. return nil, errors.New("unauthorized: authentication required")
  261. }
  262. if !auth.HasPermission(ctx, "role:update") {
  263. return nil, errors.New("unauthorized: missing role:update permission")
  264. }
  265. roleID, err := toID(id)
  266. if err != nil {
  267. return nil, fmt.Errorf("invalid role ID: %w", err)
  268. }
  269. var existing models.Role
  270. if err := r.DB.First(&existing, roleID).Error; err != nil {
  271. return nil, fmt.Errorf("role not found: %w", err)
  272. }
  273. if input.Name != nil {
  274. existing.Name = *input.Name
  275. }
  276. if input.Description != nil {
  277. existing.Description = *input.Description
  278. }
  279. if len(input.Permissions) > 0 {
  280. permissions := make([]models.Permission, len(input.Permissions))
  281. for i, permIDStr := range input.Permissions {
  282. permID, err := toID(permIDStr)
  283. if err != nil {
  284. return nil, fmt.Errorf("invalid permission ID: %w", err)
  285. }
  286. var perm models.Permission
  287. if err := r.DB.First(&perm, permID).Error; err != nil {
  288. return nil, fmt.Errorf("permission not found: %w", err)
  289. }
  290. permissions[i] = perm
  291. }
  292. existing.Permissions = permissions
  293. }
  294. if err := r.DB.Save(&existing).Error; err != nil {
  295. return nil, fmt.Errorf("failed to update role: %w", err)
  296. }
  297. logging.LogMutation(ctx, "UPDATE", "ROLE", existing.Name)
  298. return convertRole(existing), nil
  299. }
  300. // DeleteRole is the resolver for the deleteRole field.
  301. func (r *mutationResolver) DeleteRole(ctx context.Context, id string) (bool, error) {
  302. // Auth check
  303. if !auth.IsAuthenticated(ctx) {
  304. return false, errors.New("unauthorized: authentication required")
  305. }
  306. if !auth.HasPermission(ctx, "role:delete") {
  307. return false, errors.New("unauthorized: missing role:delete permission")
  308. }
  309. roleID, err := toID(id)
  310. if err != nil {
  311. return false, fmt.Errorf("invalid role ID: %w", err)
  312. }
  313. result := r.DB.Delete(&models.Role{}, roleID)
  314. if result.Error != nil {
  315. return false, fmt.Errorf("failed to delete role: %w", result.Error)
  316. }
  317. logging.LogMutation(ctx, "DELETE", "ROLE", id)
  318. return result.RowsAffected > 0, nil
  319. }
  320. // CreatePermission is the resolver for the createPermission field.
  321. func (r *mutationResolver) CreatePermission(ctx context.Context, input model.NewPermission) (*model.Permission, error) {
  322. // Auth check
  323. if !auth.IsAuthenticated(ctx) {
  324. return nil, errors.New("unauthorized: authentication required")
  325. }
  326. permission := models.Permission{
  327. Code: input.Code,
  328. Description: input.Description,
  329. }
  330. if err := r.DB.Create(&permission).Error; err != nil {
  331. return nil, fmt.Errorf("failed to create permission: %w", err)
  332. }
  333. logging.LogMutation(ctx, "CREATE", "PERMISSION", permission.Code)
  334. return convertPermission(permission), nil
  335. }
  336. // UpdatePermission is the resolver for the updatePermission field.
  337. func (r *mutationResolver) UpdatePermission(ctx context.Context, id string, input model.UpdatePermissionInput) (*model.Permission, error) {
  338. // Auth check
  339. if !auth.IsAuthenticated(ctx) {
  340. return nil, errors.New("unauthorized: authentication required")
  341. }
  342. if !auth.HasPermission(ctx, "permission:update") {
  343. return nil, errors.New("unauthorized: missing permission:update permission")
  344. }
  345. permID, err := toID(id)
  346. if err != nil {
  347. return nil, fmt.Errorf("invalid permission ID: %w", err)
  348. }
  349. var existing models.Permission
  350. if err := r.DB.First(&existing, permID).Error; err != nil {
  351. return nil, fmt.Errorf("permission not found: %w", err)
  352. }
  353. if input.Code != nil {
  354. existing.Code = *input.Code
  355. }
  356. if input.Description != nil {
  357. existing.Description = *input.Description
  358. }
  359. if err := r.DB.Save(&existing).Error; err != nil {
  360. return nil, fmt.Errorf("failed to update permission: %w", err)
  361. }
  362. logging.LogMutation(ctx, "UPDATE", "PERMISSION", existing.Code)
  363. return convertPermission(existing), nil
  364. }
  365. // DeletePermission is the resolver for the deletePermission field.
  366. func (r *mutationResolver) DeletePermission(ctx context.Context, id string) (bool, error) {
  367. // Auth check
  368. if !auth.IsAuthenticated(ctx) {
  369. return false, errors.New("unauthorized: authentication required")
  370. }
  371. if !auth.HasPermission(ctx, "permission:delete") {
  372. return false, errors.New("unauthorized: missing permission:delete permission")
  373. }
  374. permID, err := toID(id)
  375. if err != nil {
  376. return false, fmt.Errorf("invalid permission ID: %w", err)
  377. }
  378. result := r.DB.Delete(&models.Permission{}, permID)
  379. if result.Error != nil {
  380. return false, fmt.Errorf("failed to delete permission: %w", result.Error)
  381. }
  382. logging.LogMutation(ctx, "DELETE", "PERMISSION", id)
  383. return result.RowsAffected > 0, nil
  384. }
  385. // CreateService is the resolver for the createService field.
  386. func (r *mutationResolver) CreateService(ctx context.Context, input model.NewService) (*model.Service, error) {
  387. // Auth check
  388. if !auth.IsAuthenticated(ctx) {
  389. return nil, errors.New("unauthorized: authentication required")
  390. }
  391. createdByID, err := toID(input.CreatedByID)
  392. if err != nil {
  393. return nil, fmt.Errorf("invalid created by ID: %w", err)
  394. }
  395. service := models.Service{
  396. Name: input.Name,
  397. CreatedByID: createdByID,
  398. }
  399. if input.Description != nil {
  400. service.Description = *input.Description
  401. }
  402. // Add participants
  403. for _, participantIDStr := range input.Participants {
  404. participantID, err := toID(participantIDStr)
  405. if err != nil {
  406. return nil, fmt.Errorf("invalid participant ID: %w", err)
  407. }
  408. var user models.User
  409. if err := r.DB.First(&user, participantID).Error; err != nil {
  410. return nil, fmt.Errorf("participant not found: %w", err)
  411. }
  412. service.Participants = append(service.Participants, user)
  413. }
  414. if err := r.DB.Create(&service).Error; err != nil {
  415. return nil, fmt.Errorf("failed to create service: %w", err)
  416. }
  417. // Reload with associations
  418. r.DB.Preload("Participants").Preload("Tasks").First(&service, service.ID)
  419. logging.LogMutation(ctx, "CREATE", "SERVICE", service.Name)
  420. return convertService(service), nil
  421. }
  422. // UpdateService is the resolver for the updateService field.
  423. func (r *mutationResolver) UpdateService(ctx context.Context, id string, input model.UpdateServiceInput) (*model.Service, error) {
  424. // Auth check
  425. if !auth.IsAuthenticated(ctx) {
  426. return nil, errors.New("unauthorized: authentication required")
  427. }
  428. if !auth.HasPermission(ctx, "service:update") {
  429. return nil, errors.New("unauthorized: missing service:update permission")
  430. }
  431. serviceID, err := toID(id)
  432. if err != nil {
  433. return nil, fmt.Errorf("invalid service ID: %w", err)
  434. }
  435. var existing models.Service
  436. if err := r.DB.Preload("Participants").First(&existing, serviceID).Error; err != nil {
  437. return nil, fmt.Errorf("service not found: %w", err)
  438. }
  439. if input.Name != nil {
  440. existing.Name = *input.Name
  441. }
  442. if input.Description != nil {
  443. existing.Description = *input.Description
  444. }
  445. if len(input.Participants) > 0 {
  446. participants := []models.User{}
  447. for _, participantIDStr := range input.Participants {
  448. participantID, err := toID(participantIDStr)
  449. if err != nil {
  450. return nil, fmt.Errorf("invalid participant ID: %w", err)
  451. }
  452. var user models.User
  453. if err := r.DB.First(&user, participantID).Error; err != nil {
  454. return nil, fmt.Errorf("participant not found: %w", err)
  455. }
  456. participants = append(participants, user)
  457. }
  458. existing.Participants = participants
  459. }
  460. if err := r.DB.Save(&existing).Error; err != nil {
  461. return nil, fmt.Errorf("failed to update service: %w", err)
  462. }
  463. // Reload with associations for response
  464. r.DB.Preload("Participants").Preload("Tasks").First(&existing, existing.ID)
  465. logging.LogMutation(ctx, "UPDATE", "SERVICE", existing.Name)
  466. return convertService(existing), nil
  467. }
  468. // DeleteService is the resolver for the deleteService field.
  469. func (r *mutationResolver) DeleteService(ctx context.Context, id string) (bool, error) {
  470. // Auth check
  471. if !auth.IsAuthenticated(ctx) {
  472. return false, errors.New("unauthorized: authentication required")
  473. }
  474. if !auth.HasPermission(ctx, "service:delete") {
  475. return false, errors.New("unauthorized: missing service:delete permission")
  476. }
  477. serviceID, err := toID(id)
  478. if err != nil {
  479. return false, fmt.Errorf("invalid service ID: %w", err)
  480. }
  481. result := r.DB.Delete(&models.Service{}, serviceID)
  482. if result.Error != nil {
  483. return false, fmt.Errorf("failed to delete service: %w", result.Error)
  484. }
  485. logging.LogMutation(ctx, "DELETE", "SERVICE", id)
  486. return result.RowsAffected > 0, nil
  487. }
  488. // CreateTask is the resolver for the createTask field.
  489. func (r *mutationResolver) CreateTask(ctx context.Context, input model.NewTask) (*model.Task, error) {
  490. // Auth check
  491. if !auth.IsAuthenticated(ctx) {
  492. return nil, errors.New("unauthorized: authentication required")
  493. }
  494. createdByID, err := toID(input.CreatedByID)
  495. if err != nil {
  496. return nil, fmt.Errorf("invalid created by ID: %w", err)
  497. }
  498. task := models.Task{
  499. Title: input.Title,
  500. Content: input.Content,
  501. CreatedByID: createdByID,
  502. Priority: input.Priority,
  503. }
  504. if input.AssigneeID != nil {
  505. assigneeID, err := toID(*input.AssigneeID)
  506. if err != nil {
  507. return nil, fmt.Errorf("invalid assignee ID: %w", err)
  508. }
  509. task.AssigneeID = &assigneeID
  510. }
  511. if input.StatusID != nil {
  512. statusID, err := toID(*input.StatusID)
  513. if err != nil {
  514. return nil, fmt.Errorf("invalid status ID: %w", err)
  515. }
  516. task.StatusID = statusID
  517. }
  518. if input.DueDate != nil {
  519. parsedTime, parseErr := time.Parse(time.RFC3339, *input.DueDate)
  520. if parseErr != nil {
  521. return nil, fmt.Errorf("invalid due date format: %w", parseErr)
  522. }
  523. task.DueDate = &parsedTime
  524. }
  525. if err := r.DB.Create(&task).Error; err != nil {
  526. return nil, fmt.Errorf("failed to create task: %w", err)
  527. }
  528. // Reload with associations
  529. r.DB.Preload("CreatedBy").Preload("Assignee").Preload("Status").First(&task, task.ID)
  530. logging.LogMutation(ctx, "CREATE", "TASK", task.Title)
  531. return convertTask(task), nil
  532. }
  533. // UpdateTask is the resolver for the updateTask field.
  534. func (r *mutationResolver) UpdateTask(ctx context.Context, id string, input model.UpdateTaskInput) (*model.Task, error) {
  535. // Auth check
  536. if !auth.IsAuthenticated(ctx) {
  537. return nil, errors.New("unauthorized: authentication required")
  538. }
  539. if !auth.HasPermission(ctx, "task:update") {
  540. return nil, errors.New("unauthorized: missing task:update permission")
  541. }
  542. taskID, err := toID(id)
  543. if err != nil {
  544. return nil, fmt.Errorf("invalid task ID: %w", err)
  545. }
  546. var existing models.Task
  547. if err := r.DB.Preload("CreatedBy").Preload("Assignee").First(&existing, taskID).Error; err != nil {
  548. return nil, fmt.Errorf("task not found: %w", err)
  549. }
  550. if input.Title != nil {
  551. existing.Title = *input.Title
  552. }
  553. if input.Content != nil {
  554. existing.Content = *input.Content
  555. }
  556. if input.AssigneeID != nil {
  557. if *input.AssigneeID == "" {
  558. existing.AssigneeID = nil
  559. } else {
  560. assigneeID, err := toID(*input.AssigneeID)
  561. if err != nil {
  562. return nil, fmt.Errorf("invalid assignee ID: %w", err)
  563. }
  564. existing.AssigneeID = &assigneeID
  565. }
  566. }
  567. if input.StatusID != nil {
  568. if *input.StatusID == "" {
  569. existing.StatusID = 0
  570. } else {
  571. statusID, err := toID(*input.StatusID)
  572. if err != nil {
  573. return nil, fmt.Errorf("invalid status ID: %w", err)
  574. }
  575. existing.StatusID = statusID
  576. }
  577. }
  578. if input.DueDate != nil {
  579. if *input.DueDate == "" {
  580. existing.DueDate = nil
  581. } else {
  582. parsedTime, parseErr := time.Parse(time.RFC3339, *input.DueDate)
  583. if parseErr != nil {
  584. return nil, fmt.Errorf("invalid due date format: %w", parseErr)
  585. }
  586. existing.DueDate = &parsedTime
  587. }
  588. }
  589. if input.Priority != nil {
  590. existing.Priority = *input.Priority
  591. }
  592. if err := r.DB.Save(&existing).Error; err != nil {
  593. return nil, fmt.Errorf("failed to update task: %w", err)
  594. }
  595. // Reload with associations for response
  596. r.DB.Preload("CreatedBy").Preload("Assignee").Preload("Status").First(&existing, existing.ID)
  597. logging.LogMutation(ctx, "UPDATE", "TASK", existing.Title)
  598. return convertTask(existing), nil
  599. }
  600. // DeleteTask is the resolver for the deleteTask field.
  601. func (r *mutationResolver) DeleteTask(ctx context.Context, id string) (bool, error) {
  602. // Auth check
  603. if !auth.IsAuthenticated(ctx) {
  604. return false, errors.New("unauthorized: authentication required")
  605. }
  606. if !auth.HasPermission(ctx, "task:delete") {
  607. return false, errors.New("unauthorized: missing task:delete permission")
  608. }
  609. taskID, err := toID(id)
  610. if err != nil {
  611. return false, fmt.Errorf("invalid task ID: %w", err)
  612. }
  613. result := r.DB.Delete(&models.Task{}, taskID)
  614. if result.Error != nil {
  615. return false, fmt.Errorf("failed to delete task: %w", result.Error)
  616. }
  617. logging.LogMutation(ctx, "DELETE", "TASK", id)
  618. return result.RowsAffected > 0, nil
  619. }
  620. // CreateTaskStatus is the resolver for the createTaskStatus field.
  621. func (r *mutationResolver) CreateTaskStatus(ctx context.Context, input model.NewTaskStatus) (*model.TaskStatus, error) {
  622. // Auth check
  623. if !auth.IsAuthenticated(ctx) {
  624. return nil, errors.New("unauthorized: authentication required")
  625. }
  626. taskStatus := models.TaskStatus{
  627. Code: input.Code,
  628. Label: input.Label,
  629. }
  630. if err := r.DB.Create(&taskStatus).Error; err != nil {
  631. return nil, fmt.Errorf("failed to create task status: %w", err)
  632. }
  633. logging.LogMutation(ctx, "CREATE", "TASKSTATUS", taskStatus.Code)
  634. return convertTaskStatus(taskStatus), nil
  635. }
  636. // UpdateTaskStatus is the resolver for the updateTaskStatus field.
  637. func (r *mutationResolver) UpdateTaskStatus(ctx context.Context, id string, input model.UpdateTaskStatusInput) (*model.TaskStatus, error) {
  638. // Auth check
  639. if !auth.IsAuthenticated(ctx) {
  640. return nil, errors.New("unauthorized: authentication required")
  641. }
  642. if !auth.HasPermission(ctx, "taskstatus:update") {
  643. return nil, errors.New("unauthorized: missing taskstatus:update permission")
  644. }
  645. statusID, err := toID(id)
  646. if err != nil {
  647. return nil, fmt.Errorf("invalid task status ID: %w", err)
  648. }
  649. var existing models.TaskStatus
  650. if err := r.DB.First(&existing, statusID).Error; err != nil {
  651. return nil, fmt.Errorf("task status not found: %w", err)
  652. }
  653. if input.Code != nil {
  654. existing.Code = *input.Code
  655. }
  656. if input.Label != nil {
  657. existing.Label = *input.Label
  658. }
  659. if err := r.DB.Save(&existing).Error; err != nil {
  660. return nil, fmt.Errorf("failed to update task status: %w", err)
  661. }
  662. // Reload with tasks for response
  663. r.DB.Preload("Tasks").First(&existing, existing.ID)
  664. logging.LogMutation(ctx, "UPDATE", "TASKSTATUS", existing.Code)
  665. return convertTaskStatus(existing), nil
  666. }
  667. // DeleteTaskStatus is the resolver for the deleteTaskStatus field.
  668. func (r *mutationResolver) DeleteTaskStatus(ctx context.Context, id string) (bool, error) {
  669. // Auth check
  670. if !auth.IsAuthenticated(ctx) {
  671. return false, errors.New("unauthorized: authentication required")
  672. }
  673. if !auth.HasPermission(ctx, "taskstatus:delete") {
  674. return false, errors.New("unauthorized: missing taskstatus:delete permission")
  675. }
  676. statusID, err := toID(id)
  677. if err != nil {
  678. return false, fmt.Errorf("invalid task status ID: %w", err)
  679. }
  680. result := r.DB.Delete(&models.TaskStatus{}, statusID)
  681. if result.Error != nil {
  682. return false, fmt.Errorf("failed to delete task status: %w", result.Error)
  683. }
  684. logging.LogMutation(ctx, "DELETE", "TASKSTATUS", id)
  685. return result.RowsAffected > 0, nil
  686. }
  687. // CreateChannel is the resolver for the createChannel field.
  688. func (r *mutationResolver) CreateChannel(ctx context.Context, input model.NewChannel) (*model.Channel, error) {
  689. // Auth check
  690. if !auth.IsAuthenticated(ctx) {
  691. return nil, errors.New("unauthorized: authentication required")
  692. }
  693. channel := models.Channel{}
  694. for _, participantIDStr := range input.Participants {
  695. participantID, err := toID(participantIDStr)
  696. if err != nil {
  697. return nil, fmt.Errorf("invalid participant ID: %w", err)
  698. }
  699. var user models.User
  700. if err := r.DB.First(&user, participantID).Error; err != nil {
  701. return nil, fmt.Errorf("participant not found: %w", err)
  702. }
  703. channel.Participants = append(channel.Participants, user)
  704. }
  705. if err := r.DB.Create(&channel).Error; err != nil {
  706. return nil, fmt.Errorf("failed to create channel: %w", err)
  707. }
  708. // Reload with participants
  709. r.DB.Preload("Participants").First(&channel, channel.ID)
  710. logging.LogMutation(ctx, "CREATE", "CHANNEL", fmt.Sprintf("id=%d", channel.ID))
  711. return convertChannel(channel), nil
  712. }
  713. // UpdateChannel is the resolver for the updateChannel field.
  714. func (r *mutationResolver) UpdateChannel(ctx context.Context, id string, input model.UpdateChannelInput) (*model.Channel, error) {
  715. // Auth check
  716. if !auth.IsAuthenticated(ctx) {
  717. return nil, errors.New("unauthorized: authentication required")
  718. }
  719. if !auth.HasPermission(ctx, "channel:update") {
  720. return nil, errors.New("unauthorized: missing channel:update permission")
  721. }
  722. channelID, err := toID(id)
  723. if err != nil {
  724. return nil, fmt.Errorf("invalid channel ID: %w", err)
  725. }
  726. var existing models.Channel
  727. if err := r.DB.Preload("Participants").First(&existing, channelID).Error; err != nil {
  728. return nil, fmt.Errorf("channel not found: %w", err)
  729. }
  730. participants := []models.User{}
  731. for _, participantIDStr := range input.Participants {
  732. participantID, err := toID(participantIDStr)
  733. if err != nil {
  734. return nil, fmt.Errorf("invalid participant ID: %w", err)
  735. }
  736. var user models.User
  737. if err := r.DB.First(&user, participantID).Error; err != nil {
  738. return nil, fmt.Errorf("participant not found: %w", err)
  739. }
  740. participants = append(participants, user)
  741. }
  742. existing.Participants = participants
  743. if err := r.DB.Save(&existing).Error; err != nil {
  744. return nil, fmt.Errorf("failed to update channel: %w", err)
  745. }
  746. logging.LogMutation(ctx, "UPDATE", "CHANNEL", id)
  747. return convertChannel(existing), nil
  748. }
  749. // DeleteChannel is the resolver for the deleteChannel field.
  750. func (r *mutationResolver) DeleteChannel(ctx context.Context, id string) (bool, error) {
  751. // Auth check
  752. if !auth.IsAuthenticated(ctx) {
  753. return false, errors.New("unauthorized: authentication required")
  754. }
  755. if !auth.HasPermission(ctx, "channel:delete") {
  756. return false, errors.New("unauthorized: missing channel:delete permission")
  757. }
  758. channelID, err := toID(id)
  759. if err != nil {
  760. return false, fmt.Errorf("invalid channel ID: %w", err)
  761. }
  762. result := r.DB.Delete(&models.Channel{}, channelID)
  763. if result.Error != nil {
  764. return false, fmt.Errorf("failed to delete channel: %w", result.Error)
  765. }
  766. logging.LogMutation(ctx, "DELETE", "CHANNEL", id)
  767. return result.RowsAffected > 0, nil
  768. }
  769. // CreateMessage is the resolver for the createMessage field.
  770. func (r *mutationResolver) CreateMessage(ctx context.Context, input model.NewMessage) (*model.Message, error) {
  771. // Auth check
  772. if !auth.IsAuthenticated(ctx) {
  773. return nil, errors.New("unauthorized: authentication required")
  774. }
  775. conversationID, err := toID(input.ConversationID)
  776. if err != nil {
  777. return nil, fmt.Errorf("invalid conversation ID: %w", err)
  778. }
  779. senderID, err := toID(input.SenderID)
  780. if err != nil {
  781. return nil, fmt.Errorf("invalid sender ID: %w", err)
  782. }
  783. message := models.Message{
  784. ConversationID: conversationID,
  785. SenderID: senderID,
  786. Content: input.Content,
  787. }
  788. if err := r.DB.Create(&message).Error; err != nil {
  789. return nil, fmt.Errorf("failed to create message: %w", err)
  790. }
  791. // Reload with associations
  792. r.DB.Preload("Sender").First(&message, message.ID)
  793. logging.LogMutation(ctx, "CREATE", "MESSAGE", fmt.Sprintf("id=%d", message.ID))
  794. return convertMessage(message), nil
  795. }
  796. // UpdateMessage is the resolver for the updateMessage field.
  797. func (r *mutationResolver) UpdateMessage(ctx context.Context, id string, input model.UpdateMessageInput) (*model.Message, error) {
  798. // Auth check
  799. if !auth.IsAuthenticated(ctx) {
  800. return nil, errors.New("unauthorized: authentication required")
  801. }
  802. if !auth.HasPermission(ctx, "message:update") {
  803. return nil, errors.New("unauthorized: missing message:update permission")
  804. }
  805. messageID, err := toID(id)
  806. if err != nil {
  807. return nil, fmt.Errorf("invalid message ID: %w", err)
  808. }
  809. var existing models.Message
  810. if err := r.DB.Preload("Sender").First(&existing, messageID).Error; err != nil {
  811. return nil, fmt.Errorf("message not found: %w", err)
  812. }
  813. if input.ConversationID != nil {
  814. conversationID, err := toID(*input.ConversationID)
  815. if err != nil {
  816. return nil, fmt.Errorf("invalid conversation ID: %w", err)
  817. }
  818. existing.ConversationID = conversationID
  819. }
  820. if input.SenderID != nil {
  821. senderID, err := toID(*input.SenderID)
  822. if err != nil {
  823. return nil, fmt.Errorf("invalid sender ID: %w", err)
  824. }
  825. existing.SenderID = senderID
  826. }
  827. if input.Content != nil {
  828. existing.Content = *input.Content
  829. }
  830. if err := r.DB.Save(&existing).Error; err != nil {
  831. return nil, fmt.Errorf("failed to update message: %w", err)
  832. }
  833. logging.LogMutation(ctx, "UPDATE", "MESSAGE", id)
  834. return convertMessage(existing), nil
  835. }
  836. // DeleteMessage is the resolver for the deleteMessage field.
  837. func (r *mutationResolver) DeleteMessage(ctx context.Context, id string) (bool, error) {
  838. // Auth check
  839. if !auth.IsAuthenticated(ctx) {
  840. return false, errors.New("unauthorized: authentication required")
  841. }
  842. if !auth.HasPermission(ctx, "message:delete") {
  843. return false, errors.New("unauthorized: missing message:delete permission")
  844. }
  845. messageID, err := toID(id)
  846. if err != nil {
  847. return false, fmt.Errorf("invalid message ID: %w", err)
  848. }
  849. result := r.DB.Delete(&models.Message{}, messageID)
  850. if result.Error != nil {
  851. return false, fmt.Errorf("failed to delete message: %w", result.Error)
  852. }
  853. logging.LogMutation(ctx, "DELETE", "MESSAGE", id)
  854. return result.RowsAffected > 0, nil
  855. }
  856. // Users is the resolver for the users field.
  857. func (r *queryResolver) Users(ctx context.Context) ([]*model.User, error) {
  858. // Auth check
  859. if !auth.IsAuthenticated(ctx) {
  860. return nil, errors.New("unauthorized: authentication required")
  861. }
  862. var users []models.User
  863. if err := r.DB.Find(&users).Error; err != nil {
  864. return nil, fmt.Errorf("failed to fetch users: %w", err)
  865. }
  866. logging.LogQuery(ctx, "USERS", "all")
  867. return convertUsers(users), nil
  868. }
  869. // User is the resolver for the user field.
  870. func (r *queryResolver) User(ctx context.Context, id string) (*model.User, error) {
  871. // Auth check
  872. if !auth.IsAuthenticated(ctx) {
  873. return nil, errors.New("unauthorized: authentication required")
  874. }
  875. userID, err := toID(id)
  876. if err != nil {
  877. return nil, fmt.Errorf("invalid user ID: %w", err)
  878. }
  879. var user models.User
  880. if err := r.DB.Preload("Roles.Permissions").First(&user, userID).Error; err != nil {
  881. return nil, fmt.Errorf("user not found: %w", err)
  882. }
  883. logging.LogQuery(ctx, "USER", id)
  884. return convertUser(user), nil
  885. }
  886. // Notes is the resolver for the notes field.
  887. func (r *queryResolver) Notes(ctx context.Context) ([]*model.Note, error) {
  888. // Auth check
  889. if !auth.IsAuthenticated(ctx) {
  890. return nil, errors.New("unauthorized: authentication required")
  891. }
  892. var notes []models.Note
  893. if err := r.DB.Preload("User").Preload("Service").Find(&notes).Error; err != nil {
  894. return nil, fmt.Errorf("failed to fetch notes: %w", err)
  895. }
  896. logging.LogQuery(ctx, "NOTES", "all")
  897. return convertNotes(notes), nil
  898. }
  899. // Note is the resolver for the note field.
  900. func (r *queryResolver) Note(ctx context.Context, id string) (*model.Note, error) {
  901. // Auth check
  902. if !auth.IsAuthenticated(ctx) {
  903. return nil, errors.New("unauthorized: authentication required")
  904. }
  905. noteID, err := toID(id)
  906. if err != nil {
  907. return nil, fmt.Errorf("invalid note ID: %w", err)
  908. }
  909. var note models.Note
  910. if err := r.DB.Preload("User").Preload("Service").First(&note, noteID).Error; err != nil {
  911. return nil, fmt.Errorf("note not found: %w", err)
  912. }
  913. logging.LogQuery(ctx, "NOTE", id)
  914. return convertNote(note), nil
  915. }
  916. // Roles is the resolver for the roles field.
  917. func (r *queryResolver) Roles(ctx context.Context) ([]*model.Role, error) {
  918. // Auth check
  919. if !auth.IsAuthenticated(ctx) {
  920. return nil, errors.New("unauthorized: authentication required")
  921. }
  922. var roles []models.Role
  923. if err := r.DB.Preload("Permissions").Find(&roles).Error; err != nil {
  924. return nil, fmt.Errorf("failed to fetch roles: %w", err)
  925. }
  926. logging.LogQuery(ctx, "ROLES", "all")
  927. return convertRoles(roles), nil
  928. }
  929. // Role is the resolver for the role field.
  930. func (r *queryResolver) Role(ctx context.Context, id string) (*model.Role, error) {
  931. // Auth check
  932. if !auth.IsAuthenticated(ctx) {
  933. return nil, errors.New("unauthorized: authentication required")
  934. }
  935. roleID, err := toID(id)
  936. if err != nil {
  937. return nil, fmt.Errorf("invalid role ID: %w", err)
  938. }
  939. var role models.Role
  940. if err := r.DB.Preload("Permissions").First(&role, roleID).Error; err != nil {
  941. return nil, fmt.Errorf("role not found: %w", err)
  942. }
  943. logging.LogQuery(ctx, "ROLE", id)
  944. return convertRole(role), nil
  945. }
  946. // Permissions is the resolver for the permissions field.
  947. func (r *queryResolver) Permissions(ctx context.Context) ([]*model.Permission, error) {
  948. // Auth check
  949. if !auth.IsAuthenticated(ctx) {
  950. return nil, errors.New("unauthorized: authentication required")
  951. }
  952. var perms []models.Permission
  953. if err := r.DB.Find(&perms).Error; err != nil {
  954. return nil, fmt.Errorf("failed to fetch permissions: %w", err)
  955. }
  956. logging.LogQuery(ctx, "PERMISSIONS", "all")
  957. return convertPermissions(perms), nil
  958. }
  959. // Permission is the resolver for the permission field.
  960. func (r *queryResolver) Permission(ctx context.Context, id string) (*model.Permission, error) {
  961. // Auth check
  962. if !auth.IsAuthenticated(ctx) {
  963. return nil, errors.New("unauthorized: authentication required")
  964. }
  965. permID, err := toID(id)
  966. if err != nil {
  967. return nil, fmt.Errorf("invalid permission ID: %w", err)
  968. }
  969. var perm models.Permission
  970. if err := r.DB.First(&perm, permID).Error; err != nil {
  971. return nil, fmt.Errorf("permission not found: %w", err)
  972. }
  973. logging.LogQuery(ctx, "PERMISSION", id)
  974. return convertPermission(perm), nil
  975. }
  976. // Services is the resolver for the services field.
  977. func (r *queryResolver) Services(ctx context.Context) ([]*model.Service, error) {
  978. // Auth check
  979. if !auth.IsAuthenticated(ctx) {
  980. return nil, errors.New("unauthorized: authentication required")
  981. }
  982. var services []models.Service
  983. if err := r.DB.Preload("CreatedBy").Preload("Participants").Preload("Tasks").Find(&services).Error; err != nil {
  984. return nil, fmt.Errorf("failed to fetch services: %w", err)
  985. }
  986. logging.LogQuery(ctx, "SERVICES", "all")
  987. return convertServices(services), nil
  988. }
  989. // Service is the resolver for the service field.
  990. func (r *queryResolver) Service(ctx context.Context, id string) (*model.Service, error) {
  991. // Auth check
  992. if !auth.IsAuthenticated(ctx) {
  993. return nil, errors.New("unauthorized: authentication required")
  994. }
  995. serviceID, err := toID(id)
  996. if err != nil {
  997. return nil, fmt.Errorf("invalid service ID: %w", err)
  998. }
  999. var service models.Service
  1000. if err := r.DB.Preload("CreatedBy").Preload("Participants").Preload("Tasks").First(&service, serviceID).Error; err != nil {
  1001. return nil, fmt.Errorf("service not found: %w", err)
  1002. }
  1003. logging.LogQuery(ctx, "SERVICE", id)
  1004. return convertService(service), nil
  1005. }
  1006. // Tasks is the resolver for the tasks field.
  1007. func (r *queryResolver) Tasks(ctx context.Context) ([]*model.Task, error) {
  1008. // Auth check
  1009. if !auth.IsAuthenticated(ctx) {
  1010. return nil, errors.New("unauthorized: authentication required")
  1011. }
  1012. var tasks []models.Task
  1013. if err := r.DB.Preload("CreatedBy").Preload("Assignee").Preload("Status").Find(&tasks).Error; err != nil {
  1014. return nil, fmt.Errorf("failed to fetch tasks: %w", err)
  1015. }
  1016. logging.LogQuery(ctx, "TASKS", "all")
  1017. return convertTasks(tasks), nil
  1018. }
  1019. // Task is the resolver for the task field.
  1020. func (r *queryResolver) Task(ctx context.Context, id string) (*model.Task, error) {
  1021. // Auth check
  1022. if !auth.IsAuthenticated(ctx) {
  1023. return nil, errors.New("unauthorized: authentication required")
  1024. }
  1025. taskID, err := toID(id)
  1026. if err != nil {
  1027. return nil, fmt.Errorf("invalid task ID: %w", err)
  1028. }
  1029. var task models.Task
  1030. if err := r.DB.Preload("CreatedBy").Preload("Assignee").Preload("Status").First(&task, taskID).Error; err != nil {
  1031. return nil, fmt.Errorf("task not found: %w", err)
  1032. }
  1033. logging.LogQuery(ctx, "TASK", id)
  1034. return convertTask(task), nil
  1035. }
  1036. // TaskStatuses is the resolver for the taskStatuses field.
  1037. func (r *queryResolver) TaskStatuses(ctx context.Context) ([]*model.TaskStatus, error) {
  1038. // Auth check
  1039. if !auth.IsAuthenticated(ctx) {
  1040. return nil, errors.New("unauthorized: authentication required")
  1041. }
  1042. var statuses []models.TaskStatus
  1043. if err := r.DB.Preload("Tasks").Find(&statuses).Error; err != nil {
  1044. return nil, fmt.Errorf("failed to fetch task statuses: %w", err)
  1045. }
  1046. logging.LogQuery(ctx, "TASKSTATUSES", "all")
  1047. return convertTaskStatuses(statuses), nil
  1048. }
  1049. // TaskStatus
  1050. func (r *queryResolver) TaskStatus(ctx context.Context, id string) (*model.TaskStatus, error) {
  1051. // Auth check
  1052. if !auth.IsAuthenticated(ctx) {
  1053. return nil, errors.New("unauthorized: authentication required")
  1054. }
  1055. statusID, err := toID(id)
  1056. if err != nil {
  1057. return nil, fmt.Errorf("invalid task status ID: %w", err)
  1058. }
  1059. var status models.TaskStatus
  1060. if err := r.DB.Preload("Tasks").First(&status, statusID).Error; err != nil {
  1061. return nil, fmt.Errorf("task status not found: %w", err)
  1062. }
  1063. logging.LogQuery(ctx, "TASKSTATUS", id)
  1064. return convertTaskStatus(status), nil
  1065. }
  1066. // Channels is the resolver for the channels field.
  1067. func (r *queryResolver) Channels(ctx context.Context) ([]*model.Channel, error) {
  1068. // Auth check
  1069. if !auth.IsAuthenticated(ctx) {
  1070. return nil, errors.New("unauthorized: authentication required")
  1071. }
  1072. var channels []models.Channel
  1073. if err := r.DB.Preload("Participants").Find(&channels).Error; err != nil {
  1074. return nil, fmt.Errorf("failed to fetch channels: %w", err)
  1075. }
  1076. logging.LogQuery(ctx, "CHANNELS", "all")
  1077. return convertChannels(channels), nil
  1078. }
  1079. // Channel is the resolver for the channel field.
  1080. func (r *queryResolver) Channel(ctx context.Context, id string) (*model.Channel, error) {
  1081. // Auth check
  1082. if !auth.IsAuthenticated(ctx) {
  1083. return nil, errors.New("unauthorized: authentication required")
  1084. }
  1085. channelID, err := toID(id)
  1086. if err != nil {
  1087. return nil, fmt.Errorf("invalid channel ID: %w", err)
  1088. }
  1089. var channel models.Channel
  1090. if err := r.DB.Preload("Participants").First(&channel, channelID).Error; err != nil {
  1091. return nil, fmt.Errorf("channel not found: %w", err)
  1092. }
  1093. logging.LogQuery(ctx, "CHANNEL", id)
  1094. return convertChannel(channel), nil
  1095. }
  1096. // Messages is the resolver for the messages field.
  1097. func (r *queryResolver) Messages(ctx context.Context) ([]*model.Message, error) {
  1098. // Auth check
  1099. if !auth.IsAuthenticated(ctx) {
  1100. return nil, errors.New("unauthorized: authentication required")
  1101. }
  1102. var messages []models.Message
  1103. if err := r.DB.Preload("Sender").Find(&messages).Error; err != nil {
  1104. return nil, fmt.Errorf("failed to fetch messages: %w", err)
  1105. }
  1106. logging.LogQuery(ctx, "MESSAGES", "all")
  1107. return convertMessages(messages), nil
  1108. }
  1109. // Message is the resolver for the message field.
  1110. func (r *queryResolver) Message(ctx context.Context, id string) (*model.Message, error) {
  1111. // Auth check
  1112. if !auth.IsAuthenticated(ctx) {
  1113. return nil, errors.New("unauthorized: authentication required")
  1114. }
  1115. messageID, err := toID(id)
  1116. if err != nil {
  1117. return nil, fmt.Errorf("invalid message ID: %w", err)
  1118. }
  1119. var message models.Message
  1120. if err := r.DB.Preload("Sender").First(&message, messageID).Error; err != nil {
  1121. return nil, fmt.Errorf("message not found: %w", err)
  1122. }
  1123. logging.LogQuery(ctx, "MESSAGE", id)
  1124. return convertMessage(message), nil
  1125. }
  1126. // TaskCreated is the resolver for the taskCreated field.
  1127. func (r *subscriptionResolver) TaskCreated(ctx context.Context) (<-chan *model.Task, error) {
  1128. return nil, nil
  1129. }
  1130. // TaskUpdated is the resolver for the taskUpdated field.
  1131. func (r *subscriptionResolver) TaskUpdated(ctx context.Context) (<-chan *model.Task, error) {
  1132. return nil, nil
  1133. }
  1134. // TaskDeleted is the resolver for the taskDeleted field.
  1135. func (r *subscriptionResolver) TaskDeleted(ctx context.Context) (<-chan *model.Task, error) {
  1136. return nil, nil
  1137. }
  1138. // MessageAdded is the resolver for the messageAdded field.
  1139. func (r *subscriptionResolver) MessageAdded(ctx context.Context) (<-chan *model.Message, error) {
  1140. return nil, nil
  1141. }
  1142. // Mutation returns MutationResolver implementation.
  1143. func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }
  1144. // Query returns QueryResolver implementation.
  1145. func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
  1146. // Subscription returns SubscriptionResolver implementation.
  1147. func (r *Resolver) Subscription() SubscriptionResolver { return &subscriptionResolver{r} }
  1148. type mutationResolver struct{ *Resolver }
  1149. type queryResolver struct{ *Resolver }
  1150. type subscriptionResolver struct{ *Resolver }