Skip to content

Commit 8a1bc29

Browse files
committed
update CORS methods and remove unused error class; enhance key enrollment logic
1 parent d729ef8 commit 8a1bc29

5 files changed

Lines changed: 35 additions & 38 deletions

File tree

packages/apps/human-app/server/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function bootstrap() {
1919
if (envConfigService.isCorsEnabled) {
2020
app.enableCors({
2121
origin: envConfigService.corsEnabledOrigin,
22-
methods: ['GET', 'POST', 'OPTIONS', 'PUT'],
22+
methods: ['GET', 'POST', 'OPTIONS', 'PUT', 'DELETE'],
2323
allowedHeaders: envConfigService.corsAllowedHeaders,
2424
});
2525
}

packages/apps/reputation-oracle/server/src/modules/exchange-api-keys/exchange-api-keys.error-filter.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ import {
1313
ExchangeApiKeyNotFoundError,
1414
IncompleteKeySuppliedError,
1515
KeyAuthorizationError,
16-
ActiveExchangeApiKeyExistsError,
1716
} from './exchange-api-keys.errors';
1817
import { ExchangeApiClientError } from '../exchange/errors';
1918

2019
@Catch(
2120
UserNotFoundError,
2221
IncompleteKeySuppliedError,
2322
KeyAuthorizationError,
24-
ActiveExchangeApiKeyExistsError,
2523
ExchangeApiKeyNotFoundError,
2624
)
2725
export class ExchangeApiKeysControllerErrorsFilter implements ExceptionFilter {
@@ -38,8 +36,7 @@ export class ExchangeApiKeysControllerErrorsFilter implements ExceptionFilter {
3836
if (
3937
exception instanceof UserNotFoundError ||
4038
exception instanceof IncompleteKeySuppliedError ||
41-
exception instanceof KeyAuthorizationError ||
42-
exception instanceof ActiveExchangeApiKeyExistsError
39+
exception instanceof KeyAuthorizationError
4340
) {
4441
status = HttpStatus.UNPROCESSABLE_ENTITY;
4542
} else if (exception instanceof ExchangeApiClientError) {

packages/apps/reputation-oracle/server/src/modules/exchange-api-keys/exchange-api-keys.errors.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,3 @@ export class KeyAuthorizationError extends BaseError {
1717
super("Provided API key can't be authorized on exchange");
1818
}
1919
}
20-
21-
export class ActiveExchangeApiKeyExistsError extends BaseError {
22-
constructor(
23-
readonly userId: number,
24-
readonly exchangeName: string,
25-
) {
26-
super('User already has an active exchange API key');
27-
}
28-
}

packages/apps/reputation-oracle/server/src/modules/exchange-api-keys/exchange-api-keys.service.spec.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import { ExchangeApiKeysService } from './exchange-api-keys.service';
1212
import { UserEntity, UserNotFoundError, UserRepository } from '@/modules/user';
1313

1414
import { ExchangeApiKeyEntity } from './exchange-api-key.entity';
15-
import {
16-
ActiveExchangeApiKeyExistsError,
17-
KeyAuthorizationError,
18-
} from './exchange-api-keys.errors';
15+
import { KeyAuthorizationError } from './exchange-api-keys.errors';
1916
import { ExchangeApiKeysRepository } from './exchange-api-keys.repository';
2017
import {
2118
generateExchangeApiKey,
@@ -104,20 +101,31 @@ describe('ExchangeApiKeysService', () => {
104101
expect(thrownError.exchangeName).toBe(input.exchangeName);
105102
});
106103

107-
it('should throw if user already has active keys', async () => {
104+
it('should overwrite existing keys if user already has active ones', async () => {
108105
const input = generateExchangeApiKeysData();
106+
const existingKey = generateExchangeApiKey();
109107
mockExchangeApiKeysRepository.findOneByUserId.mockResolvedValueOnce(
110-
generateExchangeApiKey(),
108+
existingKey,
109+
);
110+
mockExchangeClient.checkRequiredAccess.mockResolvedValueOnce(true);
111+
mockUserRepository.findOneById.mockResolvedValueOnce({
112+
id: input.userId,
113+
} as UserEntity);
114+
mockExchangeApiKeysRepository.updateOne.mockImplementation(
115+
async (entity) => entity,
111116
);
112117

113-
let thrownError;
114-
try {
115-
await exchangeApiKeysService.enroll(input);
116-
} catch (error) {
117-
thrownError = error;
118-
}
118+
const updatedEntity = await exchangeApiKeysService.enroll(input);
119119

120-
expect(thrownError).toBeInstanceOf(ActiveExchangeApiKeyExistsError);
120+
expect(mockExchangeApiKeysRepository.updateOne).toHaveBeenCalledWith(
121+
existingKey,
122+
);
123+
const [decryptedApiKey, decryptedSecretKey] = await Promise.all([
124+
aesEncryptionService.decrypt(updatedEntity.apiKey),
125+
aesEncryptionService.decrypt(updatedEntity.secretKey),
126+
]);
127+
expect(decryptedApiKey.toString()).toBe(input.apiKey);
128+
expect(decryptedSecretKey.toString()).toBe(input.secretKey);
121129
});
122130

123131
it('should throw if user not exists', async () => {

packages/apps/reputation-oracle/server/src/modules/exchange-api-keys/exchange-api-keys.service.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import { ExchangeClientFactory } from '@/modules/exchange/exchange-client.factor
1010
import { UserNotFoundError, UserRepository } from '@/modules/user';
1111

1212
import { ExchangeApiKeyEntity } from './exchange-api-key.entity';
13-
import {
14-
ActiveExchangeApiKeyExistsError,
15-
KeyAuthorizationError,
16-
} from './exchange-api-keys.errors';
13+
import { KeyAuthorizationError } from './exchange-api-keys.errors';
1714
import { ExchangeApiKeysRepository } from './exchange-api-keys.repository';
1815

1916
@Injectable()
@@ -39,9 +36,6 @@ export class ExchangeApiKeysService {
3936

4037
const currentKeys =
4138
await this.exchangeApiKeysRepository.findOneByUserId(userId);
42-
if (currentKeys) {
43-
throw new ActiveExchangeApiKeyExistsError(userId, exchangeName);
44-
}
4539

4640
const client = await this.exchangeClientFactory.create(exchangeName, {
4741
apiKey,
@@ -57,14 +51,21 @@ export class ExchangeApiKeysService {
5751
throw new UserNotFoundError(userId);
5852
}
5953

60-
const enrolledKey = new ExchangeApiKeyEntity();
61-
enrolledKey.userId = userId;
62-
enrolledKey.exchangeName = exchangeName;
63-
6454
const [encryptedApiKey, encryptedSecretKey] = await Promise.all([
6555
this.aesEncryptionService.encrypt(Buffer.from(apiKey)),
6656
this.aesEncryptionService.encrypt(Buffer.from(secretKey)),
6757
]);
58+
if (currentKeys) {
59+
currentKeys.exchangeName = exchangeName;
60+
currentKeys.apiKey = encryptedApiKey;
61+
currentKeys.secretKey = encryptedSecretKey;
62+
63+
return this.exchangeApiKeysRepository.updateOne(currentKeys);
64+
}
65+
66+
const enrolledKey = new ExchangeApiKeyEntity();
67+
enrolledKey.userId = userId;
68+
enrolledKey.exchangeName = exchangeName;
6869
enrolledKey.apiKey = encryptedApiKey;
6970
enrolledKey.secretKey = encryptedSecretKey;
7071
await this.exchangeApiKeysRepository.createUnique(enrolledKey);

0 commit comments

Comments
 (0)