|
| 1 | +import { AppRegistry } from ".."; |
| 2 | +import { Mock } from "./generic"; |
| 3 | +import { BaseUsers } from "./user"; |
| 4 | +import { readableVideo } from "./video"; |
| 5 | +import { faker } from "@faker-js/faker"; |
| 6 | +import { Factory, Model, Server } from "miragejs"; |
| 7 | +import { FactoryDefinition, ModelDefinition } from "miragejs/-types"; |
| 8 | + |
| 9 | +export interface Attachment { |
| 10 | + id: string; |
| 11 | + title: string; |
| 12 | + url: string; |
| 13 | + description?: string; |
| 14 | + userId: string; |
| 15 | + status: AttachmentStatus; |
| 16 | + type: AttachmentType; |
| 17 | + videos: string[]; |
| 18 | + modules: string[]; |
| 19 | +} |
| 20 | + |
| 21 | +export class AttachmentMock implements Mock { |
| 22 | + name(): string { |
| 23 | + return "attachment"; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Converts page and pageSize to start and end, to use with `slice` method |
| 28 | + */ |
| 29 | + getPagination = (queryParams: Record<string, string>) => { |
| 30 | + const page = parseInt(queryParams.page); |
| 31 | + const pageSize = parseInt(queryParams.pageSize); |
| 32 | + const start = (page - 1) * pageSize; |
| 33 | + const end = start + pageSize; |
| 34 | + return { start, end }; |
| 35 | + }; |
| 36 | + |
| 37 | + routes(server: Server<AppRegistry>): void { |
| 38 | + server.get("attachments/", (schema, { queryParams }) => { |
| 39 | + const { start, end } = this.getPagination(queryParams); |
| 40 | + const models = schema.all("attachment"); |
| 41 | + return { |
| 42 | + items: models.slice(start, end).models, |
| 43 | + totalCount: models.length, |
| 44 | + }; |
| 45 | + }); |
| 46 | + |
| 47 | + server.get("attachments/video/:videoId", (schema) => { |
| 48 | + const { models } = schema.all("attachment"); |
| 49 | + return { |
| 50 | + items: models, |
| 51 | + totalCount: models.length, |
| 52 | + }; |
| 53 | + }); |
| 54 | + |
| 55 | + server.get("attachments/:id", (schema, { params }) => { |
| 56 | + return schema.find("attachment", params.id); |
| 57 | + }); |
| 58 | + |
| 59 | + server.post("attachments", (schema, request) => { |
| 60 | + return schema.create("attachment", JSON.parse(request.requestBody)); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + factory(): FactoryDefinition<{}> { |
| 65 | + return Factory.extend({ |
| 66 | + id() { |
| 67 | + return faker.datatype.uuid(); |
| 68 | + }, |
| 69 | + title() { |
| 70 | + return faker.internet.domainWord(); |
| 71 | + }, |
| 72 | + description() { |
| 73 | + return faker.datatype.string(); |
| 74 | + }, |
| 75 | + url() { |
| 76 | + return faker.internet.url(); |
| 77 | + }, |
| 78 | + userId() { |
| 79 | + return BaseUsers[0].id; |
| 80 | + }, |
| 81 | + status() { |
| 82 | + return AttachmentStatus.Completed; |
| 83 | + }, |
| 84 | + videos() { |
| 85 | + return []; |
| 86 | + }, |
| 87 | + modules() { |
| 88 | + return []; |
| 89 | + }, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + seeds(server: Server<AppRegistry>): void { |
| 94 | + server.createList(this.name(), 20); |
| 95 | + } |
| 96 | + |
| 97 | + model(): ModelDefinition<Attachment> { |
| 98 | + return Model.extend({}); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export enum AttachmentStatus { |
| 103 | + InProgress = "IN_PROGRESS", |
| 104 | + Completed = "COMPLETED", |
| 105 | +} |
| 106 | + |
| 107 | +export enum AttachmentType { |
| 108 | + External = "EXTERNAL", |
| 109 | + Internal = "INTERNAL", |
| 110 | +} |
| 111 | + |
| 112 | +export const mockAttachment: Attachment = { |
| 113 | + id: "139c2ab0-4e66-48ff-9149-d66782432bfa", |
| 114 | + title: "Google", |
| 115 | + url: "https://google.com", |
| 116 | + videos: [readableVideo.id], |
| 117 | + modules: [], |
| 118 | + type: AttachmentType.External, |
| 119 | + status: AttachmentStatus.Completed, |
| 120 | + userId: BaseUsers[0].id, |
| 121 | +}; |
0 commit comments