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 3, 2024
1 parent cddaa84 commit 7ac58bf
Show file tree
Hide file tree
Showing 10 changed files with 559 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/models/src/config/behaviors/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { apiServerBehaviors } from "../../server/Server/behaviors/index.js";
import { apiCustomerBehaviors } from "../../customer/Customer/behaviors/index.js";
import { apiIngressBehaviors } from "../../domain/Ingress/behaviors/index.js";
import { apiAppInstallationBehaviors } from "../../app/AppInstallation/behaviors/index.js";
import { apiUserBehaviors } from "../../user/User/behaviors/index.js";

class ApiSetupState {
private _client: MittwaldAPIV2Client | undefined;
Expand All @@ -22,6 +23,7 @@ class ApiSetupState {
config.behaviors.customer = apiCustomerBehaviors(client);
config.behaviors.ingress = apiIngressBehaviors(client);
config.behaviors.appInstallation = apiAppInstallationBehaviors(client);
config.behaviors.user = apiUserBehaviors(client);
}

public setupWithApiToken(apiToken: string) {
Expand Down
3 changes: 3 additions & 0 deletions packages/models/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ServerBehaviors } from "../server/Server/behaviors/index.js";
import { CustomerBehaviors } from "../customer/Customer/behaviors/index.js";
import { IngressBehaviors } from "../domain/Ingress/behaviors/index.js";
import { AppInstallationBehaviors } from "../app/AppInstallation/behaviors/index.js";
import { UserBehaviors } from "../user/User/behaviors/index.js";

interface Config {
behaviors: {
Expand All @@ -11,6 +12,7 @@ interface Config {
customer: CustomerBehaviors;
ingress: IngressBehaviors;
appInstallation: AppInstallationBehaviors;
user: UserBehaviors;
};
}

Expand All @@ -21,5 +23,6 @@ export const config: Config = {
customer: undefined as unknown as CustomerBehaviors,
ingress: undefined as unknown as IngressBehaviors,
appInstallation: undefined as unknown as AppInstallationBehaviors,
user: undefined as unknown as UserBehaviors,
},
};
102 changes: 102 additions & 0 deletions packages/models/src/user/User/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { ReferenceModel } from "../../base/ReferenceModel.js";
import {
type AsyncResourceVariant,
provideReact,
} from "../../lib/provideReact.js";
import { config } from "../../config/config.js";
import { DataModel } from "../../base/DataModel.js";
import { classes } from "polytype";
import {
UserAddPhoneNumberRequestData,
UserAuthenticateRequestData,
UserAuthenticateResponseData,
UserData,
UserUpdatePersonalInformationData,
UserVerifyPhoneNumberRequestData,
} from "./types.js";
import assertObjectFound from "../../base/assertObjectFound.js";

export class User extends ReferenceModel {
public static ofId(id: string): User {
return new User(id);
}

public static find = provideReact(
async (id: string): Promise<UserDetailed | undefined> => {
const data = await config.behaviors.user.find(id);

if (data !== undefined) {
return new UserDetailed(data);
}
},
);

public static get = provideReact(
async (id: string): Promise<UserDetailed> => {
const user = await this.find(id);

assertObjectFound(user, this, id);

return user;
},
);

public static getOwn = provideReact(async (): Promise<UserDetailed> => {
return await this.get("self");
});

public getDetailed = provideReact(() =>
User.get(this.id),
) as AsyncResourceVariant<UserDetailed, []>;

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

public async addPhoneNumber(
data: UserAddPhoneNumberRequestData,
): Promise<void> {
await config.behaviors.user.addPhoneNumber(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 requestAvatarUpload(): Promise<{ id: string }> {
return await config.behaviors.user.requestAvatarUpload(this.id);
}

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

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

class UserCommon extends classes(DataModel<UserData>, User) {
public readonly fullName: string;

public constructor(data: UserData) {
super([data], [data.userId]);
this.fullName = `${data.person.firstName} ${data.person.lastName}`;
}
}

export class UserDetailed extends classes(UserCommon, DataModel<UserData>) {
public constructor(data: UserData) {
super([data], [data]);
}
}
73 changes: 73 additions & 0 deletions packages/models/src/user/User/behaviors/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
assertStatus,
assertOneOfStatus,
MittwaldAPIV2Client,
} from "@mittwald/api-client";
import { UserBehaviors } from "./types.js";
import { UserAuthenticateRequestData } from "../types.js";

export const apiUserBehaviors = (
client: MittwaldAPIV2Client,
): UserBehaviors => ({
find: async (id) => {
const response = await client.user.getUser({ userId: id });

if (response.status === 200) {
return response.data;
}
assertOneOfStatus(response, [403, 404]);
},

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

assertStatus(response, 204);
},

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

assertStatus(response, 204);
},

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

assertStatus(response, 204);
},

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

// ToDo: 400 abfangen?

assertStatus(response, 204);
},

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

assertStatus(response, 200);

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

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

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;
},
});
2 changes: 2 additions & 0 deletions packages/models/src/user/User/behaviors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./api.js";
export * from "./types.js";
34 changes: 34 additions & 0 deletions packages/models/src/user/User/behaviors/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
UserAddPhoneNumberRequestData,
UserAuthenticateRequestData,
UserAuthenticateResponseData,
UserData,
UserUpdatePersonalInformationData,
UserVerifyPhoneNumberRequestData,
} from "../types.js";

export interface UserBehaviors {
find: (id: string) => Promise<UserData | undefined>;

updatePersonalInformation: (
id: string,
data: UserUpdatePersonalInformationData,
) => Promise<void>;

addPhoneNumber: (
id: string,
data: UserAddPhoneNumberRequestData,
) => Promise<void>;
removePhoneNumber: (id: string) => Promise<void>;
verifyPhoneNumber: (
id: string,
data: UserVerifyPhoneNumberRequestData,
) => Promise<void>;

requestAvatarUpload: (id: string) => Promise<{ id: string }>;
removeAvatar: (id: string) => Promise<void>;

authenticate: (
data: UserAuthenticateRequestData,
) => Promise<UserAuthenticateResponseData>;
}
2 changes: 2 additions & 0 deletions packages/models/src/user/User/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./User.js";
export * from "./types.js";
19 changes: 19 additions & 0 deletions packages/models/src/user/User/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MittwaldAPIV2 } from "@mittwald/api-client";

export type UserData = MittwaldAPIV2.Operations.UserGetUser.ResponseData;

export type UserUpdatePersonalInformationData =
MittwaldAPIV2.Paths.V2UsersSelfPersonalInformation.Put.Parameters.RequestBody;

export type UserAddPhoneNumberRequestData =
MittwaldAPIV2.Paths.V2UsersUserIdPhone.Post.Parameters.RequestBody;

export type UserVerifyPhoneNumberRequestData =
MittwaldAPIV2.Paths.V2UsersUserIdActionsVerifyPhone.Post.Parameters.RequestBody;

export type UserAuthenticateResponseData =
| MittwaldAPIV2.Paths.V2Authenticate.Post.Responses.$200.Content.ApplicationJson
| MittwaldAPIV2.Paths.V2Authenticate.Post.Responses.$202.Content.ApplicationJson;

export type UserAuthenticateRequestData =
MittwaldAPIV2.Paths.V2Authenticate.Post.Parameters.RequestBody;
1 change: 1 addition & 0 deletions packages/models/src/user/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./User/index.js";
Loading

0 comments on commit 7ac58bf

Please sign in to comment.