change login v1 to v2

This commit is contained in:
Peter Maquiran
2024-11-06 10:38:58 +01:00
parent 5c0bd094ca
commit 5c18f281bc
40 changed files with 271 additions and 246 deletions
+101 -20
View File
@@ -1,27 +1,108 @@
import { z } from "zod";
import { SHA1 } from 'crypto-js'
import { localstoreService } from "src/app/store/localstore.service";
// import { z } from "zod";
// Define the schema for the user object
const UserSchema = z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
role: z.string(),
roleId: z.number(),
userPhoto: z.string(),
adUserSID: z.string(),
// // Define the schema for the user object
// const UserSchema = z.object({
// wxUserId: z.number(),
// wxFullName: z.string(),
// wxeMail: z.string(),
// role: z.string(),
// roleId: z.number(),
// userPhoto: z.string(),
// adUserSID: z.string(),
// });
// // Define the schema for the main response object
// const UserDataSchema = z.object({
// user: UserSchema,
// authorization: z.string(),
// refreshToken: z.string(),
// permissions: z.array(z.number()),
// });
// export type IUser = z.infer<typeof UserDataSchema>
// export class UserEntity {
// wxUserId: number
// wxFullName: string
// wxeMail: string
// role: string
// roleId: number
// userPhoto: string
// adUserSID: string
// authorization: string
// refreshToken: string
// permissions: number[]
// Profile: 'PR' | 'MDGPR'| 'Consultant'| 'SGGPR'| 'Unknown'
// constructor(input: IUser) {
// Object.assign(this, input)
// if (input) {
// if (input?.user?.roleId == 100000014) {
// this.Profile = 'PR'
// } else if (input.user.roleId == 100000011) {
// this.Profile = 'MDGPR'
// } else if (input.user.roleId == 99999872) {
// this.Profile = 'Consultant'
// } else if (input.user.roleId == 99999886) {
// this.Profile = 'SGGPR'
// } else {
// this.Profile = 'Unknown'
// }
// }
// }
// }
import { z } from 'zod';
const LoginUserResponseSchema = z.object({
UserId: z.number(),
Authorization: z.string(),
Email: z.string().email(),
FullName: z.string(),
RoleDescription: z.string(),
RoleID: z.number(),
Profile: z.string(), // You can further define the Profile if you have more details
UserPermissions: z.array(z.number()), // Same as above, you can define more details if needed
});
// Define the schema for the main response object
const UserDataSchema = z.object({
user: UserSchema,
authorization: z.string(),
refreshToken: z.string(),
permissions: z.array(z.number()),
type LoginUserResponse = z.infer<typeof LoginUserResponseSchema>;
const CalendarInterfaceSchema = z.object({
CalendarId: z.string(),
CalendarName: z.enum(["Oficial", "Pessoal"]),
CalendarRoleId: z.string(),
Id: z.number(),
OwnerUserId: z.unknown(), // You can define more specifically based on your requirements
});
export type IUser = z.infer<typeof UserDataSchema>
type CalendarInterface = z.infer<typeof CalendarInterfaceSchema>;
const UserSessionSchema = z.object({
UserId: z.number(),
Authorization: z.string(),
Email: z.string().email(),
FullName: z.string(),
RoleDescription: z.string(),
RoleID: z.number(),
Password: z.string(),
RochetChatUserId: z.string(),
Profile: z.enum(['PR', 'MDGPR', 'Consultant', 'SGGPR', 'Unknown']),
LoginPreference: z.enum(['None', 'Password', 'Pin', null]).nullable(),
PIN: z.string(),
Inactivity: z.boolean(),
UrlBeforeInactivity: z.string(),
UserPermissions: z.unknown(), // Again, you can define it more explicitly if needed
UserPhoto: z.string(),
RefreshToken: z.string(),
});
type UserSession = z.infer<typeof UserSessionSchema>;
export class UserEntity {
@@ -37,7 +118,7 @@ export class UserEntity {
permissions: number[]
Profile: 'PR' | 'MDGPR'| 'Consultant'| 'SGGPR'| 'Unknown'
constructor(input: IUser) {
constructor(input: any) {
Object.assign(this, input)
if (input) {
+18
View File
@@ -0,0 +1,18 @@
import { UserLoginOutputResponse } from "../repository/user-remote-repository";
import { UserLoginOutput } from "../use-case/user-login-use-case.service";
export class UserLoginMapper{
static toDomainData(input: UserLoginOutputResponse): UserLoginOutput {
return {
RefreshToken: input.data.refreshToken,
Authorization: input.data.authorization,
Email: input.data.user.wxeMail,
FullName: input.data.user.wxeMail,
RoleDescription: input.data.user.role,
RoleID: input.data.user.roleId,
UserId: input.data.user.wxUserId,
UserPermissions: input.data.permissions,
Profile: ''
}
}
}
@@ -1,7 +1,7 @@
import { HttpErrorResponse } from "@angular/common/http";
import { Result } from "neverthrow";
import { HttpResult } from "src/app/infra/http/type";
import { UserLoginInput, UserLoginOutput } from "../use-case/user-login-use-case.service";
import { UserLoginInput } from "../use-case/user-login-use-case.service";
import { z } from "zod";
const UserRepositoryLoginParams = z.object({
@@ -9,8 +9,37 @@ const UserRepositoryLoginParams = z.object({
ChannelId: z.number()
})
export type IUserRepositoryLoginParams = z.infer<typeof UserRepositoryLoginParams>
// Define the schema for the user object
const UserSchema = z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
role: z.string(),
roleId: z.number(),
userPhoto: z.string(),
adUserSID: z.string(),
});
// Define the schema for the main response object
const UserLoginOutputSchema = z.object({
user: UserSchema,
authorization: z.string(),
refreshToken: z.string(),
permissions: z.array(z.number()),
});
// Define the main schema for the response
const LoginUserResponseSchema = z.object({
success: z.boolean(),
message: z.nullable(z.string()), // Message can be null
data: UserLoginOutputSchema,
});
export type UserLoginOutputResponse = z.infer<typeof LoginUserResponseSchema>
export type IUserRepositoryLoginParams = z.infer<typeof UserRepositoryLoginParams>
export abstract class IUserRemoteRepository {
abstract login(input: IUserRepositoryLoginParams): Promise<Result<HttpResult<UserLoginOutput>, HttpErrorResponse>>
abstract login(input: IUserRepositoryLoginParams): Promise<Result<HttpResult<UserLoginOutputResponse>, HttpErrorResponse>>
}
@@ -3,9 +3,11 @@ import { z } from 'zod';
import { IUserRemoteRepository } from '../repository/user-remote-repository';
import { XTracerAsync, TracingType } from 'src/app/services/monitoring/opentelemetry/tracer';
import { zodSafeValidation } from 'src/app/utils/zodValidation';
import { AlertController, Platform } from '@ionic/angular';
import { Platform } from '@ionic/angular';
import { AESEncrypt } from 'src/app/services/aesencrypt.service';
import { UserLoginMapper } from '../mapper/user-login';
import { UserSession } from 'src/app/models/user.model';
import { SessionStore } from 'src/app/store/session.service';
const UserLoginInputSchema = z.object({
username: z.string(),
@@ -16,34 +18,22 @@ export type UserLoginInput = z.infer<typeof UserLoginInputSchema>
// Define the schema for the user object
const UserSchema = z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
role: z.string(),
roleId: z.number(),
userPhoto: z.string(),
adUserSID: z.string(),
});
// Define the schema for the main response object
const UserLoginOutputSchema = z.object({
user: UserSchema,
authorization: z.string(),
refreshToken: z.string(),
permissions: z.array(z.number()),
});
// Define the main schema for the response
const LoginUserResponseSchema = z.object({
success: z.boolean(),
message: z.nullable(z.string()), // Message can be null
data: UserLoginOutputSchema,
UserId: z.number(),
Authorization: z.string(),
RefreshToken: z.string(),
Email: z.string().email(),
FullName: z.string(),
RoleDescription: z.string(),
RoleID: z.number(),
Profile: z.string(), // You can further define the Profile if you have more details
UserPermissions: z.array(z.number()), // Same as above, you can define more details if needed
});
export type UserLoginOutput = z.infer<typeof LoginUserResponseSchema>
export type UserLoginOutput = z.infer<typeof UserLoginOutputSchema>
@Injectable({
providedIn: 'root'
@@ -76,13 +66,28 @@ export class UserLoginUseCaseService {
ChannelId: channelId
})
if(result.isOk()) {
if(result.value) {
return result.map(e => {
const data = UserLoginMapper.toDomainData(e.data)
const session: UserSession = Object.assign(SessionStore.user, data)
if (session.RoleID == 100000014) {
session.Profile = 'PR'
} else if (session.RoleID == 100000011) {
session.Profile = 'MDGPR'
} else if (session.RoleID == 99999872) {
session.Profile = 'Consultant'
} else if (session.RoleID == 99999886) {
session.Profile = 'SGGPR'
} else {
session.Profile = 'Unknown'
}
}
SessionStore.reset(session)
return result.map(e => e.data.data)
return UserLoginMapper.toDomainData(e.data)
})
} else {
tracing.setAttribute('parameter error','true')