Classroom.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. bulkEnroll: (id) => '/lma/classrooms/' + id + '/bulk_enroll',
  16. graders: (id) => '/lma/classrooms/' + id + '/graders',
  17. drop: (id) => '/lma/classrooms/' + id + '/drop',
  18. register: (id) => '/lma/classrooms/' + id + '/register',
  19. bulkRegister: (id) => '/lma/classrooms/' + id + '/bulk_register',
  20. leaders: (id) => '/lma/classrooms/' + id + '/leaders',
  21. announceResult: (id) => '/lma/classrooms/' + id + '/announce_result',
  22. getClassroomAndCorrespondingRegistration: (classroomId, ownerId) => '/lma/classrooms/' + classroomId + '/get_classroom_and_corresponding_registration/' + ownerId,
  23. members: '/lma/registrations',
  24. activitySheet: '/lma/registrations/activity_sheet',
  25. exportActivitySheetReport: '/lma/registrations/export_report',
  26. enrollByAdmin: (classroomId, userId) => '/lma/classrooms/' + classroomId + '/enrolll?user_id=' + userId,
  27. dropByAdmin: (classroomId) => '/lma/classrooms/' + classroomId + '/drop',
  28. createInvoice: (id) => '/lma/classrooms/' + id + '/create_invoice'
  29. }
  30. this.CacheList = {
  31. base: this.name + this.APIAdresses.base,
  32. index: (filters) => this.name + this.APIAdresses.base + encodeURI(filters),
  33. byId: (id) => this.name + this.APIAdresses.byId(id)
  34. }
  35. }
  36. index (data) {
  37. const normalizedSendData = this.getNormalizedSendData({
  38. unit__category__type: null, // String
  39. per_page: 10, // Number
  40. page: 1 // Number
  41. }, data)
  42. return this.sendRequest({
  43. apiMethod: 'get',
  44. api: this.api,
  45. request: this.APIAdresses.base,
  46. cacheKey: this.CacheList.index(normalizedSendData),
  47. data: normalizedSendData,
  48. resolveCallback: (response) => {
  49. const paginate = response.data
  50. const results = response.data.results
  51. delete paginate.results
  52. return {
  53. list: new ClassroomList(results),
  54. paginate
  55. // {
  56. // "count": 1,
  57. // "num_pages": 1,
  58. // "per_page": 10,
  59. // "current": 1,
  60. // "next": null,
  61. // "previous": null,
  62. // }
  63. }
  64. },
  65. rejectCallback: (error) => {
  66. return error
  67. }
  68. })
  69. }
  70. exportActivitySheetReport(data) {
  71. return this.sendRequest({
  72. apiMethod: 'get',
  73. api: this.api,
  74. request: this.APIAdresses.exportActivitySheetReport,
  75. data: this.getNormalizedSendData({
  76. classroom: null, // Number
  77. type: null, // String
  78. status: null // String
  79. }, data),
  80. responseType: 'blob',
  81. resolveCallback: (response) => {
  82. return response.data // xlsxData
  83. },
  84. rejectCallback: (error) => {
  85. return error
  86. }
  87. })
  88. }
  89. get (id) {
  90. return this.sendRequest({
  91. apiMethod: 'get',
  92. api: this.api,
  93. request: this.APIAdresses.byId(id),
  94. resolveCallback: (response) => {
  95. return new Classroom(response.data)
  96. },
  97. rejectCallback: (error) => {
  98. return error
  99. }
  100. })
  101. }
  102. getClassroomAndCorrespondingRegistration (classroomId, ownerId) {
  103. return this.sendRequest({
  104. apiMethod: 'get',
  105. api: this.api,
  106. request: this.APIAdresses.getClassroomAndCorrespondingRegistration(classroomId, ownerId),
  107. resolveCallback: (response) => {
  108. return new Classroom(response.data)
  109. },
  110. rejectCallback: (error) => {
  111. return error
  112. }
  113. })
  114. }
  115. register (id) {
  116. return this.sendRequest({
  117. apiMethod: 'put',
  118. api: this.api,
  119. request: this.APIAdresses.register(id),
  120. resolveCallback: (response) => {
  121. const data = {
  122. type: null,
  123. model: response.data
  124. }
  125. if (response.data.status === 'PAYING') {
  126. data.type = 'Invoice'
  127. data.model = new Invoice(response.data)
  128. } else if (response.data.status === 'REGISTERED') {
  129. data.type = 'Registration'
  130. data.model = new Registration(response.data)
  131. }
  132. return data
  133. },
  134. rejectCallback: (error) => {
  135. return error
  136. }
  137. })
  138. }
  139. toggleOngoingStatus (id) {
  140. return this.sendRequest({
  141. apiMethod: 'put',
  142. api: this.api,
  143. request: this.APIAdresses.toggleOngoingStatus(id),
  144. resolveCallback: (response) => {
  145. return new Classroom(response.data)
  146. },
  147. rejectCallback: (error) => {
  148. return error
  149. }
  150. })
  151. }
  152. enrollByUser (id) {
  153. return this.sendRequest({
  154. apiMethod: 'put',
  155. api: this.api,
  156. request: this.APIAdresses.enroll(id),
  157. resolveCallback: (response) => {
  158. return new Classroom(response.data)
  159. },
  160. rejectCallback: (error) => {
  161. return error
  162. }
  163. })
  164. }
  165. dropByUser (id) {
  166. return this.sendRequest({
  167. apiMethod: 'put',
  168. api: this.api,
  169. request: this.APIAdresses.drop(id),
  170. resolveCallback: (response) => {
  171. return new Classroom(response.data)
  172. },
  173. rejectCallback: (error) => {
  174. return error
  175. }
  176. })
  177. }
  178. appendLeaders (data) {
  179. return this.sendRequest({
  180. apiMethod: 'put',
  181. api: this.api,
  182. request: this.APIAdresses.leaders(data.classroomId),
  183. data: {
  184. leaders: data.leaders // array of numbers
  185. },
  186. resolveCallback: (response) => {
  187. return new Classroom(response.data)
  188. },
  189. rejectCallback: (error) => {
  190. return error
  191. }
  192. })
  193. }
  194. dropByAdmin (classroomId, userId) {
  195. return this.sendRequest({
  196. apiMethod: 'put',
  197. api: this.api,
  198. request: this.APIAdresses.dropByAdmin(classroomId),
  199. data: {
  200. owner: userId
  201. },
  202. resolveCallback: (response) => {
  203. return new Classroom(response.data)
  204. },
  205. rejectCallback: (error) => {
  206. return error
  207. }
  208. })
  209. }
  210. createInvoice (id) {
  211. return this.sendRequest({
  212. apiMethod: 'put',
  213. api: this.api,
  214. request: this.APIAdresses.createInvoice(id),
  215. resolveCallback: (response) => {
  216. return new Invoice(response.data)
  217. },
  218. rejectCallback: (error) => {
  219. return error
  220. }
  221. })
  222. }
  223. appendGraders (data) {
  224. return this.sendRequest({
  225. apiMethod: 'put',
  226. api: this.api,
  227. request: this.APIAdresses.graders(data.classroomId),
  228. data: {
  229. classroomId: data.classroomId,
  230. graders: data.graders
  231. },
  232. resolveCallback: (response) => {
  233. return new ClassroomList(response.data)
  234. },
  235. rejectCallback: (error) => {
  236. return error
  237. }
  238. })
  239. }
  240. registerNewMembers (data) {
  241. return this.sendRequest({
  242. apiMethod: 'put',
  243. api: this.api,
  244. request: this.APIAdresses.bulkRegister(data.classroomId),
  245. data: {
  246. classroomId: data.classroomId,
  247. owners: data.owners
  248. },
  249. resolveCallback: (response) => {
  250. return new ClassroomList(response.data)
  251. },
  252. rejectCallback: (error) => {
  253. return error
  254. }
  255. })
  256. }
  257. enrollNewMembers (data) {
  258. return this.sendRequest({
  259. apiMethod: 'put',
  260. api: this.api,
  261. request: this.APIAdresses.bulkEnroll(data.classroomId),
  262. data: {
  263. classroomId: data.classroomId,
  264. owners: data.owners
  265. },
  266. resolveCallback: (response) => {
  267. return new ClassroomList(response.data)
  268. },
  269. rejectCallback: (error) => {
  270. return error
  271. }
  272. })
  273. }
  274. createInvoiceByAdmin (classroomId, userId) {
  275. return this.sendRequest({
  276. apiMethod: 'put',
  277. api: this.api,
  278. request: this.APIAdresses.createInvoice(classroomId),
  279. data: { owner: userId },
  280. resolveCallback: (response) => {
  281. return new Invoice(response.data)
  282. },
  283. rejectCallback: (error) => {
  284. return error
  285. }
  286. })
  287. }
  288. notifyAnnounceResult (classroomId) {
  289. return this.sendRequest({
  290. apiMethod: 'put',
  291. api: this.api,
  292. request: this.APIAdresses.announceResult(classroomId),
  293. resolveCallback: (response) => {
  294. return response.data
  295. },
  296. rejectCallback: (error) => {
  297. return error
  298. }
  299. })
  300. }
  301. }