Skip to content

Commit

Permalink
feat(User): add User model
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisa18289 committed May 6, 2024
1 parent 7ac58bf commit d309a35
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 356 deletions.
114 changes: 103 additions & 11 deletions packages/models/src/user/User/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ import { config } from "../../config/config.js";
import { DataModel } from "../../base/DataModel.js";
import { classes } from "polytype";
import {
UserAddPhoneNumberRequestData,
UserAuthenticateMfaRequestData,
UserAuthenticateMfaResponseData,
UserAuthenticateRequestData,
UserAuthenticateResponseData,
UserConfirmPasswordResetRequestData,
UserCreateAccessTokenRetrievalKeyResponseData,
UserData,
UserUpdatePersonalInformationData,
UserDeleteRequestData,
UserMfaStatusData,
UserRegisterRequestData,
UserRequestAvatarUploadResponseData,
UserResendVerificationEmailRequestData,
UserUpdatePasswordRequestData,
UserUpdatePasswordResponseData,
UserUpdatePersonalInformationRequestData,
UserVerifyEmailRequestData,
UserVerifyPhoneNumberRequestData,
UserVerifyRegistrationRequestData,
} from "./types.js";
import assertObjectFound from "../../base/assertObjectFound.js";

Expand Down Expand Up @@ -49,41 +61,121 @@ export class User extends ReferenceModel {
User.get(this.id),
) as AsyncResourceVariant<UserDetailed, []>;

public getPasswordUpdatedAt = provideReact(
async (): Promise<{ passwordUpdatedAt: string }> => {
return await config.behaviors.user.getPasswordUpdatedAt();
},
);

public getMfaStatus = provideReact(async (): Promise<UserMfaStatusData> => {
return await config.behaviors.user.getMfaStatus();
});

public async updatePersonalInformation(
data: UserUpdatePersonalInformationData,
data: UserUpdatePersonalInformationRequestData,
): Promise<void> {
await config.behaviors.user.updatePersonalInformation(this.id, data);
}

