-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add more proper types to events service
- Loading branch information
Showing
19 changed files
with
168 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Client/reasn-client/packages/common/__tests__/schemas/EventDto.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Client/reasn-client/packages/common/__tests__/schemas/ImageDto.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Client/reasn-client/packages/common/__tests__/schemas/UserDto.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Client/reasn-client/packages/common/src/schemas/EventRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { z } from "zod"; | ||
import { AddressDtoSchema } from "./AddressDto"; | ||
import { TagDtoSchema } from "./TagDto"; | ||
import { ParameterDtoSchema } from "./ParameterDto"; | ||
|
||
export const EventRequestSchema = z | ||
.object({ | ||
name: z.string().max(64), | ||
address: AddressDtoSchema, | ||
description: z.string().max(4048), | ||
startAt: z | ||
.string() | ||
.datetime({ offset: true }) | ||
.or(z.date()) | ||
.transform((arg) => new Date(arg)), | ||
endAt: z | ||
.string() | ||
.datetime({ offset: true }) | ||
.or(z.date()) | ||
.transform((arg) => new Date(arg)), | ||
tags: z.array(TagDtoSchema).nullable(), | ||
parameters: z.array(ParameterDtoSchema).nullable(), | ||
}) | ||
.refine((schema) => schema.startAt < schema.endAt); | ||
|
||
export type EventRequest = z.infer<typeof EventRequestSchema>; |
79 changes: 79 additions & 0 deletions
79
Client/reasn-client/packages/common/src/schemas/EventResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { z } from "zod"; | ||
import { AddressDtoSchema } from "@reasn/common/src/schemas/AddressDto"; | ||
import ModelMappingError from "@reasn/common/src/errors/ModelMappingError"; | ||
import { TagDtoSchema } from "@reasn/common/src/schemas/TagDto"; | ||
import { ParameterDtoSchema } from "@reasn/common/src/schemas/ParameterDto"; | ||
import { EventStatus } from "@reasn/common/src/enums/schemasEnums"; | ||
|
||
export const EventRespsoneSchema = z.object({ | ||
name: z.string().max(64), | ||
addressId: z.number(), | ||
address: AddressDtoSchema, | ||
description: z.string().max(4048), | ||
organizer: z.object({ | ||
username: z.string(), | ||
image: z.string(), | ||
}), | ||
startAt: z | ||
.string() | ||
.datetime({ offset: true }) | ||
.or(z.date()) | ||
.transform((arg) => new Date(arg)), | ||
endAt: z | ||
.string() | ||
.datetime({ offset: true }) | ||
.or(z.date()) | ||
.transform((arg) => new Date(arg)), | ||
createdAt: z | ||
.string() | ||
.datetime({ offset: true }) | ||
.or(z.date()) | ||
.transform((arg) => new Date(arg)), | ||
updatedAt: z | ||
.string() | ||
.datetime({ offset: true }) | ||
.or(z.date()) | ||
.transform((arg) => new Date(arg)), | ||
slug: z | ||
.string() | ||
.max(128) | ||
.regex(/^[\p{L}\d]+[\p{L}\d-]*$/u) | ||
.nullable(), | ||
status: z.nativeEnum(EventStatus), | ||
tags: z.array(TagDtoSchema), | ||
parameters: z.array(ParameterDtoSchema), | ||
participants: z.object({ | ||
interested: z.number(), | ||
participating: z.number(), | ||
}), | ||
}); | ||
|
||
export type EventResponse = z.infer<typeof EventRespsoneSchema>; | ||
|
||
export const EventResponseMapper = { | ||
fromObject: (entity: object): EventResponse => { | ||
const result = EventRespsoneSchema.safeParse(entity); | ||
if (!result.success) { | ||
throw new ModelMappingError( | ||
"EventResponse", | ||
result.error.message, | ||
result.error, | ||
); | ||
} | ||
return result.data; | ||
}, | ||
fromJSON: (jsonEntity: string): any => { | ||
if (!jsonEntity) { | ||
throw new ModelMappingError("EventResponse", "Empty JSON string"); | ||
} | ||
const result = EventRespsoneSchema.safeParse(JSON.parse(jsonEntity)); | ||
if (!result.success) { | ||
throw new ModelMappingError( | ||
"EventResponse", | ||
result.error.message, | ||
result.error, | ||
); | ||
} | ||
return result.data; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Client/reasn-client/packages/common/src/schemas/ParticipantDto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Client/reasn-client/packages/common/src/services/authorizationServices.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.