feat(dakao): transfer existing users to new KakaoTalk IDs
This commit is contained in:
1 parent
2bac89d3dc
commit
71085d0ac4
10 files changed
+432
-1
No files matched your search
@@ -60,6 +60,14 @@ interface ResolvedRiotIdentity {
|
||||
const DEFAULT_RIOT_PLATFORM: RiotPlatformRoute = 'kr';
|
||||
const DEFAULT_RIOT_REGION: RiotRegionalRoute = 'asia';
|
||||
|
||||
export type KakaoDataTransferResult =
|
||||
| 'transferred'
|
||||
| 'already-linked'
|
||||
| 'not-found'
|
||||
| 'ambiguous'
|
||||
| 'id-conflict'
|
||||
| 'changed';
|
||||
|
||||
@Injectable()
|
||||
export class DakaoUserService {
|
||||
private readonly logger = new Logger(DakaoUserService.name);
|
||||
@@ -127,6 +135,44 @@ export class DakaoUserService {
|
||||
}).exec();
|
||||
}
|
||||
|
||||
public async transferKakaoTalkId(params: {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
kakaoTalkId: string;
|
||||
previousNickname: string;
|
||||
}): Promise<KakaoDataTransferResult> {
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
const targetFilter = {
|
||||
communityId: params.communityId,
|
||||
kakaoTalkNickname: params.previousNickname,
|
||||
'riotAccounts.0': { $exists: true },
|
||||
};
|
||||
const candidates = await UserModel.find(targetFilter).limit(2).exec();
|
||||
if (candidates.length === 0) return 'not-found';
|
||||
if (candidates.length > 1) return 'ambiguous';
|
||||
|
||||
const user = candidates[0];
|
||||
if (user.kakaoTalkId === params.kakaoTalkId) return 'already-linked';
|
||||
const occupied = await this.findUserByKakaoTalkId(params);
|
||||
if (occupied) return 'id-conflict';
|
||||
|
||||
try {
|
||||
// Compare the previous ID so concurrent transfers cannot overwrite one another.
|
||||
const updated = await UserModel.findOneAndUpdate(
|
||||
{
|
||||
...targetFilter,
|
||||
_id: user._id,
|
||||
kakaoTalkId: user.kakaoTalkId ?? null,
|
||||
},
|
||||
{ $set: { kakaoTalkId: params.kakaoTalkId } },
|
||||
{ returnDocument: 'after' },
|
||||
).exec();
|
||||
return updated ? 'transferred' : 'changed';
|
||||
} catch (error) {
|
||||
if ((error as { code?: number })?.code === 11000) return 'id-conflict';
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async resolveRiotAccount(
|
||||
context: ResolveContext,
|
||||
): Promise<ResolveResult> {
|
||||
|
||||
Reference in new issue
Block a user