public async addPhoneNumber(
data: UserAddPhoneNumberRequestData,
public async addPhoneNumber(phoneNumber: string): Promise<void> {
await config.behaviors.user.addPhoneNumber(this.id, phoneNumber);
}

public async verifyPhoneNumber(
data: UserVerifyPhoneNumberRequestData,
): Promise<void> {
await config.behaviors.user.addPhoneNumber(this.id, data);
await config.behaviors.user.verifyPhoneNumber(this.id, data);
}

public async removePhoneNumber(): Promise<void> {
await config.behaviors.user.removePhoneNumber(this.id);
}

public async verifyPhoneNumber(
data: UserVerifyPhoneNumberRequestData,
): Promise<void> {
await config.behaviors.user.verifyPhoneNumber(this.id, data);
public async updateEmail(email: string): Promise<void> {
await config.behaviors.user.updateEmail(email);
}

public async requestAvatarUpload(): Promise<{ id: string }> {
public async verifyEmail(data: UserVerifyEmailRequestData): Promise<void> {
await config.behaviors.user.verifyEmail(data);
}

public async requestAvatarUpload(): Promise<UserRequestAvatarUploadResponseData> {
return await config.behaviors.user.requestAvatarUpload(this.id);
}

public async removeAvatar(): Promise<void> {
await config.behaviors.user.removeAvatar(this.id);
}

public async updatePassword(
data: UserUpdatePasswordRequestData,
): Promise<UserUpdatePasswordResponseData> {
return await config.behaviors.user.updatePassword(data);
}

public async resetPassword(email: string): Promise<void> {
await config.behaviors.user.resetPassword(email);
}

public async confirmPasswordReset(
data: UserConfirmPasswordResetRequestData,
): Promise<void> {
await config.behaviors.user.confirmPasswordReset(data);
}

public static async authenticate(
data: UserAuthenticateRequestData,
): Promise<UserAuthenticateResponseData> {
return await config.behaviors.user.authenticate(data);
}

public static async register(
data: UserRegisterRequestData,
): Promise<{ id: string }> {
return await config.behaviors.user.register(data);
}

public static async verifyRegistration(
data: UserVerifyRegistrationRequestData,
): Promise<void> {
return await config.behaviors.user.verifyRegistration(data);
}

public static async resendVerificationEmail(
data: UserResendVerificationEmailRequestData,
): Promise<void> {
return await config.behaviors.user.resendVerificationEmail(data);
}

public static async delete(data: UserDeleteRequestData): Promise<void> {
await config.behaviors.user.delete(data);
}

public async authenticateMfa(
data: UserAuthenticateMfaRequestData,
): Promise<UserAuthenticateMfaResponseData> {
return await config.behaviors.user.authenticateMfa(data);
}

public async confirmMfa(
multiFactorCode: string,
): Promise<{ recoveryCodesList: string[] }> {
return await config.behaviors.user.confirmMfa(multiFactorCode);
}

public async disableMfa(multiFactorCode: string): Promise<void> {
await config.behaviors.user.disableMfa(multiFactorCode);
}

public async resetRecoveryCodes(
multiFactorCode: string,
): Promise<{ recoveryCodesList: string[] }> {
return await config.behaviors.user.resetRecoveryCodes(multiFactorCode);
}

public async createAccessTokenRetrievalKey(): Promise<UserCreateAccessTokenRetrievalKeyResponseData> {
return await config.behaviors.user.createAccessTokenRetrievalKey();
}
}

class UserCommon extends classes(DataModel<UserData>, User) {
Expand Down
151 changes: 142 additions & 9 deletions packages/models/src/user/User/behaviors/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {
assertStatus,
assertOneOfStatus,
assertStatus,
MittwaldAPIV2Client,
} from "@mittwald/api-client";
import { UserBehaviors } from "./types.js";
import { UserAuthenticateRequestData } from "../types.js";
import {
UserAuthenticateMfaRequestData,
UserAuthenticateRequestData,
UserConfirmPasswordResetRequestData,
UserDeleteRequestData,
UserRegisterRequestData,
UserResendVerificationEmailRequestData,
UserVerifyRegistrationRequestData,
} from "../types.js";

export const apiUserBehaviors = (
client: MittwaldAPIV2Client,
Expand All @@ -18,6 +26,22 @@ export const apiUserBehaviors = (
assertOneOfStatus(response, [403, 404]);
},

getPasswordUpdatedAt: async () => {
const response = await client.user.getPasswordUpdatedAt({});

assertStatus(response, 200);

return response.data;
},

getMfaStatus: async () => {
const response = await client.user.getMfaStatus({});

assertStatus(response, 200);

return response.data;
},

updatePersonalInformation: async (id, data) => {
const response = await client.user.updatePersonalInformation({
userId: id,
Expand All @@ -27,8 +51,17 @@ export const apiUserBehaviors = (
assertStatus(response, 204);
},

addPhoneNumber: async (id, data) => {
const response = await client.user.addPhoneNumber({ userId: id, data });
addPhoneNumber: async (id, phoneNumber) => {
const response = await client.user.addPhoneNumber({
userId: id,
data: { phoneNumber },
});

assertStatus(response, 204);
},

verifyPhoneNumber: async (id, data) => {
const response = await client.user.verifyPhoneNumber({ userId: id, data });

assertStatus(response, 204);
},
Expand All @@ -39,10 +72,14 @@ export const apiUserBehaviors = (
assertStatus(response, 204);
},

verifyPhoneNumber: async (id, data) => {
const response = await client.user.verifyPhoneNumber({ userId: id, data });
updateEmail: async (email) => {
const response = await client.user.changeEmail({ data: { email } });

assertStatus(response, 204);
},

// ToDo: 400 abfangen?
verifyEmail: async (data) => {
const response = await client.user.verifyEmail({ data });

assertStatus(response, 204);
},
Expand All @@ -52,7 +89,7 @@ export const apiUserBehaviors = (

assertStatus(response, 200);

return { id: response.data.refId };
return response.data;
},

removeAvatar: async (id) => {
Expand All @@ -61,12 +98,108 @@ export const apiUserBehaviors = (
assertStatus(response, 204);
},

updatePassword: async (data) => {
const response = await client.user.changePassword({ data });

assertStatus(response, 200);

return response.data;
},

resetPassword: async (email) => {
const response = await client.user.initPasswordReset({ data: { email } });

assertStatus(response, 201);
},

confirmPasswordReset: async (data: UserConfirmPasswordResetRequestData) => {
const response = await client.user.confirmPasswordReset({ data });

assertStatus(response, 204);
},

authenticate: async (data: UserAuthenticateRequestData) => {
const response = await client.user.authenticate({ data });

assertOneOfStatus(response, [200, 202]);

// ToDo: 400/401 abfangen?
return response.data;
},

logout: async () => {
const response = await client.user.logout();

assertStatus(response, 204);
},

register: async (data: UserRegisterRequestData) => {
const response = await client.user.register({ data });

assertStatus(response, 201);

return { id: response.data.userId };
},

verifyRegistration: async (data: UserVerifyRegistrationRequestData) => {
const response = await client.user.verifyRegistration({ data });

assertStatus(response, 200);
},

resendVerificationEmail: async (
data: UserResendVerificationEmailRequestData,
) => {
const response = await client.user.resendVerificationEmail({ data });

assertStatus(response, 204);
},

delete: async (data: UserDeleteRequestData) => {
const response = await client.user.deleteUser({ data });

assertOneOfStatus(response, [200, 202]);
},

authenticateMfa: async (data: UserAuthenticateMfaRequestData) => {
const response = await client.user.authenticateMfa({ data });

assertStatus(response, 200);

return response.data;
},

confirmMfa: async (multiFactorCode: string) => {
const response = await client.user.confirmMfa({
data: { multiFactorCode },
});

assertStatus(response, 200);

return response.data;
},

disableMfa: async (multiFactorCode: string) => {
const response = await client.user.disableMfa({
data: { multiFactorCode },
});

assertStatus(response, 204);
},

resetRecoveryCodes: async (multiFactorCode: string) => {
const response = await client.user.resetRecoverycodes({
data: { multiFactorCode },
});

assertStatus(response, 200);

return response.data;
},

createAccessTokenRetrievalKey: async () => {
const response = await client.user.createAccessTokenRetrievalKey();

assertStatus(response, 201);

return response.data;
},
Expand Down
Loading

0 comments on commit d309a35

Please sign in to comment.