Skip to content

Commit

Permalink
fix: issue-3447-redundant-fields-in-session-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihar committed May 20, 2024
1 parent cb5e6bd commit e95b60d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api-gateway/src/api/service/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class AccountApi {
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
if (error.message.includes('already exists')) {
throw new HttpException('An account with the same name already exists.', HttpStatus.CONFLICT);
throw new HttpException(error.message, HttpStatus.CONFLICT);
}
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand Down
11 changes: 10 additions & 1 deletion auth-service/src/api/account-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
IUser,
UserRole
} from '@guardian/interfaces';
import { USER_REQUIRED_PROPS } from '../constants/index.js';

const { sign, verify } = pkg;

Expand Down Expand Up @@ -60,8 +61,16 @@ export class AccountService extends NatsService {
if (Date.now() > decryptedToken.expireAt) {
throw new Error('Token expired');
}

const user = await new DataBaseHelper(User).findOne({ username: decryptedToken.username });
return new MessageResponse(user);

const userRequiredProps = {}

for(const prop of Object.values(USER_REQUIRED_PROPS)) {
userRequiredProps[prop] = user[prop];
}

return new MessageResponse(userRequiredProps);
} catch (error) {
return new MessageError(error);
}
Expand Down
1 change: 1 addition & 0 deletions auth-service/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { REQUIRED_PROPS as USER_REQUIRED_PROPS } from './user.js';
10 changes: 10 additions & 0 deletions auth-service/src/constants/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const REQUIRED_PROPS = {
STATUS_CODE: 'status_code',
REASON: 'reason',
USER_NAME: 'username',
DID: 'did',
PARENT: 'parent',
HEDERA_ACCOUNT_ID: 'hederaAccountId',
ROLE: 'role',
ID: 'id',
};
3 changes: 0 additions & 3 deletions e2e-tests/cypress/e2e/api-tests/accounts/getSession.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ context('Accounts', { tags: '@accounts' }, () => {
expect(response.status).to.eq(STATUS_CODE.OK)
expect(response.body).to.have.property('id')
expect(response.body).to.have.property('username', 'StandardRegistry')
expect(response.body).to.have.property('password')
expect(response.body).to.have.property('did')
expect(response.body).to.have.property('walletToken')
expect(response.body).to.have.property('hederaAccountId')
expect(response.body).to.have.property('role')
})
Expand Down Expand Up @@ -50,7 +48,6 @@ context('Accounts', { tags: '@accounts' }, () => {
}).then((response) => {
expect(response.status).to.eq(200)
expect(response.body).to.have.property('id')
expect(response.body).to.have.property('password')
expect(response.body.role).eq('USER')
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ context("Accounts", { tags: "@accounts" }, () => {
},
failOnStatusCode:false,
}).then(response => {
expect(response.status).eql(STATUS_CODE.ERROR);
expect(response.status).eql(STATUS_CODE.CONFLICT);
expect(response.body.message).eql("An account with the same name already exists.");
});
});
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/cypress/support/api/api-const.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const STATUS_CODE = {
UNAUTHORIZED: 401,
FORBIDDEN: 403,
NOT_FOUND: 404,
CONFLICT: 409,
UNPROCESSABLE: 422,
ERROR: 500,
};

0 comments on commit e95b60d

Please sign in to comment.