User.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { appApi } from 'src/boot/axios.js'
  2. import { User, UserList } from 'src/models/User.js'
  3. import APIRepository from '../classes/APIRepository.js'
  4. export default class UserAPI extends APIRepository {
  5. constructor() {
  6. super('user', appApi, '/uma/users', User)
  7. this.APIAdresses = {
  8. base: '/uma/users',
  9. professors: '/uma/users/professors',
  10. exportReport: '/uma/users/export_report',
  11. verify: '/uma/users/verify',
  12. current: '/uma/users/current',
  13. byId: (id) => '/uma/users/' + id,
  14. changeRole: (id) => '/uma/users/' + id + '/change_role',
  15. unassignedGradersWithLastPassedUnit: '/uma/users/unassigned_graders_with_last_passed_unit',
  16. unassignedLeadersWithLastPassedUnit: '/uma/users/unassigned_leaders_with_last_passed_unit'
  17. }
  18. this.CacheList = {
  19. base: this.name + this.APIAdresses.base,
  20. current: this.name + this.APIAdresses.current,
  21. verify: this.name + this.APIAdresses.verify,
  22. byId: (id) => this.name + this.APIAdresses.byId(id),
  23. changeRole: (id) => this.name + this.APIAdresses.changeRole(id)
  24. }
  25. this.restUrl = (id) => this.APIAdresses.byId(id)
  26. /* Setting the callback functions for the CRUD operations. */
  27. this.setCrudCallbacks({
  28. get: (response) => { return new User(response.data) },
  29. post: (response) => { return new User(response.data) },
  30. put: (response) => { return new User(response.data) },
  31. delete: (response) => { return response.data }
  32. })
  33. }
  34. index(data) {
  35. return this.sendRequest({
  36. apiMethod: 'get',
  37. api: this.api,
  38. request: this.APIAdresses.base,
  39. data: this.getNormalizedSendData({
  40. role: null, // String
  41. per_page: 10, // Number
  42. page: 1 // Number
  43. }, data),
  44. resolveCallback: (response) => {
  45. const paginate = response.data
  46. const results = response.data.results
  47. delete paginate.results
  48. return {
  49. list: new UserList(results),
  50. paginate
  51. // {
  52. // "count": 1,
  53. // "num_pages": 1,
  54. // "per_page": 10,
  55. // "current": 1,
  56. // "next": null,
  57. // "previous": null,
  58. // }
  59. }
  60. },
  61. rejectCallback: (error) => {
  62. return error
  63. }
  64. })
  65. }
  66. professors(data) {
  67. return this.sendRequest({
  68. apiMethod: 'get',
  69. api: this.api,
  70. request: this.APIAdresses.professors,
  71. data: this.getNormalizedSendData({
  72. per_page: 10, // Number
  73. page: 1 // Number
  74. }, data),
  75. resolveCallback: (response) => {
  76. const paginate = response.data
  77. const results = response.data.results
  78. delete paginate.results
  79. return {
  80. list: new UserList(results),
  81. paginate
  82. // {
  83. // "count": 1,
  84. // "num_pages": 1,
  85. // "per_page": 10,
  86. // "current": 1,
  87. // "next": null,
  88. // "previous": null,
  89. // }
  90. }
  91. },
  92. rejectCallback: (error) => {
  93. return error
  94. }
  95. })
  96. }
  97. exportReport(data) {
  98. return this.sendRequest({
  99. apiMethod: 'get',
  100. api: this.api,
  101. request: this.APIAdresses.exportReport,
  102. data,
  103. responseType: 'blob',
  104. resolveCallback: (response) => {
  105. return response.data // xlsxData
  106. },
  107. rejectCallback: (error) => {
  108. return error
  109. }
  110. })
  111. }
  112. updateCurrent(data = {}) {
  113. return this.sendRequest({
  114. apiMethod: 'put',
  115. api: this.api,
  116. request: this.APIAdresses.current,
  117. data: this.getNormalizedSendData({
  118. firstname: '', // String
  119. lastname: '', // String
  120. gender: 'MALE', // String
  121. marital_status: 'UNKNOWN', // String
  122. birthdate: null, // String
  123. national_code: '', // String
  124. has_seminary_education: false, // Boolean
  125. last_academic_degree: 'UNDER_DIPLOMA', // String
  126. last_academic_degree_field: null, // String
  127. last_academy_name: null, // String
  128. educational_status: null, // String
  129. phone_number: null, // String
  130. on_social_network_mobile_number: null, // String
  131. on_call_mobile_number: null, // String
  132. mobile_number: null, // String
  133. email: '', // String
  134. is_abroad_birth_address: 'False', // String
  135. is_abroad_living_address: 'False', // String
  136. birth_country: 'ایران', // String
  137. living_country: 'ایران', // String
  138. birth_province: null, // String
  139. living_province: null, // String
  140. birth_city: null, // String
  141. living_city: null, // String
  142. living_address: null, // String
  143. living_postal_code: null, // String
  144. living_address_additional_info: null // String
  145. }, data),
  146. resolveCallback: (response) => {
  147. return new User(response.data)
  148. },
  149. rejectCallback: (error) => {
  150. return error
  151. }
  152. })
  153. }
  154. getCurrent() {
  155. return this.sendRequest({
  156. apiMethod: 'get',
  157. api: this.api,
  158. request: this.APIAdresses.current,
  159. resolveCallback: (response) => {
  160. return new User(response.data)
  161. },
  162. rejectCallback: (error) => {
  163. return error
  164. }
  165. })
  166. }
  167. updateByAdmin(data = {}) {
  168. return this.sendRequest({
  169. apiMethod: 'put',
  170. api: this.api,
  171. request: this.APIAdresses.byId(data.id),
  172. data: this.getNormalizedSendData({
  173. firstname: '', // String
  174. lastname: '', // String
  175. gender: 'MALE', // String
  176. marital_status: 'UNKNOWN', // String
  177. birthdate: null, // String
  178. national_code: '', // String
  179. is_active: false, // Boolean
  180. has_seminary_education: false, // Boolean
  181. last_academic_degree: 'UNDER_DIPLOMA', // String
  182. last_academic_degree_field: null, // String
  183. last_academy_name: null, // String
  184. educational_status: null, // String
  185. phone_number: null, // String
  186. on_social_network_mobile_number: null, // String
  187. mobile_number: null, // String
  188. email: '', // String
  189. is_abroad_birth_address: 'False', // String
  190. is_abroad_living_address: 'False', // String
  191. birth_country: 'ایران', // String
  192. living_country: 'ایران', // String
  193. birth_province: null, // String
  194. living_province: null, // String
  195. birth_city: null, // String
  196. living_city: null, // String
  197. living_address: null, // String
  198. living_postal_code: null, // String
  199. living_address_additional_info: null // String
  200. }, data),
  201. resolveCallback: (response) => {
  202. return new User(response.data)
  203. },
  204. rejectCallback: (error) => {
  205. return error
  206. }
  207. })
  208. }
  209. }