Classroom.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { appApi } from 'src/boot/axios.js'
  2. import { Invoice } from 'src/models/Invoice.js'
  3. import APIRepository from '../classes/APIRepository.js'
  4. import { Registration } from 'src/models/Registration.js'
  5. import { Classroom, ClassroomList } from 'src/models/Classroom.js'
  6. export default class ClassroomAPI extends APIRepository {
  7. constructor() {
  8. super('classrooms', appApi, '/lma/classrooms', Classroom)
  9. this.APIAdresses = {
  10. base: '/lma/classrooms',
  11. myAsGraderClassrooms: '/lma/classrooms/my_as_a_grader_classrooms',
  12. byId: (id) => '/lma/classrooms/' + id,
  13. toggleOngoingStatus: (id) => '/lma/classrooms/' + id + '/toggle_ongoing_status',
  14. enroll: (id) => '/lma/classrooms/' + id + '/enroll',
  15. graders: (id) => '/lma/classrooms/' + id + '/graders',
  16. drop: (id) => '/lma/classrooms/' + id + '/drop',
  17. register: (id) => '/lma/classrooms/' + id + '/register',
  18. leaders: (id) => '/lma/classrooms/' + id + '/leaders',
  19. announceResult: (id) => '/lma/classrooms/' + id + '/announce_result',
  20. getClassroomAndCorrespondingRegistration: (classroomId, ownerId) => '/lma/classrooms/' + classroomId + '/get_classroom_and_corresponding_registration/' + ownerId,
  21. members: '/lma/registrations',
  22. activitySheet: '/lma/registrations/activity_sheet',
  23. exportActivitySheetReport: '/lma/registrations/export_report',
  24. enrollByAdmin: (classroomId, userId) => '/lma/classrooms/' + classroomId + '/enrolll?user_id=' + userId,
  25. dropByAdmin: (classroomId) => '/lma/classrooms/' + classroomId + '/drop',
  26. createInvoice: (id) => '/lma/classrooms/' + id + '/create_invoice'
  27. }
  28. this.CacheList = {
  29. base: this.name + this.APIAdresses.base,
  30. byId: (id) => this.name + this.APIAdresses.byId(id)
  31. }
  32. }
  33. index (data) {
  34. return this.sendRequest({
  35. apiMethod: 'get',
  36. api: this.api,
  37. request: this.APIAdresses.base,
  38. data: this.getNormalizedSendData({
  39. unit__category__type: null, // String
  40. per_page: 10, // Number
  41. page: 1 // Number
  42. }, data),
  43. resolveCallback: (response) => {
  44. const paginate = response.data
  45. const results = response.data.results
  46. delete paginate.results
  47. return {
  48. list: new ClassroomList(results),
  49. paginate
  50. // {
  51. // "count": 1,
  52. // "num_pages": 1,
  53. // "per_page": 10,
  54. // "current": 1,
  55. // "next": null,
  56. // "previous": null,
  57. // }
  58. }
  59. },
  60. rejectCallback: (error) => {
  61. return error
  62. }
  63. })
  64. }
  65. exportActivitySheetReport(data) {
  66. return this.sendRequest({
  67. apiMethod: 'get',
  68. api: this.api,
  69. request: this.APIAdresses.exportActivitySheetReport,
  70. data: this.getNormalizedSendData({
  71. classroom: null, // Number
  72. type: null, // String
  73. status: null // String
  74. }, data),
  75. responseType: 'blob',
  76. resolveCallback: (response) => {
  77. return response.data // xlsxData
  78. },
  79. rejectCallback: (error) => {
  80. return error
  81. }
  82. })
  83. }
  84. get (id) {
  85. return this.sendRequest({
  86. apiMethod: 'get',
  87. api: this.api,
  88. request: this.APIAdresses.byId(id),
  89. resolveCallback: (response) => {
  90. return new Classroom(response.data)
  91. },
  92. rejectCallback: (error) => {
  93. return error
  94. }
  95. })
  96. }
  97. getClassroomAndCorrespondingRegistration (classroomId, ownerId) {
  98. return this.sendRequest({
  99. apiMethod: 'get',
  100. api: this.api,
  101. request: this.APIAdresses.getClassroomAndCorrespondingRegistration(classroomId, ownerId),
  102. resolveCallback: (response) => {
  103. return new Classroom(response.data)
  104. },
  105. rejectCallback: (error) => {
  106. return error
  107. }
  108. })
  109. }
  110. register (id) {
  111. return this.sendRequest({
  112. apiMethod: 'put',
  113. api: this.api,
  114. request: this.APIAdresses.register(id),
  115. resolveCallback: (response) => {
  116. const data = {
  117. type: null,
  118. model: response.data
  119. }
  120. if (response.data.status === 'PAYING') {
  121. data.type = 'Invoice'
  122. data.model = new Invoice(response.data)
  123. } else if (response.data.status === 'REGISTERED') {
  124. data.type = 'Registration'
  125. data.model = new Registration(response.data)
  126. }
  127. return data
  128. },
  129. rejectCallback: (error) => {
  130. return error
  131. }
  132. })
  133. }
  134. toggleOngoingStatus (id) {
  135. return this.sendRequest({
  136. apiMethod: 'put',
  137. api: this.api,
  138. request: this.APIAdresses.toggleOngoingStatus(id),
  139. resolveCallback: (response) => {
  140. return new Classroom(response.data)
  141. },
  142. rejectCallback: (error) => {
  143. return error
  144. }
  145. })
  146. }
  147. enrollByUser (id) {
  148. return this.sendRequest({
  149. apiMethod: 'put',
  150. api: this.api,
  151. request: this.APIAdresses.enroll(id),
  152. resolveCallback: (response) => {
  153. return new Classroom(response.data)
  154. },
  155. rejectCallback: (error) => {
  156. return error
  157. }
  158. })
  159. }
  160. dropByUser (id) {
  161. return this.sendRequest({
  162. apiMethod: 'put',
  163. api: this.api,
  164. request: this.APIAdresses.drop(id),
  165. resolveCallback: (response) => {
  166. return new Classroom(response.data)
  167. },
  168. rejectCallback: (error) => {
  169. return error
  170. }
  171. })
  172. }
  173. appendLeaders (data) {
  174. return this.sendRequest({
  175. apiMethod: 'put',
  176. api: this.api,
  177. request: this.APIAdresses.leaders(data.classroomId),
  178. data: {
  179. leaders: data.leaders // array of numbers
  180. },
  181. resolveCallback: (response) => {
  182. return new Classroom(response.data)
  183. },
  184. rejectCallback: (error) => {
  185. return error
  186. }
  187. })
  188. }
  189. dropByAdmin (classroomId, userId) {
  190. return this.sendRequest({
  191. apiMethod: 'put',
  192. api: this.api,
  193. request: this.APIAdresses.dropByAdmin(classroomId),
  194. data: {
  195. owner: userId
  196. },
  197. resolveCallback: (response) => {
  198. return new Classroom(response.data)
  199. },
  200. rejectCallback: (error) => {
  201. return error
  202. }
  203. })
  204. }
  205. createInvoice (id) {
  206. return this.sendRequest({
  207. apiMethod: 'put',
  208. api: this.api,
  209. request: this.APIAdresses.createInvoice(id),
  210. resolveCallback: (response) => {
  211. return new Invoice(response.data)
  212. },
  213. rejectCallback: (error) => {
  214. return error
  215. }
  216. })
  217. }
  218. appendGraders (data) {
  219. return this.sendRequest({
  220. apiMethod: 'put',
  221. api: this.api,
  222. request: this.APIAdresses.graders(data.classroomId),
  223. data: {
  224. classroomId: data.classroomId,
  225. graders: data.graders
  226. },
  227. resolveCallback: (response) => {
  228. return new ClassroomList(response.data)
  229. },
  230. rejectCallback: (error) => {
  231. return error
  232. }
  233. })
  234. }
  235. createInvoiceByAdmin (classroomId, userId) {
  236. return this.sendRequest({
  237. apiMethod: 'put',
  238. api: this.api,
  239. request: this.APIAdresses.createInvoice(classroomId),
  240. data: { owner: userId },
  241. resolveCallback: (response) => {
  242. return new Invoice(response.data)
  243. },
  244. rejectCallback: (error) => {
  245. return error
  246. }
  247. })
  248. }
  249. notifyAnnounceResult (classroomId) {
  250. return this.sendRequest({
  251. apiMethod: 'put',
  252. api: this.api,
  253. request: this.APIAdresses.announceResult(classroomId),
  254. resolveCallback: (response) => {
  255. return response.data
  256. },
  257. rejectCallback: (error) => {
  258. return error
  259. }
  260. })
  261. }
  262. }