-
-
Notifications
You must be signed in to change notification settings - Fork 18
Backend refactoring ts #1686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backend refactoring ts #1686
Changes from all commits
59445f9
2b67618
666b8f2
fedd891
bf4994a
a7cd65f
07a4467
61015bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,15 +25,26 @@ export class AgentEntity { | |
| @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) | ||
| updatedAt: Date; | ||
|
|
||
| private _tokenChanged = false; | ||
|
|
||
| setToken(token: string): void { | ||
| this.token = token; | ||
| this._tokenChanged = true; | ||
| } | ||
|
|
||
| @BeforeInsert() | ||
| encryptToken(): void { | ||
| this.token = Encryptor.hashDataHMAC(this.token); | ||
| this._tokenChanged = false; | ||
| } | ||
|
|
||
| @BeforeUpdate() | ||
| updateTimestampAndEncryptToken(): void { | ||
| this.updatedAt = new Date(); | ||
| this.token = Encryptor.hashDataHMAC(this.token); | ||
| if (this._tokenChanged) { | ||
| this.token = Encryptor.hashDataHMAC(this.token); | ||
| this._tokenChanged = false; | ||
| } | ||
|
Comment on lines
+28
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Description: Verify all token assignments use setToken() method
# Expected: Only setToken() should be used for token assignment, not direct property access
rg -n '\.token\s*=' --type=ts -g '**/agent/**' -g '!*.entity.ts' -C2Repository: rocket-admin/rocketadmin Length of output: 588 Replace direct The Use - savedAgent.token = token;
+ savedAgent.setToken(token);Ensure all token assignments throughout the codebase use 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| @OneToOne( | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,36 +1,56 @@ | ||||||
| import Amplitude from '@amplitude/node'; | ||||||
| import { Injectable } from '@nestjs/common'; | ||||||
| import { Injectable, OnModuleInit } from '@nestjs/common'; | ||||||
| import { InjectRepository } from '@nestjs/typeorm'; | ||||||
| import { Repository } from 'typeorm'; | ||||||
| import { AmplitudeEventTypeEnum } from '../../enums/index.js'; | ||||||
| import { UserEntity } from '../user/user.entity.js'; | ||||||
|
|
||||||
| export interface AmplitudeLogOptions { | ||||||
| user_email?: string; | ||||||
| tablesCount?: number; | ||||||
| reason?: string; | ||||||
| message?: string; | ||||||
| operationCount?: number; | ||||||
| } | ||||||
|
|
||||||
| @Injectable() | ||||||
| export class AmplitudeService { | ||||||
| export class AmplitudeService implements OnModuleInit { | ||||||
| private client: ReturnType<typeof Amplitude.init>; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type annotation should reflect that The Proposed fix- private client: ReturnType<typeof Amplitude.init>;
+ private client: ReturnType<typeof Amplitude.init> | undefined;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| constructor( | ||||||
| @InjectRepository(UserEntity) | ||||||
| private readonly userRepository: Repository<UserEntity>, | ||||||
| ) {} | ||||||
|
|
||||||
| public async formAndSendLogRecord(event_type: AmplitudeEventTypeEnum, user_id: string, options = null) { | ||||||
| public onModuleInit(): void { | ||||||
| if (process.env.AMPLITUDE_API_KEY) { | ||||||
| this.client = Amplitude.init(process.env.AMPLITUDE_API_KEY); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public async formAndSendLogRecord( | ||||||
| event_type: AmplitudeEventTypeEnum, | ||||||
| user_id: string, | ||||||
| options?: AmplitudeLogOptions, | ||||||
| ): Promise<void> { | ||||||
| try { | ||||||
| if (process.env.NODE_ENV === 'test') return; | ||||||
| let user_email = (await this.userRepository.findOne({ where: { id: user_id } }))?.email; | ||||||
| if (!user_email && options) { | ||||||
| user_email = options?.user_email; | ||||||
| user_email = options.user_email; | ||||||
| } | ||||||
| let event_properties; | ||||||
| let event_properties: Record<string, unknown> | undefined; | ||||||
| if (user_email) { | ||||||
| event_properties = { | ||||||
| user_properties: { | ||||||
| email: user_email ? user_email : 'unknown', | ||||||
| tablesCount: options?.tablesCount ? options.tablesCount : undefined, | ||||||
| reason: options?.reason ? options?.reason : undefined, | ||||||
| message: options?.message ? options.message : undefined, | ||||||
| email: user_email ?? 'unknown', | ||||||
| tablesCount: options?.tablesCount, | ||||||
| reason: options?.reason, | ||||||
| message: options?.message, | ||||||
| }, | ||||||
| }; | ||||||
| } | ||||||
| if (options?.operationCount && options?.operationCount > 0) { | ||||||
| if (options?.operationCount && options.operationCount > 0) { | ||||||
| const promisesArr = Array.from(Array(options.operationCount), () => | ||||||
| this.sendLog(event_type, user_id, event_properties), | ||||||
| ); | ||||||
|
|
@@ -43,21 +63,19 @@ export class AmplitudeService { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| private async sendLog(eventType, cognitoUserName, eventProperties) { | ||||||
| const client = Amplitude.init(process.env.AMPLITUDE_API_KEY); | ||||||
| private async sendLog( | ||||||
| eventType: AmplitudeEventTypeEnum, | ||||||
| userId: string, | ||||||
| eventProperties?: Record<string, unknown>, | ||||||
| ): Promise<void> { | ||||||
| if (!this.client) return; | ||||||
| try { | ||||||
| client | ||||||
| .logEvent({ | ||||||
| event_type: eventType, | ||||||
| user_id: cognitoUserName, | ||||||
| event_properties: eventProperties ? eventProperties : undefined, | ||||||
| }) | ||||||
| .catch((e) => { | ||||||
| throw new Error(e); | ||||||
| }); | ||||||
| client.flush().catch((e) => { | ||||||
| throw new Error(e); | ||||||
| await this.client.logEvent({ | ||||||
| event_type: eventType, | ||||||
| user_id: userId, | ||||||
| event_properties: eventProperties, | ||||||
| }); | ||||||
| await this.client.flush(); | ||||||
| } catch (e) { | ||||||
| console.error(e); | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updateTimestampAndEncryptToken()now hashes only when_tokenChangedis set viasetToken(). Sincetokenremains a public field, any direct assignment likeagent.token = newTokenwill bypass hashing and can result in plaintext tokens being stored on update. Consider makingtokeneffectively write-protected (e.g.,private token+ getter) and/or tracking changes via@AfterLoad/previous-value comparison so hashing cannot be skipped.