namespace sap.capire.bank_details;
using { Currency, managed, cuid, temporal } from '@sap/cds/common';
type Acc_type : String enum {
Savings;
Current;
}
type Status : String enum {
Active;
DeActivated;
}
type Transaction_type : String enum {
deposit;
withdraw;
}
entity State {
key ID : Integer;
name : localized String(100) not null @assert.unique;
customers : Composition of many Customers on customers.state = $self;
banks : Composition of many Banks on banks.state = $self;
}
entity City {
key ID : Integer;
name : localized String(100) not null @assert.unique;
customers : Composition of many Customers on customers.city = $self;
banks : Composition of many Banks on banks.city = $self;
}
entity Banks : managed {
key bankID : Integer @assert.unique;
bankname : String(100) @assert.unique;
state : Composition of State ;
city : Composition of City;
status : Status default 'Active';
customers : Composition of many Customers on customers.bank = $self;
}
entity Customers : managed {
key custID : Integer @assert.unique;
firstname : localized String(50) ;
lastname : localized String(50) ;
age : Integer ;
dateOfBirth : Date;
bank : Composition of Banks;
address : localized String(500) ;
state : Composition of State ;
city : Composition of City;
status : Status default 'Active';
message : String(50) default 'Customer Created Successfully';
accounts : Composition of many Accounts on accounts.customers = $self;
}
entity Accounts : managed {
key accountid : Integer64 @assert.unique;
customers : Association to Customers;
account_type : Acc_type default 'Savings';
Balance : Integer;
account_status : Status default 'Active';
message : String(50) default 'Account Created Successfully';
transections : Composition of many Transections on transections.accounts = $self;
}
entity Transections : cuid {
key accounts : Association to Accounts;
type : Transaction_type;
description : String(100);
date : DateTime @cds.on.update: $now;
amount : Integer;
}
user: bank_operations $ cds watch
using { sap.capire.bank_details as my } from '../db/schema';
service ExecutiveService @(requires: 'authenticated-user' ) {
entity Banks @(restrict: [ { grant: '*', to: 'Manager'}])
as projection on my.Banks {
key bankID,
bankname,
state.name as StateName,
city.name as CityName,
state,
city,
customers
};
entity Customers @(restrict: [ { grant: '*', to: 'Manager'}])
as projection on my.Customers {
key custID,
firstname,
lastname,
bank.bankID as BankId,
bank.bankname as BankName,
status as Accountstatus,
address,
city.name as CityName,
state.name as StateName,
accounts,
state,
city,
bank
};
entity Accounts @(restrict: [ { grant: '*', to: 'Manager'}])
as projection on my.Accounts {
key accountid,
account_type,
account_status,
Balance,
customers.custID,
customers.firstname ,
customers.lastname,
customers.bank.bankID,
customers.bank.bankname,
message,
createdAt,
transections,
customers
} ;
entity Transections @(restrict: [ { grant: '*', to: 'Manager'}])
as projection on my.Transections;
@readonly State as projection on my.State;
}
{
"auth": {
"passport": {
"strategy": "mock",
"users": {
"Ranjith": {
"password": "user",
"ID": "200",
"roles": [
"authenticated-user",
"Manager"
]
},
"Vinyan": {
"password": "user",
"ID": "202",
"roles": [
"authenticated-user",
"customer"
]
}
}
}
}
}
ID;name
401;"Tamilnadu"
402;"Kerala"
When clicking any entity, you can see the browser is asking for the user authentication details. Provide the valid user details to access the data.
const cds = require('@sap/cds')
const { Banks, Customers, Accounts, Transactions } = cds.entities
module.exports = cds.service.impl((srv)=> {
srv.before('CREATE', 'Transections', _transection)
//srv.before('UPDATE', 'Transections' , _beforeTransectionUpdate)
srv.after('CREATE', 'Banks', _afterCreationBank)
})
async function _transection(req) {
const data = req.data
console.log(req.user)
return cds.transaction(req).run(()=> {
if(data.hasOwnProperty("type")) {
if(data.type === 'Deposit') {
UPDATE(Accounts).set('balance +=', data.amount).where('accountid =', data.accounts_accountid)
} else {
UPDATE(Accounts).set('balance -=', data.amount).where('accountid =', data.accounts_accountid).and(
'balance >=', data.amount
)
}
}
}).then((affectedrows) => {
if(affectedrows == 0) {
req.error(409, `insufficient balance in the #${data.accounts_accountid}`)
}
})
}
async function _afterCreationBank(bankdetails, req) {
console.log(`Bank ${bankdetails.bankID} is Created`)
for(let i in bankdetails.customers) {
console.log(`Customer ${bankdetails.customers[i].custID} is Created`)
for(let j in bankdetails.customers[i].accounts) {
console.log(`Account ${bankdetails.customers[i].accounts[j].accountid} is Created`)
for(let k in bankdetails.customers[i].accounts[j].transections) {
console.log(`Transecction ${bankdetails.customers[i].accounts[j].transections[k].ID} is Created`)
}
}
}
}
### creating all the details using many to many association
### Read
send Request
GET http://localhost:4004/executive/Banks HTTP/1.1\
Authorization: Basic YWRtaW46
### Create
send Request
POST http://localhost:4004/executive/Banks HTTP/1.1
Content-Type: application/json
Authorization: Basic YWRtaW46
{
"bankID": 117,
"bankname": "Mahendra Bank",
"state_ID" : 401,
"city_ID" : 502,
"customers" : [
{
"custID" : 230,
"firstname" : "Dhnees",
"lastname" : "naaraj",
"address" : "asff Street",
"state_ID" : 401,
"city_ID" : 502,
"accounts" : [
{
"accountid" : 1000000000000800,
"Balance" : 45,
"account_status" : "Active",
"account_type" : "Savings",
"transections" : [
{
"type" : "Deposit",
"description" : "Check Deposit",
"amount" : 45
}
]
},
{
"accountid" : 1000000000000900,
"Balance" : 45,
"account_status" : "Active",
"account_type" : "Current",
"transections" : [
{
"type" : "withdraw",
"description" : "Check Deposit",
"amount" : 45
}
]
}
]
},
{
"custID" : 231,
"firstname" : "Dhaneesha",
"lastname" : "narayanaraj",
"address" : "Kuppaiah Street",
"state_ID" : 401,
"city_ID" : 502,
"accounts" : [
{
"accountid" : 1000000000001100,
"Balance" : 45,
"account_status" : "Active",
"account_type" : "Savings",
"transections" : [
{
"type" : "Deposit",
"description" : "Check Deposit",
"amount" : 45
}
]
},
{
"accountid" : 1000000000001200,
"Balance" : 45,
"account_status" : "Active",
"account_type" : "Current",
"transections" : [
{
"type" : "withdraw",
"description" : "Check Deposit",
"amount" : 45
}
]
}
]
}
]
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
10 | |
6 | |
5 | |
4 | |
4 | |
3 | |
3 | |
2 | |
2 |