|
@@ -5,6 +5,8 @@ import { UserLoginEventPayload } from '@box/common/events/user-login-event.dto';
|
|
|
import { RabbitmqPublisherService } from '../../rabbitmq/rabbitmq-publisher.service';
|
|
import { RabbitmqPublisherService } from '../../rabbitmq/rabbitmq-publisher.service';
|
|
|
import { AdService } from '../ads/ad.service';
|
|
import { AdService } from '../ads/ad.service';
|
|
|
import { AppJwtPayload } from './interfaces/app-jwt-payload';
|
|
import { AppJwtPayload } from './interfaces/app-jwt-payload';
|
|
|
|
|
+import { PrismaMongoStatsService } from '../../prisma/prisma-mongo-stats.service';
|
|
|
|
|
+import { PrismaMongoService } from '../../prisma/prisma-mongo.service';
|
|
|
|
|
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class AuthService {
|
|
export class AuthService {
|
|
@@ -13,6 +15,8 @@ export class AuthService {
|
|
|
private readonly jwtService: JwtService,
|
|
private readonly jwtService: JwtService,
|
|
|
private readonly rabbitmqPublisher: RabbitmqPublisherService,
|
|
private readonly rabbitmqPublisher: RabbitmqPublisherService,
|
|
|
private readonly adService: AdService,
|
|
private readonly adService: AdService,
|
|
|
|
|
+ private readonly prismaMongoStatsService: PrismaMongoStatsService,
|
|
|
|
|
+ private readonly prismaMongoService: PrismaMongoService,
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
async login(params: {
|
|
async login(params: {
|
|
@@ -35,6 +39,18 @@ export class AuthService {
|
|
|
|
|
|
|
|
const now = Date.now(); // number (ms since epoch)
|
|
const now = Date.now(); // number (ms since epoch)
|
|
|
|
|
|
|
|
|
|
+ const user = await this.getUserByUid(uid);
|
|
|
|
|
+ const firstChannel = await this.getChannelById(uChannelId || '');
|
|
|
|
|
+ // if user does not exist, uChannelId = firstChannel.channelId
|
|
|
|
|
+ let finalUChannelId = uChannelId;
|
|
|
|
|
+ if (!user && firstChannel) {
|
|
|
|
|
+ finalUChannelId = firstChannel.channelId;
|
|
|
|
|
+ }
|
|
|
|
|
+ // if user exists, take firstChannel.channelId
|
|
|
|
|
+ else if (user && firstChannel) {
|
|
|
|
|
+ finalUChannelId = firstChannel.channelId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Build JWT payload with required and optional tracking fields.
|
|
// Build JWT payload with required and optional tracking fields.
|
|
|
// Tracking fields (uChannelId, machine, ip, userAgent, appVersion, os) are optional
|
|
// Tracking fields (uChannelId, machine, ip, userAgent, appVersion, os) are optional
|
|
|
// to preserve backward compatibility with older tokens and minimize JWT size for stability.
|
|
// to preserve backward compatibility with older tokens and minimize JWT size for stability.
|
|
@@ -42,7 +58,7 @@ export class AuthService {
|
|
|
sub: uid,
|
|
sub: uid,
|
|
|
uid,
|
|
uid,
|
|
|
jti: tokenId,
|
|
jti: tokenId,
|
|
|
- uChannelId,
|
|
|
|
|
|
|
+ uChannelId: finalUChannelId,
|
|
|
machine,
|
|
machine,
|
|
|
ip,
|
|
ip,
|
|
|
userAgent,
|
|
userAgent,
|
|
@@ -60,7 +76,7 @@ export class AuthService {
|
|
|
userAgent,
|
|
userAgent,
|
|
|
appVersion,
|
|
appVersion,
|
|
|
os,
|
|
os,
|
|
|
- uChannelId,
|
|
|
|
|
|
|
+ uChannelId: finalUChannelId,
|
|
|
machine,
|
|
machine,
|
|
|
tokenId,
|
|
tokenId,
|
|
|
loginAt: now,
|
|
loginAt: now,
|
|
@@ -102,4 +118,45 @@ export class AuthService {
|
|
|
|
|
|
|
|
return { accessToken, allAds };
|
|
return { accessToken, allAds };
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // add a function to retrieve user record from mongo by uid
|
|
|
|
|
+ async getUserByUid(uid: string): Promise<any> {
|
|
|
|
|
+ // Implement your MongoDB query here to find the user by uid
|
|
|
|
|
+ // For example, if you have a UserModel injected, you might do:
|
|
|
|
|
+ // return this.userModel.findOne({ uid }).exec();
|
|
|
|
|
+ // Placeholder implementation:
|
|
|
|
|
+
|
|
|
|
|
+ const user = await this.prismaMongoStatsService.user.findUnique({
|
|
|
|
|
+ where: { uid },
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (user) {
|
|
|
|
|
+ return user;
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // retrive the first channel from mongo collection, order by createAt ascending
|
|
|
|
|
+ async getFirstChannel(): Promise<any> {
|
|
|
|
|
+ const channel = await this.prismaMongoService.channel.findFirst({
|
|
|
|
|
+ orderBy: { createAt: 'asc' },
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (channel) {
|
|
|
|
|
+ return channel;
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // find by channelId
|
|
|
|
|
+ async getChannelById(channelId: string): Promise<any> {
|
|
|
|
|
+ const channel = await this.prismaMongoService.channel.findUnique({
|
|
|
|
|
+ where: { channelId },
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (channel) {
|
|
|
|
|
+ return this.getFirstChannel();
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|