Skip to content

Commit

Permalink
fix(wallet_event): made changes to code based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
joonaun93 committed Oct 1, 2023
1 parent d7c6e42 commit 519d061
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ exports.up = function (db) {
notNull: true,
defaultValue: new String('now()'),
},
payload: { type: 'json', notNull: true },
});
};

Expand Down
36 changes: 36 additions & 0 deletions database/migrations/20230916050749-AlterWalletEventTypeEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var dbm;
var type;
var seed;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db) {
return db.runSql(
`ALTER TYPE wallet_event_type ADD VALUE 'login';
ALTER TYPE wallet_event_type ADD VALUE 'wallet_created';`,
);
};

exports.down = function (db) {
return db.runSql(`
CREATE TYPE wallet_event_type_original AS ENUM ('trust_request', 'trust_request_granted', 'trust_request_cancelled_by_originator', 'trust_request_cancelled_by_actor', 'trust_request_cancelled_by_target', 'transfer_requested', 'transfer_request_cancelled_by_source', 'transfer_request_cancelled_by_destination', 'transfer_request_cancelled_by_originator', 'transfer_pending_cancelled_by_source', 'transfer_pending_cancelled_by_destination', 'transfer_pending_cancelled_by_requestor', 'transfer_completed', 'transfer_failed');
ALTER TABLE wallet_event
ALTER COLUMN type TYPE wallet_event_type_original USING wallet_event_type::text::wallet_event_type_original;
DROP TYPE wallet_event_type;
ALTER TYPE wallet_event_type_original RENAME TO wallet_event_type;
`);
};

exports._meta = {
version: 1,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var dbm;
var type;
var seed;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db) {
return db.addColumn('wallet_event', 'payload', {
type: 'json',
notNull: true,
});
};

exports.down = function (db) {
return db.removeColumn('wallet_event', 'payload');
};

exports._meta = {
version: 1,
};
3 changes: 0 additions & 3 deletions server/handlers/transferHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const transferPost = async (req, res) => {
};

const transferIdAcceptPost = async (req, res) => {
// need to add to the events table
await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });

const transferService = new TransferService();
Expand All @@ -42,7 +41,6 @@ const transferIdAcceptPost = async (req, res) => {
};

const transferIdDeclinePost = async (req, res) => {
// need to add to the events table
await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });

const transferService = new TransferService();
Expand All @@ -55,7 +53,6 @@ const transferIdDeclinePost = async (req, res) => {
};

const transferIdDelete = async (req, res) => {
// need to add to the events table
await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });

const transferService = new TransferService();
Expand Down
19 changes: 7 additions & 12 deletions server/handlers/trustHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const trustGet = async (req, res) => {
};

const trustPost = async (req, res) => {
// need to add to the events table
await trustPostSchema.validateAsync(req.body, { abortEarly: false });

const trustService = new TrustService();
Expand All @@ -51,20 +50,18 @@ const trustRelationshipGetById = async (req, res) => {
abortEarly: false,
});

const { trustRelationshipId } = req.params
const trustService = new TrustService()
const { trustRelationshipId } = req.params;
const trustService = new TrustService();

const trustRelationship = await trustService.trustRelationshipGetById({
walletLoginId: req.wallet_id,
trustRelationshipId
})

res.status(200).json(trustRelationship)
}
trustRelationshipId,
});

res.status(200).json(trustRelationship);
};

const trustRelationshipAccept = async (req, res) => {
// need to add to the events table
await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});
Expand All @@ -79,7 +76,6 @@ const trustRelationshipAccept = async (req, res) => {
};

const trustRelationshipDecline = async (req, res) => {
// need to add to the events table
await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});
Expand All @@ -94,7 +90,6 @@ const trustRelationshipDecline = async (req, res) => {
};

const trustRelationshipDelete = async (req, res) => {
// need to add to the events table
await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});
Expand All @@ -114,5 +109,5 @@ module.exports = {
trustRelationshipAccept,
trustRelationshipDecline,
trustRelationshipDelete,
trustRelationshipGetById
trustRelationshipGetById,
};
1 change: 0 additions & 1 deletion server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const walletGetTrustRelationships = async (req, res) => {
};

