@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
181 lines (140 loc) • 4.18 kB
text/typescript
import * as R from "ramda";
import { createLogger } from "../logger";
import { requiresAuthentication } from "../configurationUtils";
import { PresentHookPluginOneTime } from "./presentPluginOneTime";
import { isPlayerHook, skipHook } from "./hooksPresentation";
type LoginPluginUtils = {
pluginId: string;
};
type Logs = { log_debug; log_error; log_info; log_verbose; log_warning };
const isPayloadScreen = ({ payload }) => {
const { ui_components, targetScreen, hooks } = payload;
return !!(ui_components || targetScreen || hooks);
};
const isAuthenticationRequired = ({ payload, logs }) => {
const { log_info } = logs;
if (!payload) {
log_info(
"isAuthenticationRequired: No payload exist, authentefication required"
);
return true;
}
const requires_authentication = requiresAuthentication(payload);
if (R.not(R.isNil(requires_authentication))) {
log_info(
`isAuthenticationRequired: Payload entry is requires_authentication: ${requires_authentication}`
);
return requires_authentication;
}
const isPlayer = isPlayerHook(payload);
if (isPlayer) {
log_info(
"isAuthenticationRequired: Payload is player screen, authentication not required"
);
return;
}
const _isPayloadScreen = isPayloadScreen({ payload });
log_info(
`isAuthenticationRequired: Payload is screen, authentication required: ${_isPayloadScreen}`
);
return _isPayloadScreen;
};
const runInBackground = async ({
item,
callback,
configuration,
presentUI,
checkUserAuthenticated,
logs,
pluginId,
}) => {
const { log_info, log_error } = logs;
try {
if (!isAuthenticationRequired({ payload: item, logs })) {
log_info(
"runInBackground: Background mode finished, authentication not required"
);
callback({ success: true, payload: item });
return;
}
const screenId = configuration.screen_id;
const general = configuration.general;
const hookPresentPolicy = PresentHookPluginOneTime(
pluginId,
screenId,
general
);
const shouldPresent = await hookPresentPolicy.shouldPresentScreen();
if (!shouldPresent) {
const isRefreshed = await checkUserAuthenticated();
log_info(
`runInBackground: Background mode finished, ${isRefreshed ? "finish hook with completion true" : "refresh failed will presentUI"}`
);
isRefreshed
? callback({ success: isRefreshed, payload: item })
: presentUI();
return;
}
const shouldSkipHook = skipHook(item);
if (shouldSkipHook) {
log_info(
`runInBackground: Background mode finished, payload force to skip hook ${shouldSkipHook}`
);
await hookPresentPolicy.onFinishHookSuccessfully();
callback({ success: true, payload: item });
return;
}
const isAuthenticated = await checkUserAuthenticated();
if (!isAuthenticated) {
presentUI();
return;
}
// TODO: Was copied from quick-brick-login-oauth-tv-2.0,
// Legacy, if not need must be removed
// const itemExtensions = item.extensions || {};
// let newPayload = {
// ...item,
// extensions: {
// ...itemExtensions,
// inPlayerLoginData: {},
// },
// };
log_info(
"runInBackground: Background mode finished, user is logged in, skip hook"
);
callback({ success: true, payload: item });
} catch (error) {
log_error(
`runInBackground: error: ${error.message}, hook invokation failed`,
{ error }
);
presentUI();
}
};
export const loginPluginUtils = ({ pluginId }: LoginPluginUtils) => {
const logs: Logs = createLogger({
subsystem: pluginId,
});
return {
isAuthenticationRequired: ({ payload }) => {
return isAuthenticationRequired({ payload, logs });
},
runInBackground: async ({
item,
callback,
configuration,
presentUI,
checkUserAuthenticated,
}) => {
runInBackground({
item,
callback,
configuration,
presentUI,
checkUserAuthenticated,
logs,
pluginId,
});
},
};
};