Skip to content

Commit 2ea96f8

Browse files
authored
refactor: add binding information in list (#765)
1 parent 8b1dd07 commit 2ea96f8

File tree

8 files changed

+126
-9
lines changed

8 files changed

+126
-9
lines changed

src/v1/controller/user/binding/List.ts

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,75 @@ export class BindingList extends AbstractController<RequestType, ResponseType> {
2929
};
3030

3131
public async execute(): Promise<Response<ResponseType>> {
32-
const result = {} as Record<Lowercase<LoginPlatform>, boolean>;
32+
const result = { meta: {} } as ResponseType;
3333

34-
await Promise.all(
35-
Object.keys(this.svc).map(svc => {
36-
return this.svc[svc as LoginPlatform].exist().then(exist => {
37-
result[svc.toLowerCase() as Lowercase<LoginPlatform>] = exist;
38-
});
39-
}),
40-
);
34+
this.svc[LoginPlatform.WeChat].name();
35+
36+
await Promise.all([
37+
this.listWeChat(result),
38+
this.listPhone(result),
39+
this.listAgora(result),
40+
this.listApple(result),
41+
this.listGithub(result),
42+
this.listGoogle(result),
43+
this.listEmail(result),
44+
]);
4145

4246
return {
4347
status: Status.Success,
4448
data: result,
4549
};
4650
}
4751

52+
private async listWeChat(result: ResponseType): Promise<void> {
53+
const name = await this.svc[LoginPlatform.WeChat].name();
54+
result.wechat = name !== null;
55+
result.meta.wechat = name || "";
56+
}
57+
58+
private async listPhone(result: ResponseType): Promise<void> {
59+
const phone = await this.svc[LoginPlatform.Phone].phoneNumber();
60+
result.phone = phone !== null;
61+
result.meta.phone = phone || "";
62+
}
63+
64+
private async listAgora(result: ResponseType): Promise<void> {
65+
const agora = await this.svc[LoginPlatform.Agora].name();
66+
result.agora = agora !== null;
67+
result.meta.agora = agora || "";
68+
}
69+
70+
private async listApple(result: ResponseType): Promise<void> {
71+
const agora = await this.svc[LoginPlatform.Apple].name();
72+
result.agora = agora !== null;
73+
result.meta.agora = agora || "";
74+
}
75+
76+
private async listGithub(result: ResponseType): Promise<void> {
77+
const github = await this.svc[LoginPlatform.Github].name();
78+
result.github = github !== null;
79+
result.meta.github = github || "";
80+
}
81+
82+
private async listGoogle(result: ResponseType): Promise<void> {
83+
const google = await this.svc[LoginPlatform.Google].name();
84+
result.google = google !== null;
85+
result.meta.google = google || "";
86+
}
87+
88+
private async listEmail(result: ResponseType): Promise<void> {
89+
const email = await this.svc[LoginPlatform.Email].email();
90+
result.email = email !== null;
91+
result.meta.email = email || "";
92+
}
93+
4894
public errorHandler(error: Error): ResponseError {
4995
return this.autoHandlerError(error);
5096
}
5197
}
5298

5399
interface RequestType {}
54100

55-
type ResponseType = Record<Lowercase<LoginPlatform>, boolean>;
101+
type ResponseType = Record<Lowercase<LoginPlatform>, boolean> & {
102+
meta: Record<Lowercase<LoginPlatform>, string>;
103+
};

src/v1/service/user/UserAgora.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export class ServiceUserAgora {
4343
return !!result;
4444
}
4545

