feat(client): show real data
This commit is contained in:
@@ -5,11 +5,11 @@
|
|||||||
<div class="avatar avatar-xl mb-3">
|
<div class="avatar avatar-xl mb-3">
|
||||||
<i class="bi bi-person-circle fs-1 text-primary"></i>
|
<i class="bi bi-person-circle fs-1 text-primary"></i>
|
||||||
</div>
|
</div>
|
||||||
<h3 class="mb-1">{{ username }}</h3>
|
<h3 class="mb-1">{{ this.api.currentUser.displayName }}</h3>
|
||||||
<p class="text-muted mb-4">{{ this.api.currentUser }}</p>
|
<p class="text-muted mb-4">{{ this.api.currentUser.id }}</p>
|
||||||
<div class="d-flex align-items-center justify-content-center gap-2 mb-1">
|
<div class="d-flex align-items-center justify-content-center gap-2 mb-1">
|
||||||
<i class="bi bi-wallet2 fs-4"></i>
|
<i class="bi bi-wallet2 fs-4"></i>
|
||||||
<h3 class="mb-0">{{ balance | currency}}</h3>
|
<h3 class="mb-0">{{ this.api.currentUser.balance | currency}}</h3>
|
||||||
</div>
|
</div>
|
||||||
<button type="button" (click)="logOut()" class="btn btn-outline-secondary">Log out</button>
|
<button type="button" (click)="logOut()" class="btn btn-outline-secondary">Log out</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
<i class="bi bi-person-fill fs-4 text-secondary"></i>
|
<i class="bi bi-person-fill fs-4 text-secondary"></i>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-start">
|
<div class="text-start">
|
||||||
@if (transaction.receiverID == this.api.currentUser) {
|
@if (transaction.receiverID == this.api.currentUser.id) {
|
||||||
<h6 class="mb-0">{{ transaction.senderID }}</h6>
|
<h6 class="mb-0">{{ transaction.senderID }}</h6>
|
||||||
}@else {
|
}@else {
|
||||||
<h6 class="mb-0">{{ transaction.receiverID }}</h6>
|
<h6 class="mb-0">{{ transaction.receiverID }}</h6>
|
||||||
@@ -40,7 +40,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-end">
|
<div class="text-end">
|
||||||
@if (transaction.receiverID == this.api.currentUser) {
|
@if (transaction.receiverID == this.api.currentUser.id) {
|
||||||
<h6 class="mb-0 text-success">
|
<h6 class="mb-0 text-success">
|
||||||
{{ transaction.amount | currency }}
|
{{ transaction.amount | currency }}
|
||||||
</h6>
|
</h6>
|
||||||
|
|||||||
@@ -11,9 +11,6 @@ import { Router } from '@angular/router';
|
|||||||
styleUrl: './screen-profile.less',
|
styleUrl: './screen-profile.less',
|
||||||
})
|
})
|
||||||
export class ScreenProfile implements OnInit{
|
export class ScreenProfile implements OnInit{
|
||||||
// TODO display real data
|
|
||||||
username = 'John Doe';
|
|
||||||
balance = 200;
|
|
||||||
transactions = signal<Transaction[]>([])
|
transactions = signal<Transaction[]>([])
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
|||||||
@@ -40,7 +40,7 @@
|
|||||||
|
|
||||||
<ng-template #content let-modal>
|
<ng-template #content let-modal>
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h4 class="modal-title" id="modal-basic-title">Pay {{ amount}} to {{ this.api.currentUser }}</h4>
|
<h4 class="modal-title" id="modal-basic-title">Pay {{ amount}} to {{ this.api.currentUser.id }}</h4>
|
||||||
<button type="button" class="btn-close" aria-label="Close" (click)="modal.dismiss()"></button>
|
<button type="button" class="btn-close" aria-label="Close" (click)="modal.dismiss()"></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body text-center">
|
<div class="modal-body text-center">
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export class ScreenReceive {
|
|||||||
amount: number = 0;
|
amount: number = 0;
|
||||||
get shareableLink(): string {
|
get shareableLink(): string {
|
||||||
const currentDomain = window.location.origin;
|
const currentDomain = window.location.origin;
|
||||||
return `${currentDomain}/send/${this.api.currentUser}?amount=${this.amount}`;
|
return `${currentDomain}/send/${this.api.currentUser.id}?amount=${this.amount}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
copyLink() {
|
copyLink() {
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ export class ScreenSend {
|
|||||||
sendMoney() {
|
sendMoney() {
|
||||||
this.api.send(this.amount, this.recipient, this.reference).subscribe({
|
this.api.send(this.amount, this.recipient, this.reference).subscribe({
|
||||||
next:()=> {
|
next:()=> {
|
||||||
this.clear()
|
|
||||||
this.notify.success(`Sent ${this.amount} to ${this.recipient}`);
|
this.notify.success(`Sent ${this.amount} to ${this.recipient}`);
|
||||||
|
this.clear()
|
||||||
},
|
},
|
||||||
error:(err)=> {
|
error:(err)=> {
|
||||||
if(err.status == 404){
|
if(err.status == 404){
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { BehaviorSubject, catchError, map, Observable, of, tap } from 'rxjs';
|
|||||||
import Transaction from '@model/transaction'
|
import Transaction from '@model/transaction'
|
||||||
import { SendRequest, SendResponse } from '@message/Send';
|
import { SendRequest, SendResponse } from '@message/Send';
|
||||||
import { TransactionsRequest } from '@message/Transactions';
|
import { TransactionsRequest } from '@message/Transactions';
|
||||||
|
import { LoginResponse } from '@message/Login';
|
||||||
|
import Account from '@model/user';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
@@ -13,15 +15,19 @@ export class APIService {
|
|||||||
private isAuthenticatedSubject = new BehaviorSubject<boolean>(false);
|
private isAuthenticatedSubject = new BehaviorSubject<boolean>(false);
|
||||||
isAuthenticated$ = this.isAuthenticatedSubject.asObservable();
|
isAuthenticated$ = this.isAuthenticatedSubject.asObservable();
|
||||||
|
|
||||||
// TODO use real user here
|
currentUser!: Account;
|
||||||
currentUser = 'DemoUser';
|
ownedAccounts!: Account[];
|
||||||
|
|
||||||
constructor(private http: HttpClient){}
|
constructor(private http: HttpClient){}
|
||||||
|
|
||||||
login(username: string, password: string): Observable<any>{
|
login(username: string, password: string): Observable<any>{
|
||||||
return this.http.post(`${this.apiUrl}/auth/login`,{ 'username': username, 'password': password}).pipe(
|
return this.http.post<LoginResponse>(`${this.apiUrl}/auth/login`,{ 'username': username, 'password': password}).pipe(
|
||||||
tap({
|
tap({
|
||||||
next: () => this.isAuthenticatedSubject.next(true),
|
next: (resp) => {
|
||||||
|
this.isAuthenticatedSubject.next(true);
|
||||||
|
this.currentUser = resp.user;
|
||||||
|
this.ownedAccounts = resp.ownedAccounts;
|
||||||
|
},
|
||||||
error: () => this.isAuthenticatedSubject.next(false)
|
error: () => this.isAuthenticatedSubject.next(false)
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -50,7 +56,8 @@ export class APIService {
|
|||||||
return this.http.get<Transaction[]>(`${this.apiUrl}/transactions`);
|
return this.http.get<Transaction[]>(`${this.apiUrl}/transactions`);
|
||||||
}
|
}
|
||||||
send(amount: number, recipientID: string, reference: string = ""): Observable<SendResponse>{
|
send(amount: number, recipientID: string, reference: string = ""): Observable<SendResponse>{
|
||||||
let request: SendRequest = {amount, recipientID, reference};
|
|
||||||
|
let request: SendRequest = {senderID: this.currentUser.id, amount, recipientID, reference};
|
||||||
return this.http.post<SendResponse>(`${this.apiUrl}/send`, request);
|
return this.http.post<SendResponse>(`${this.apiUrl}/send`, request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ export class LoginRequest{
|
|||||||
export class LoginResponse{
|
export class LoginResponse{
|
||||||
constructor(
|
constructor(
|
||||||
public user: Account,
|
public user: Account,
|
||||||
public ownedBusinesses: Account[],
|
public ownedAccounts: Account[],
|
||||||
){}
|
){}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export class SendRequest{
|
export class SendRequest{
|
||||||
constructor(
|
constructor(
|
||||||
|
public senderID: string,
|
||||||
public recipientID: string,
|
public recipientID: string,
|
||||||
public amount: number,
|
public amount: number,
|
||||||
public reference: string
|
public reference: string
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import Transaction from "../model/transaction";
|
|||||||
|
|
||||||
export class TransactionsRequest{
|
export class TransactionsRequest{
|
||||||
constructor(
|
constructor(
|
||||||
|
public forId: string,
|
||||||
public offset = 0,
|
public offset = 0,
|
||||||
public count = 50,
|
public count = 50,
|
||||||
){}
|
){}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { Table, Column, Model, DataType, Scopes, DefaultScope, DeletedAt, BelongsToMany, ForeignKey} from 'sequelize-typescript';
|
import { Table, Column, Model, DataType, Scopes, DefaultScope, DeletedAt, BelongsToMany, ForeignKey} from 'sequelize-typescript';
|
||||||
|
|
||||||
|
@Table
|
||||||
@DefaultScope(() => ({
|
@DefaultScope(() => ({
|
||||||
|
//FIXME getting account still includes password
|
||||||
attributes:{ exclude: ['password']}
|
attributes:{ exclude: ['password']}
|
||||||
}))
|
}))
|
||||||
@Scopes(() => ({
|
@Scopes(() => ({
|
||||||
@@ -8,7 +10,6 @@ import { Table, Column, Model, DataType, Scopes, DefaultScope, DeletedAt, Belong
|
|||||||
attributes: {include: ['password']}
|
attributes: {include: ['password']}
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
@Table
|
|
||||||
export default class Account extends Model{
|
export default class Account extends Model{
|
||||||
|
|
||||||
@Column({primaryKey: true, unique: true, allowNull: false})
|
@Column({primaryKey: true, unique: true, allowNull: false})
|
||||||
@@ -29,17 +30,37 @@ export default class Account extends Model{
|
|||||||
@DeletedAt
|
@DeletedAt
|
||||||
declare deletedAt: Date | null;
|
declare deletedAt: Date | null;
|
||||||
|
|
||||||
|
@BelongsToMany(() => Account, () => BusinessOwnership, 'ownerAccountId', 'ownedAccountId')
|
||||||
|
declare ownedBusinesses: Account[];
|
||||||
|
|
||||||
|
@BelongsToMany(() => Account, () => BusinessOwnership, 'ownedAccountId', 'ownerAccountId')
|
||||||
|
declare owners: Account[];
|
||||||
|
|
||||||
|
override toJSON() {
|
||||||
|
const values = { ...this.get() };
|
||||||
|
delete values.password;
|
||||||
|
return values;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Table
|
@Table
|
||||||
export class BusinessOwnership extends Model {
|
export class BusinessOwnership extends Model {
|
||||||
@ForeignKey(() => Account)
|
@ForeignKey(() => Account)
|
||||||
@Column
|
@Column
|
||||||
ownerAccountId!: number;
|
declare ownerAccountId: string;
|
||||||
|
|
||||||
@ForeignKey(() => Account)
|
@ForeignKey(() => Account)
|
||||||
@Column
|
@Column
|
||||||
ownedAccountId!: number;
|
declare ownedAccountId: string;
|
||||||
|
|
||||||
@DeletedAt
|
@DeletedAt
|
||||||
declare deletedAt: Date | null;
|
declare deletedAt: Date | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getOwnedAccounts(user: Account){
|
||||||
|
let q = await Account.findByPk(user.id, {
|
||||||
|
include: ['ownedBusinesses'],
|
||||||
|
});
|
||||||
|
let ownedAccounts = q?.ownedBusinesses;
|
||||||
|
return ownedAccounts ?? [];
|
||||||
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import { compare } from 'bcrypt';
|
import { compare } from 'bcrypt';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { logger } from '../util/logging';
|
import { logger } from '../util/logging';
|
||||||
import Account from '../model/user';
|
import Account, { getOwnedAccounts } from '../model/user';
|
||||||
import { Scope } from '../util/db';
|
import { Scope } from '../util/db';
|
||||||
import { getJWT, requireAuth } from '../util/auth';
|
import { getJWT, requireAuth } from '../util/auth';
|
||||||
import { LoginRequest } from '../messages/Login';
|
import { LoginRequest, LoginResponse } from '../messages/Login';
|
||||||
import { GenericMessage as Msg } from '../messages/Message';
|
import { GenericMessage as Msg } from '../messages/Message';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@@ -25,7 +25,7 @@ router.post('/login', async (req, res) => {
|
|||||||
sameSite: 'strict', // CSRF protection
|
sameSite: 'strict', // CSRF protection
|
||||||
maxAge: 86400000, // 1 day
|
maxAge: 86400000, // 1 day
|
||||||
});
|
});
|
||||||
res.json(new Msg('Logged in successfully'));
|
res.json(new LoginResponse(user, await getOwnedAccounts(user)));
|
||||||
}catch (err) {
|
}catch (err) {
|
||||||
logger.error('Failed to authenticate:', err);
|
logger.error('Failed to authenticate:', err);
|
||||||
res.status(500).json(new Msg('Failed to authenticate'));
|
res.status(500).json(new Msg('Failed to authenticate'));
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { logger } from '../util/logging';
|
import { logger } from '../util/logging';
|
||||||
import { requireAuth } from '../util/auth';
|
import { requireAuth } from '../util/auth';
|
||||||
import Account from '../model/user';
|
import Account, { BusinessOwnership, getOwnedAccounts } from '../model/user';
|
||||||
import { db } from '../util/db';
|
import { db } from '../util/db';
|
||||||
import Transaction from '../model/transaction';
|
import Transaction from '../model/transaction';
|
||||||
import { SendRequest, SendResponse} from '../messages/Send';
|
import { SendRequest, SendResponse} from '../messages/Send';
|
||||||
@@ -12,15 +12,25 @@ const router = express.Router();
|
|||||||
|
|
||||||
router.post('/', requireAuth, async (req, res) => {
|
router.post('/', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const sender = res.locals.user as Account;
|
const user = res.locals.user as Account;
|
||||||
const data : SendRequest = req.body;
|
const data : SendRequest = req.body;
|
||||||
const recipient = await Account.findOne({where: {id: data.recipientID}})
|
|
||||||
if ( Number(data.amount) <= 0) {
|
if ( Number(data.amount) <= 0) {
|
||||||
return res.status(400).json(new Err('Invalid transfer amount'));
|
return res.status(400).json(new Err('Invalid transfer amount'));
|
||||||
}
|
}
|
||||||
|
const recipient = await Account.findOne({where: {id: data.recipientID}})
|
||||||
if (!recipient) {
|
if (!recipient) {
|
||||||
return res.status(404).json(new Err('Recipient not found' ));
|
return res.status(404).json(new Err('Recipient not found' ));
|
||||||
}
|
}
|
||||||
|
const sender = await Account.findOne({where: {id: data.senderID}})
|
||||||
|
if (!sender) {
|
||||||
|
return res.status(404).json(new Err('Sender not found' ));
|
||||||
|
}
|
||||||
|
let ownsAccount = (await getOwnedAccounts(user))?.some(
|
||||||
|
business => business.id == data.senderID
|
||||||
|
);
|
||||||
|
if(!(user.equals(sender) || ownsAccount )){
|
||||||
|
return res.status(403).json(new Err('Unauthorized sender'));
|
||||||
|
}
|
||||||
if (Number(sender.balance) < Number(data.amount)){
|
if (Number(sender.balance) < Number(data.amount)){
|
||||||
logger.debug(`Insufficient balance: ${sender.balance} < ${data.amount}`)
|
logger.debug(`Insufficient balance: ${sender.balance} < ${data.amount}`)
|
||||||
return res.status(402).json(new Err('Insufficient balance'))
|
return res.status(402).json(new Err('Insufficient balance'))
|
||||||
|
|||||||
Reference in New Issue
Block a user