const walletPost = async (req, res) => {
// need to add to the events table
await walletPostSchema.validateAsync(req.body, { abortEarly: false });

const walletService = new WalletService();
Expand Down
9 changes: 5 additions & 4 deletions server/models/Event.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const log = require('loglevel');
// const Joi = require('joi');
const EventRepository = require('../repositories/EventRepository');
// const WalletService = require('../services/WalletService');
// const HttpError = require('../utils/HttpError');

class Event {
constructor(session) {
this._eventRepository = new EventRepository(session);
// this._walletService = new WalletService();
}

async getAllEvents(walletId, limit, since) {
Expand All @@ -31,10 +33,9 @@ class Event {
});
}

async logEvent({ loggedInWalletId, type, payload }) {
log.info(loggedInWalletId);
async logEvent({ wallet_id, type, payload }) {
const event = await this._eventRepository.create({
wallet_id: loggedInWalletId,
wallet_id,
type,
payload,
});
Expand Down
2 changes: 0 additions & 2 deletions server/models/Transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ class Transfer {
claim: claimBoolean,
});

log.info(transfer);
log.info('here');
log.debug('now, deal with tokens');
await this._token.completeTransfer(tokens, transfer, claimBoolean);
return this.constructor.removeWalletIds(transfer);
Expand Down
2 changes: 0 additions & 2 deletions server/repositories/EventRepository.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const log = require('loglevel');
const BaseRepository = require('./BaseRepository');

class EventRepository extends BaseRepository {
Expand All @@ -9,7 +8,6 @@ class EventRepository extends BaseRepository {
}

async getAllEvents(filter, limitOptions) {
log.info(filter);
let promise = this._session
.getDB()
.select('wallet_event.*')
Expand Down
13 changes: 6 additions & 7 deletions server/services/AuthService.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const WalletService = require('./WalletService');
const Event = require('../models/Event');
const Session = require('../infra/database/Session');
const EventService = require('./EventService');
const JWTService = require('./JWTService');
const HashService = require('./HashService');
const EventEnums = require('../utils/event-enum');

class AuthService {
static async signIn({ wallet, password }) {
const session = new Session();
const event = new Event(session);
const eventService = new EventService();
const walletService = new WalletService();
const walletObject = await walletService.getByIdOrName(wallet);

Expand All @@ -16,9 +15,9 @@ class AuthService {
if (hash === walletObject.password) {
const token = JWTService.sign(walletObject);

await event.logEvent({
loggedInWalletId: walletObject.id,
type: 'login',
await eventService.logEvent({
wallet_id: walletObject.id,
type: EventEnums.AUTH.login,
payload: {},
});

Expand Down
2 changes: 1 addition & 1 deletion server/services/AuthService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('AuthService', () => {
expect(token).eql('token');
expect(
logEventStub.calledOnceWithExactly({
loggedInWalletId: 'id',
wallet_id: 'id',
type: 'login',
payload: {},
}),
Expand Down
22 changes: 12 additions & 10 deletions server/services/EventService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const Event = require('../models/Event');
const WalletService = require('./WalletService');
const Session = require('../infra/database/Session');
const HttpError = require('../utils/HttpError');

class EventService {
constructor() {
Expand All @@ -14,17 +13,10 @@ class EventService {
let events = [];

if (wallet) {
const walletInstance = await this._walletService.getByName(wallet);
const isSub = await this._walletService.hasControlOver(
const walletInstance = await this._walletService.hasControlOverByName(
walletLoginId,
walletInstance.id,
wallet,
);
if (!isSub) {
throw new HttpError(
403,
'Wallet does not belong to the logged in wallet',
);
}

events = await this._event.getAllEvents(walletInstance.id, limit, since);
} else {
Expand All @@ -33,6 +25,16 @@ class EventService {

return events;
}

async logEvent({ wallet_id, type, payload }) {
const event = await this._event.logEvent({
wallet_id,
type,
payload,
});

return event;
}
}

module.exports = EventService;
Loading

0 comments on commit 519d061

Please sign in to comment.