Skip to content

Commit

Permalink
add units and language to user type
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaRip committed Sep 20, 2024
1 parent 127ed2b commit c46ce38
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
16 changes: 9 additions & 7 deletions app/server/src/server/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validateModel, Token, TokenSchema, UserSchema, User } from 'shared'
import { validateModel, Token, TokenSchema, UserSchema, User, LanguageSchema, UnitsSchema} from 'shared'
import { KnownError } from '../errors'
import { z } from 'zod'
import { Context } from 'koa'
Expand All @@ -16,10 +16,10 @@ const RegisterRequestSchema = z.object({
email: z.string(),
password: z.string(),
name: z.string(),
language: LanguageSchema,
units: UnitsSchema,
})

type RegisterRequest = z.infer<typeof RegisterRequestSchema>

export const UserController = {
async getUserByToken(ctx: Context): Promise<void> {
const token = TokenSchema.parse(ctx.state.token);
Expand Down Expand Up @@ -98,22 +98,24 @@ export const UserController = {
},

async register(ctx: Context): Promise<void> {
const result = validateModel(ctx.request.body, RegisterRequestSchema)
const bodyParseRes = RegisterRequestSchema.safeParse(ctx.request.body)

if (!result.ok) {
if (!bodyParseRes.success) {
throw new KnownError({
message: result.error.message,
message: bodyParseRes.error.message,
code: 400,
name: 'ValidationError',
});
}

const body = ctx.request.body as RegisterRequest
const body = bodyParseRes.data

await UserService.register(
body.email,
body.password,
body.name,
body.language,
body.units,
)

ctx.status = 201
Expand Down
1 change: 0 additions & 1 deletion app/server/src/server/repositories/user.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const UserRepo: IUserRepo = {
createdAt: doc.createdAt.toDate(),
}


const validatedUser = UserSchema.parse(createdUser);
return validatedUser;
},
Expand Down
18 changes: 13 additions & 5 deletions app/server/src/server/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid4 from 'uuid4';
import { KnownError } from '../errors';
import { Token, User, UserSchema, validateModel } from 'shared';
import { Token, User, UserSchema, validateModel, Language, Units } from 'shared';
import { UserRepo, TokenRepo } from '../repositories'
import bcrypt from 'bcrypt'

Expand Down Expand Up @@ -44,8 +44,6 @@ export const UserService = {
type: "access",
})

// TODO revoke user's previous tokens

return jwt
},

Expand All @@ -71,7 +69,13 @@ export const UserService = {
return validatedNewUser
},

async register(email: string, password: string, name: string): Promise<void> {
async register(
email: string,
password: string,
name: string,
language: Language,
units: Units,
): Promise<void> {
const existingUser = await UserRepo.findByEmail(email)

if (existingUser != null) {
Expand All @@ -84,16 +88,20 @@ export const UserService = {

const hashedPassword = bcrypt.hashSync(password, 10)

const user = await UserRepo.create({
const newUser = UserSchema.parse({
id: uuid4(),
email: email,
emailVerified: false,
password: hashedPassword,
name: name,
type: 'user',
createdAt: new Date(),
language: language,
units: units,
})

const user = await UserRepo.create({ ...newUser })

const result = validateModel(user, UserSchema)

if (!result.ok) throw new Error(
Expand Down
6 changes: 6 additions & 0 deletions app/shared/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export type { Token } from './token.model';
export { UserSchema } from './user.model';
export type { User } from './user.model';

export { LanguageSchema } from './user.model';
export type { Language } from './user.model';

export { UnitsSchema } from './user.model';
export type { Units } from './user.model';

export { default as validateModel } from './validateModel';

export { WellSchema } from './well.model';
Expand Down
8 changes: 8 additions & 0 deletions app/shared/src/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { z } from 'zod';

export const LanguageSchema = z.enum(['english', 'bengali']);
export type Language = z.infer<typeof LanguageSchema>;

export const UnitsSchema = z.enum(['meters', 'feet']);
export type Units = z.infer<typeof UnitsSchema>;

export const UserSchema = z.object({
id: z.string(),
email: z.string(),
Expand All @@ -8,6 +14,8 @@ export const UserSchema = z.object({
name: z.string(),
type: z.enum(['admin', 'user']),
createdAt: z.date(),
language: LanguageSchema,
units: UnitsSchema,
});

export type User = z.infer<typeof UserSchema>;

0 comments on commit c46ce38

Please sign in to comment.