-
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.
- Loading branch information
Showing
10 changed files
with
559 additions
and
0 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
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,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]); | ||
} | ||
} |
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,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; | ||
}, | ||
}); |
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,2 @@ | ||
export * from "./api.js"; | ||
export * from "./types.js"; |
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,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>; | ||
} |
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,2 @@ | ||
export * from "./User.js"; | ||
export * from "./types.js"; |
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,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; |
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 @@ | ||
export * from "./User/index.js"; |
Oops, something went wrong.