ggez-banking-sdk
Version:
A Node.js package to handle GGEZ Banking API endpoints, Simplify the process of managing CRUD operations with this efficient and easy-to-use package.
321 lines (320 loc) • 11.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAuth = void 0;
const token_1 = require("./token");
const restApi_1 = __importDefault(require("../restApi"));
const helper_1 = __importDefault(require("../helper"));
const dataStructure_1 = __importDefault(require("../helper/dataStructure"));
const useAuth = (baseUrl, nodeUrl, userInfo, tokenData, programId, geoCoordinates, lang) => {
let endPointUser = "V1/user";
const { checkResponse, headerConfig, convertDataToUrlEncoded } = (0, helper_1.default)(userInfo);
const { dataSignInUrlEncoded, dataSignInUrlEncodedWithGoogle, dataSignInDeviceEncoded, } = convertDataToUrlEncoded();
const { checkUserResponseAuth, errorHandler } = checkResponse();
const { dataSignUpApi, dataSignUpWithGoogle } = (0, dataStructure_1.default)();
const { CreateLimitedToken } = (0, token_1.useToken)(userInfo, nodeUrl, programId, "", geoCoordinates);
const GetUser = async (installationId, token, userId) => {
try {
let endPoint = `${endPointUser}/${userId}`;
let headersData = {
header: true,
isUrlEncoded: false,
token: token,
lang: lang,
installationId: installationId,
};
let headers = headerConfig(headersData);
const response = await restApi_1.default.restApi(baseUrl, "GET", endPoint, headers);
checkUserResponseAuth(response, false, "GetUser");
return {
response: response,
newUser: {},
message: response?.data?.result?.message ?? "Get User Successfully",
status: "success",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const SignUpApi = async (values) => {
let data = dataSignUpApi({ ...values, geoCoordinates: geoCoordinates });
let token;
let { message, response, status } = await CreateLimitedToken(tokenData);
if (status == "success") {
token = response?.data?.access_token;
}
else {
errorHandler(message, "SignUpApi", response);
// throw new Error(response);
}
let headersData = {
header: true,
isUrlEncoded: false,
token: token,
lang: lang,
};
let headers = headerConfig(headersData);
try {
const response = await restApi_1.default.restApi(baseUrl, "POST", endPointUser, headers, data);
checkUserResponseAuth(response, true, "SignUpApi");
return {
response: response,
newUser: {},
message: response?.data?.result?.message ?? "SignUp Api Successfully",
status: "success",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const LoginRequest = async (values) => {
let data = dataSignInUrlEncoded({ ...values, geoCoordinates: geoCoordinates }, programId);
let headersData = {
header: true,
isUrlEncoded: true,
token: null,
lang: lang,
};
let headers = headerConfig(headersData);
try {
const response = await restApi_1.default.restApi(baseUrl, "POST", "oauth/token", headers, data);
if (response.data.access_token) {
return {
response: response,
newUser: {},
message: response?.data?.result?.message ?? "Login Request Successfully",
status: "success",
};
}
return {
response: response,
newUser: null,
message: response.data.error,
status: "failed",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const LoginDeviceCredential = async (values) => {
let data = dataSignInDeviceEncoded({ ...values, geoCoordinates: geoCoordinates }, programId);
let headersData = {
header: true,
isUrlEncoded: true,
token: null,
lang: lang,
};
let headers = headerConfig(headersData);
try {
const response = await restApi_1.default.restApi(baseUrl, "POST", "oauth/token", headers, data);
if (response.data.access_token) {
return {
response: response,
newUser: {},
message: response?.data?.result?.message ??
"Login Device Credential Successfully",
status: "success",
};
}
return {
response: response,
newUser: null,
message: response.data.error,
status: "failed",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const SignUpWithGoogle = async (values) => {
let data = dataSignUpWithGoogle({
...values,
geoCoordinates: geoCoordinates,
});
let token;
let { message, response, status } = await CreateLimitedToken(tokenData);
if (status == "success") {
token = response?.data?.access_token;
}
else {
errorHandler(message, "SignUpApi", response);
}
let headersData = {
header: true,
isUrlEncoded: false,
token: token,
lang: lang,
};
let headers = headerConfig(headersData);
try {
const response = await restApi_1.default.restApi(baseUrl, "POST", endPointUser, headers, data);
checkUserResponseAuth(response, true, "SignUpWithGoogle");
return {
response: response,
newUser: {},
message: response?.data?.result?.message ?? "SignUp With Google Successfully",
status: "success",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const LoginWithGoogle = async (values) => {
let data = dataSignInUrlEncodedWithGoogle({ ...values, geoCoordinates: geoCoordinates }, programId);
let headersData = {
header: true,
isUrlEncoded: true,
token: null,
lang: lang,
};
let headers = headerConfig(headersData);
try {
const response = await restApi_1.default.restApi(baseUrl, "POST", "oauth/token", headers, data);
if (response.data.access_token) {
return {
response: response,
newUser: {},
message: response?.data?.result?.message ?? "Login Request Successfully",
status: "success",
};
}
return {
response: response,
newUser: null,
message: response.data.error,
status: "failed",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const getPromotionDataByCode = async (promotionCode) => {
try {
let headersData = {
isUrlEncoded: false,
token: "",
header: false,
disableCache: true,
};
let headers = headerConfig(headersData);
const response = await restApi_1.default.restApi(nodeUrl, "get", "api/promotion/" + promotionCode, headers);
return {
response: response,
newUser: null,
message: response?.data?.result?.message,
status: "success",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const incrementPromotionParticipants = async (giftData) => {
try {
const data = {
gift_data: giftData,
};
let headersData = {
isUrlEncoded: false,
token: "",
header: false,
};
let headers = headerConfig(headersData);
const response = await restApi_1.default.restApi(nodeUrl, "put", "api/promotion", headers, data);
return {
response: response,
newUser: null,
message: response?.data?.result?.message,
status: "success",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
const getTwitterSpotlightPostIds = async () => {
try {
let headersData = {
isUrlEncoded: false,
token: "",
header: false,
};
let headers = headerConfig(headersData);
const response = await restApi_1.default.restApi(nodeUrl, "get", "api/promotion/spotlight/twitter", headers);
return {
response: response,
newUser: null,
message: response?.data?.result?.message,
status: "success",
};
}
catch (error) {
return {
response: error.response,
newUser: null,
message: error.message,
status: "failed",
};
}
};
return {
GetUser,
SignUpApi,
LoginRequest,
LoginDeviceCredential,
SignUpWithGoogle,
LoginWithGoogle,
incrementPromotionParticipants,
getPromotionDataByCode,
getTwitterSpotlightPostIds,
};
};
exports.useAuth = useAuth;