Commit 0d28b306 authored by Kiridena I.T.K_IT19981840's avatar Kiridena I.T.K_IT19981840

Merge branch 'View-NFT-requests-from-practitioner' into 'master'

view and send NFT completed

See merge request !45
parents 9ab56074 362fe9dc
const express = require("express");
const router = express.Router();
const Patient = require("./../../model/patient/patient");
const NFTRequest = require("./../../model/stellar/nftRequests");
router.get("/:patientKey", async (req, res) => {
const patientKey = req.params.patientKey;
try {
const issuerData = await Patient.findOne({
publickey: patientKey,
});
//get the NFT requests
try {
const NFTrequests = await NFTRequest.find({
issuerpk: issuerData.issuerpk,
});
res.status(200).json({
data: NFTrequests,
});
console.log("Data taken successfully");
} catch (errorWhenGettingTheNftRrquests) {
console.log(
"Error when getting the NFT requests : ",
errorWhenGettingTheNftRrquests
);
res.status(500).json({
error: "Error when getting the NFT requests : ",
errorWhenGettingTheNftRrquests,
});
}
} catch (errorWhenGettingTheIssuerKey) {
console.log(
"Error when getting the issuer key : ",
errorWhenGettingTheIssuerKey
);
res.status(500).json({
error: "Error when getting the issuer key : ",
errorWhenGettingTheIssuerKey,
});
}
});
module.exports = router;
......@@ -2,7 +2,7 @@ const express = require("express");
const router = express.Router();
const AssetCodeWithPractitioner = require("./../../model/stellar/assetCodeWithPractitioner");
router.get("./:practitionerKey", async (req, res) => {
router.get("/:practitionerKey", async (req, res) => {
const practitionerKey = req.params.practitionerKey;
try {
......
import React from "react";
import { View, Text, StyleSheet } from "react-native";
import { useEffect, useState } from "react";
import { LOCALBACKEND } from "../../env";
import { Button, StyleSheet, View, Text, Alert } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import EncryptWithServerKey from "../../services/encryptByServerKey";
import Toast from "react-native-toast-message";
export default function PatientNFTRequests({ navigation }) {
const [data, setData] = useState([]);
const [assetData, setAssetData] = useState();
useEffect(() => {
fetchRequests();
}, []);
const fetchRequests = async () => {
//get key from async storage
try {
const patientKey = await AsyncStorage.getItem("patientStellarPublicKey");
const patientSeed = await AsyncStorage.getItem("patientStellarSecretKey");
try {
const requestUrl = LOCALBACKEND + `/nftrequests/${patientKey}`;
console.log(requestUrl);
const response = await fetch(requestUrl);
const json = await response.json();
setData(json.data);
} catch (errorFetchingRequests) {
console.log(
"Error when fetching request data : ",
errorFetchingRequests
);
}
} catch (errorWhenGettingKey) {
console.log(
"Error when getting the public key from the async storage : ",
errorWhenGettingKey
);
}
};
const renderItem = (item) => {
return (
<View key={item._id}>
<Text>Key: {item.requesterpk}</Text>
<Button title="Send NFT" onPress={() => handleSendNFT(item)}></Button>
</View>
);
};
const handleSendNFT = async (item) => {
//get the asset code for the specific practitioner
try {
const patientKey = await AsyncStorage.getItem("patientStellarPublicKey");
const patientSeed = await AsyncStorage.getItem("patientStellarSecretKey");
try {
const assetUrl =
LOCALBACKEND + `/practitioner/getassetcode/${item.requesterpk}`;
const response = await fetch(assetUrl);
const json = await response.json();
setAssetData(json.assetcode);
//send the NFT
const practitionerPubKey = item.requesterpk;
const practitionerRsaKey = item.requesterrsa;
const assetCode = assetData;
const sendNFTObj = {
patientkey: patientKey,
patientseed: patientSeed,
practitioner: practitionerPubKey,
practitionerrsa: practitionerRsaKey,
assetcode: assetCode,
};
const nftUrl = LOCALBACKEND + `/patient/createnft`;
try {
const nftResponse = await fetch(nftUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(sendNFTObj),
});
const nftResponseData = await nftResponse.json();
Toast.show({
type: "success", // 'success', 'error', 'info', 'warning'
text1: "EHR sent",
visibilityTime: 3000, // Duration to show the toast in milliseconds
autoHide: true,
});
} catch (errorWhenSendingNft) {
console.error("Error when sending NFT : ", errorWhenSendingNft);
}
} catch (errorWhenGettingAssetCode) {
console.error(
"Error when getting the asset code : ",
errorWhenGettingAssetCode
);
}
} catch (errorWhenGettingKeyPair) {
console.error("Error when getting key pair : ", errorWhenGettingKeyPair);
}
};
export default function PatientNFTRequests({ navigation, router }) {
return (
<View style={styles.container}>
<Text style={styles.text}>All NFTrequests</Text>
{data.map((item) => renderItem(item))}
<Toast ref={(ref) => Toast.setRef(ref)} />
</View>
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment