Skip to content

Commit

Permalink
Merge pull request #382 from joonaun93/master
Browse files Browse the repository at this point in the history
#294 Implement missing end point (Wallet event list)
  • Loading branch information
Kpoke authored Oct 29, 2023
2 parents 0247955 + 66e6ff6 commit fe9733b
Show file tree
Hide file tree
Showing 28 changed files with 1,446 additions and 240 deletions.
22 changes: 11 additions & 11 deletions database/migrations/20200901213252-CreateEnumWalletEventType.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@


let dbm;
let type;
let 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) {
* 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("CREATE TYPE wallet_event_type 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')");
exports.up = function (db) {
return db.runSql(
"CREATE TYPE wallet_event_type 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')",
);
};

exports.down = function(db) {
return db.runSql("DROP TYPE wallet_event_type");
exports.down = function (db) {
return db.runSql('DROP TYPE wallet_event_type');
};

exports._meta = {
"version": 1
version: 1,
};
16 changes: 7 additions & 9 deletions database/migrations/20200901222910-CreateTableWallet-Event.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@


let dbm;
let type;
let 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) {
* 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) {
exports.up = function (db) {
return db.createTable('wallet_event', {
id: { type: 'uuid', primaryKey: true, notNull: true },
wallet_id: { type: 'uuid', notNull: true },
Expand All @@ -27,10 +25,10 @@ exports.up = function(db) {
});
};

exports.down = function(db) {
exports.down = function (db) {
return db.dropTable('wallet_event');
};

exports._meta = {
"version": 1
version: 1,
};
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,
};
22 changes: 22 additions & 0 deletions server/handlers/eventHandler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const EventService = require('../../services/EventService');

const { eventsGetQuerySchema } = require('./schemas');

const eventsGet = async (req, res) => {
await eventsGetQuerySchema.validateAsync(req.query, { abortEarly: false });

const { limit, since, wallet } = req.query;

const eventService = new EventService();

const events = await eventService.getAllEvents({
limit,
since,
wallet,
walletLoginId: req.wallet_id,
});

res.status(200).json({ events });
};

module.exports = { eventsGet };
9 changes: 9 additions & 0 deletions server/handlers/eventHandler/schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Joi = require('joi');

const eventsGetQuerySchema = Joi.object({
limit: Joi.number().required(),
since: Joi.date().iso(),
wallet: Joi.string(),
});

module.exports = { eventsGetQuerySchema };
120 changes: 72 additions & 48 deletions server/handlers/transferHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ const {
} = require('./schemas');

const transferPost = async (req, res) => {
const validatedBody = await transferPostSchema.validateAsync(req.body, { abortEarly: false });
const validatedBody = await transferPostSchema.validateAsync(req.body, {
abortEarly: false,
});
const transferService = new TransferService();

const {wallet_id} = req
const { wallet_id } = req;
const { result, status } = await transferService.initiateTransfer(
validatedBody,
wallet_id,
Expand All @@ -20,60 +22,66 @@ const transferPost = async (req, res) => {
const modifiedTransfer = {
...result,
token_count:
+result.parameters?.bundle?.bundleSize || +result.parameters?.tokens?.length,
}
+result.parameters?.bundle?.bundleSize ||
+result.parameters?.tokens?.length,
};

res.status(status).send(modifiedTransfer);
};

const transferIdAcceptPost = async (req, res) => {
const validatedParams = await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });
const validatedParams = await transferIdParamSchema.validateAsync(
req.params,
{ abortEarly: false },
);

const {transfer_id} = validatedParams
const {wallet_id} = req
const { transfer_id } = validatedParams;
const { wallet_id } = req;
const transferService = new TransferService();
const result = await transferService.acceptTransfer(
transfer_id,
wallet_id,
);
const result = await transferService.acceptTransfer(transfer_id, wallet_id);

res.json(result);
};

const transferIdDeclinePost = async (req, res) => {
const validatedParams = await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });
const validatedParams = await transferIdParamSchema.validateAsync(
req.params,
{ abortEarly: false },
);

const {transfer_id} = validatedParams
const {wallet_id} = req
const { transfer_id } = validatedParams;
const { wallet_id } = req;
const transferService = new TransferService();
const result = await transferService.declineTransfer(
transfer_id,
wallet_id,
);
const result = await transferService.declineTransfer(transfer_id, wallet_id);

res.json(result);
};