46+
public async name(): Promise<string | null> {
47+
const result = await UserAgoraDAO().findOne(["user_name"], {
48+
user_uuid: this.userUUID,
49+
});
50+
51+
return result ? result.user_name : null;
52+
}
53+
4654
public static async userUUIDByUnionUUID(unionUUID: string): Promise<string | null> {
4755
const result = await UserAgoraDAO().findOne(["user_uuid"], {
4856
union_uuid: String(unionUUID),

src/v1/service/user/UserApple.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export class ServiceUserApple {
4343
return !!result;
4444
}
4545

46+
public async name(): Promise<string | null> {
47+
const result = await UserAppleDAO().findOne(["user_name"], {
48+
user_uuid: this.userUUID,
49+
});
50+
51+
return result ? result.user_name : null;
52+
}
53+
4654
public static async userUUIDByUnionUUID(unionUUID: string): Promise<string | null> {
4755
const result = await UserAppleDAO().findOne(["user_uuid"], {
4856
union_uuid: String(unionUUID),

src/v1/service/user/UserEmail.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ export class ServiceUserEmail {
3737
return !!result;
3838
}
3939

40+
public async email(): Promise<string | null> {
41+
const result = await UserEmailDAO().findOne(["user_email"], {
42+
user_uuid: this.userUUID,
43+
});
44+
45+
return result ? this.desensitiveEmail(result.user_email) : null;
46+
}
47+
48+
private desensitiveEmail(email: string): string {
49+
const at = email.indexOf("@");
50+
if (at < 0) return email.slice(0, -3) + "***";
51+
52+
const name = email.slice(0, at);
53+
const suffix = email.slice(at);
54+
return name.slice(0, -3) + "***" + suffix;
55+
}
56+
4057
public async existEmail(email: string): Promise<boolean> {
4158
return await ServiceUserEmail.existEmail(email);
4259
}

src/v1/service/user/UserGithub.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export class ServiceUserGithub {
4343
return !!result;
4444
}
4545

46+
public async name(): Promise<string | null> {
47+
const result = await UserGithubDAO().findOne(["user_name"], {
48+
user_uuid: this.userUUID,
49+
});
50+
51+
return result ? result.user_name : null;
52+
}
53+
4654
public static async userUUIDByUnionUUID(unionUUID: string): Promise<string | null> {
4755
const result = await UserGithubDAO().findOne(["user_uuid"], {
4856
union_uuid: String(unionUUID),

src/v1/service/user/UserGoogle.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export class ServiceUserGoogle {
4343
return !!result;
4444
}
4545

46+
public async name(): Promise<string | null> {
47+
const result = await UserGoogleDAO().findOne(["user_name"], {
48+
user_uuid: this.userUUID,
49+
});
50+
51+
return result ? result.user_name : null;
52+
}
53+
4654
public static async userUUIDByUnionUUID(unionUUID: string): Promise<string | null> {
4755
const result = await UserGoogleDAO().findOne(["user_uuid"], {
4856
union_uuid: String(unionUUID),

src/v1/service/user/UserPhone.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export class ServiceUserPhone {
3939
return !!result;
4040
}
4141

42+
public async phoneNumber(): Promise<string | null> {
43+
const result = await UserPhoneDAO().findOne(["phone_number"], {
44+
user_uuid: this.userUUID,
45+
});
46+
47+
return result ? this.desensitivePhone(result.phone_number) : null;
48+
}
49+
50+
private desensitivePhone(phone: string): string {
51+
return phone.slice(0, -8) + "****" + phone.slice(-4);
52+
}
53+
4254
public async existPhone(phone: string): Promise<boolean> {
4355
return await ServiceUserPhone.existPhone(phone);
4456
}

src/v1/service/user/UserWeChat.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export class ServiceUserWeChat {
4545
return !!result;
4646
}
4747

48+
public async name(): Promise<string | null> {
49+
const result = await UserWeChatDAO().findOne(["user_name"], {
50+
user_uuid: this.userUUID,
51+
});
52+
53+
return result ? result.user_name : null;
54+
}
55+
4856
public static async userUUIDByUnionUUID(unionUUID: string): Promise<string | null> {
4957
const result = await UserWeChatDAO().findOne(["user_uuid"], {
5058
union_uuid: unionUUID,

0 commit comments

Comments
 (0)