const transferIdDelete = async (req, res) => {
const validatedParams = await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });
const validatedParams = await transferIdParamSchema.validateAsync(
req.params,
{ abortEarly: false },
);

const {transfer_id} = validatedParams
const {wallet_id} = req
const { transfer_id } = validatedParams;
const { wallet_id } = req;
const transferService = new TransferService();
const result = await transferService.cancelTransfer(
transfer_id,
wallet_id,
);
const result = await transferService.cancelTransfer(transfer_id, wallet_id);

res.json(result);
};

const transferIdFulfill = async (req, res) => {
const validatedParams = await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });
const validatedBody = await transferIdFulfillSchema.validateAsync(req.body, { abortEarly: false });
const validatedParams = await transferIdParamSchema.validateAsync(
req.params,
{ abortEarly: false },
);
const validatedBody = await transferIdFulfillSchema.validateAsync(req.body, {
abortEarly: false,
});

const {transfer_id} = validatedParams
const {wallet_id} = req
const { transfer_id } = validatedParams;
const { wallet_id } = req;
const transferService = new TransferService();
const result = await transferService.fulfillTransfer(
wallet_id,
Expand All @@ -84,53 +92,69 @@ const transferIdFulfill = async (req, res) => {
};

const transferGet = async (req, res) => {
const validatedQuery = await transferGetQuerySchema.validateAsync(req.query, { abortEarly: false });
const validatedQuery = await transferGetQuerySchema.validateAsync(req.query, {
abortEarly: false,
});

const { limit, offset, ...params } = validatedQuery;
const {wallet_id} = req
const { wallet_id } = req;

const transferService = new TransferService();

const {transfers, count} = await transferService.getByFilter({...params, limit, offset}, wallet_id);
const { transfers, count } = await transferService.getByFilter(
{ ...params, limit, offset },
wallet_id,
);

const modifiedTransfers = transfers.map((t) => ({
...t,
token_count:
+t.parameters?.bundle?.bundleSize || +t.parameters?.tokens?.length,
}));

res.status(200).json({ transfers: modifiedTransfers, query: {...params, limit, offset}, total:count });
res.status(200).json({
transfers: modifiedTransfers,
query: { ...params, limit, offset },
total: count,
});
};

const transferIdGet = async (req, res) => {
const validatedParams = await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });
const validatedParams = await transferIdParamSchema.validateAsync(
req.params,
{ abortEarly: false },
);

const {transfer_id} = validatedParams
const {wallet_id} = req
const { transfer_id } = validatedParams;
const { wallet_id } = req;
const transferService = new TransferService();
const result = await transferService.getTransferById(
transfer_id,
wallet_id,
);
const result = await transferService.getTransferById(transfer_id, wallet_id);

const modifiedTransfer = {
...result,
token_count:
+result.parameters?.bundle?.bundleSize || +result.parameters?.tokens?.length,
}
+result.parameters?.bundle?.bundleSize ||
+result.parameters?.tokens?.length,
};

res.json(modifiedTransfer);
};

const transferIdTokenGet = async (req, res) => {
const validatedParams = await transferIdParamSchema.validateAsync(req.params, { abortEarly: false });
const validatedQuery = await transferLimitOffsetQuerySchema.validateAsync(req.query, {
abortEarly: false,
});
const validatedParams = await transferIdParamSchema.validateAsync(
req.params,
{ abortEarly: false },
);
const validatedQuery = await transferLimitOffsetQuerySchema.validateAsync(
req.query,
{
abortEarly: false,
},
);

const { limit, offset } = validatedQuery;
const {transfer_id} = validatedParams
const {wallet_id} = req
const { transfer_id } = validatedParams;
const { wallet_id } = req;
const transferService = new TransferService();
const tokens = await transferService.getTokensByTransferId(
transfer_id,
Expand Down
Loading

0 comments on commit fe9733b

Please sign in to comment.