Skip to content

Commit

Permalink
add New Translator Orga Permissions Group.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Schau committed Feb 2, 2023
1 parent c579aca commit 29f0c8f
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 298 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@o-platform/api-server",
"version": "0.1.516",
"version": "0.1.518",
"description": "",
"main": "dist/index.js",
"prepublish": "tsc",
Expand Down
124 changes: 42 additions & 82 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AWS from "aws-sdk";
import { Pool } from "pg";
import fetch from "cross-fetch";
import { PrismaClient } from "./api-db/client";
import {Generate} from "./utils/generate";
import { Generate } from "./utils/generate";

export type SmtpConfig = {
from: string;
Expand All @@ -20,9 +20,8 @@ export type SmtpConfig = {
};

export class Environment {

static async validateAndSummarize(logInfo:boolean = true) {
const errors:string[] = [];
static async validateAndSummarize(logInfo: boolean = true) {
const errors: string[] = [];

if (!this.corsOrigins) {
errors.push(`The CORS_ORIGNS environment variable is not set.`);
Expand All @@ -31,22 +30,16 @@ export class Environment {
errors.push(`The APP_URL environment variable is not set.`);
}
if (!this.blockchainIndexerUrl) {
errors.push(
`The BLOCKCHAIN_INDEX_WS_URL environment variable is not set.`
);
errors.push(`The BLOCKCHAIN_INDEX_WS_URL environment variable is not set.`);
}
if (!this.rpcGatewayUrl) {
errors.push(`The RPC_GATEWAY_URL environment variable is not set.`);
}
if (!this.readonlyApiConnectionString) {
errors.push(
`The CONNECTION_STRING_RO environment variable is not set.`
);
errors.push(`The CONNECTION_STRING_RO environment variable is not set.`);
}
if (!this.readWriteApiConnectionString) {
errors.push(
`The CONNECTION_STRING_RW environment variable is not set.`
);
errors.push(`The CONNECTION_STRING_RW environment variable is not set.`);
}
if (!this.appId) {
errors.push(`The APP_ID environment variable is not set.`);
Expand All @@ -55,35 +48,23 @@ export class Environment {
errors.push(`The EXTERNAL_DOMAIN environment variable is not set.`);
}
if (!this.operatorOrganisationAddress) {
errors.push(
`The OPERATOR_ORGANISATION_ADDRESS environment variable is not set.`
);
errors.push(`The OPERATOR_ORGANISATION_ADDRESS environment variable is not set.`);
}
if (!this.pathfinderUrl) {
errors.push(
`The PATHFINDER_URL environment variable is not set.`
);
errors.push(`The PATHFINDER_URL environment variable is not set.`);
}

if (logInfo) {
console.log(
`* Testing connection to the json rpc gateway (${this.rpcGatewayUrl}) ...`
);
console.log(`* Testing connection to the json rpc gateway (${this.rpcGatewayUrl}) ...`);
}
const rpcGateway = await fetch(
this.rpcGatewayUrl
.replace("ws://", "http://")
.replace("wss://", "https://")
);
const rpcGateway = await fetch(this.rpcGatewayUrl.replace("ws://", "http://").replace("wss://", "https://"));
if (rpcGateway.status < 500) {
if (logInfo) {
console.log(" Success. Body: " + (await rpcGateway.text()));
}
} else {
errors.push(
`The json rpc gateway responded with a non 200 code: ${
rpcGateway.status
}. Body: ${await rpcGateway.text()}`
`The json rpc gateway responded with a non 200 code: ${rpcGateway.status}. Body: ${await rpcGateway.text()}`
);
}

Expand All @@ -98,9 +79,7 @@ export class Environment {
console.log(` ${this.operatorOrganisationAddress} nonce is: ${nonce}`);
}
if (!process.env.INVITATION_FUNDS_SAFE_ADDRESS) {
errors.push(
`The INVITATION_FUNDS_SAFE_ADDRESS environment variable is not set.`
);
errors.push(`The INVITATION_FUNDS_SAFE_ADDRESS environment variable is not set.`);
}

if (logInfo) {
Expand All @@ -111,18 +90,14 @@ export class Environment {
console.log(` ${this.invitationFundsSafe.address} nonce is: ${nonce}`);
}
if (!process.env.INVITATION_FUNDS_SAFE_KEY) {
errors.push(
`The INVITATION_FUNDS_SAFE_KEY environment variable is not set.`
);
errors.push(`The INVITATION_FUNDS_SAFE_KEY environment variable is not set.`);
}

if (logInfo) {
console.log(`* Checking GCS_CREDENTIALS ...`);
}
if (!this.googleCloudStorageCredentials?.project_id) {
errors.push(
`The GCS_CREDENTIALS environment variable contains an invalid json object.`
);
errors.push(`The GCS_CREDENTIALS environment variable contains an invalid json object.`);
} else if (logInfo) {
console.log(` Success:`);
console.log(` project_id:`, this.googleCloudStorageCredentials?.project_id);
Expand All @@ -134,9 +109,7 @@ export class Environment {
console.log(`* Checking GCS_AVATAR_BUCKET ...`);
}
if (!this.avatarBucketName) {
errors.push(
`The GCS_AVATAR_BUCKET environment variable is not set.`
);
errors.push(`The GCS_AVATAR_BUCKET environment variable is not set.`);
} else if (logInfo) {
console.log(` Success:`);
console.log(` bucket name:`, this.avatarBucketName);
Expand Down Expand Up @@ -170,14 +143,10 @@ export class Environment {
if (logInfo) {
console.log(` Success`);

console.log(
`* Testing connection to the indexer ws endpoint (${this.blockchainIndexerUrl}) ...`
);
console.log(`* Testing connection to the indexer ws endpoint (${this.blockchainIndexerUrl}) ...`);
}

let u = this.blockchainIndexerUrl
.replace("ws://", "http://")
.replace("wss://", "https://");
let u = this.blockchainIndexerUrl.replace("ws://", "http://").replace("wss://", "https://");

if (!u.endsWith("/")) {
u += "/";
Expand All @@ -193,9 +162,7 @@ export class Environment {
}
} else {
const body = await indexerWsEndpoint.text();
errors.push(
`The indexer ws endpoint responded with a non 200 code: ${indexerWsEndpoint.status}. Body: ${body}`
);
errors.push(`The indexer ws endpoint responded with a non 200 code: ${indexerWsEndpoint.status}. Body: ${body}`);
}

if (errors.length > 0) {
Expand All @@ -221,20 +188,20 @@ export class Environment {
}

private static _instanceId = Generate.randomBase64String(8).substr(0, 8);
static get instanceId() : string {
static get instanceId(): string {
return this._instanceId;
}

static get keyRotationInterval(): number {
return 24 * 60 * 60 * 1000;
return 24 * 60 * 60 * 1000;
}

static get periodicTaskInterval() : number {
return 5 * 60 * 1000;
static get periodicTaskInterval(): number {
return 5 * 60 * 1000;
}

static get maxKeyAge() : number {
return 2 * this.keyRotationInterval;
static get maxKeyAge(): number {
return 2 * this.keyRotationInterval;
}

private static _indexDb: Pool = new Pool({
Expand All @@ -250,10 +217,12 @@ export class Environment {
private static _pgReadWriteApiDb: Pool = new Pool({
connectionString: process.env.CONNECTION_STRING_RW,
//ssl: !process.env.DEBUG,
ssl: process.env.API_DB_SSL_CERT ? {
cert: process.env.API_DB_SSL_CERT,
ca: process.env.API_DB_SSL_CA
} : undefined
ssl: process.env.API_DB_SSL_CERT
? {
cert: process.env.API_DB_SSL_CERT,
ca: process.env.API_DB_SSL_CA,
}
: undefined,
}).on("error", (err) => {
console.error("An idle client has experienced an error", err.stack);
});
Expand Down Expand Up @@ -320,10 +289,7 @@ export class Environment {
}

static get sessionLifetimeInSeconds(): number {
return parseInt(
<string | undefined>process.env.SESSION_LIIFETIME ??
(60 * 60 * 24 * 30).toString()
);
return parseInt(<string | undefined>process.env.SESSION_LIIFETIME ?? (60 * 60 * 24 * 30).toString());
}

static get appId(): string {
Expand Down Expand Up @@ -370,25 +336,20 @@ export class Environment {
*/
static get operatorOrganisationAddress(): string {
// TODO: Remove default value "Basic Income Lab - Test Orga"
return <string>(
(process.env.OPERATOR_ORGANISATION_ADDRESS ??
"0xc5a786eafefcf703c114558c443e4f17969d9573")
);
return <string>(process.env.OPERATOR_ORGANISATION_ADDRESS ?? "0xc5a786eafefcf703c114558c443e4f17969d9573");
}
static get translatorOrganisationAddress(): string {
// TODO: Remove default value "Basic Income Lab - Test Orga"
return <string>(process.env.TRANSLATOR_ORGANISATION_ADDRESS ?? "0xe3F306f70A3FFDD20c7b26B3ad3650Cc31e5D84A");
}

static get circlesHubAddress(): string {
return <string>(
(process.env.CIRCLES_HUB_ADDRESS?.toLowerCase() ??
"0x29b9a7fBb8995b2423a71cC17cf9810798F6C543")
);
return <string>(process.env.CIRCLES_HUB_ADDRESS?.toLowerCase() ?? "0x29b9a7fBb8995b2423a71cC17cf9810798F6C543");
}

static get invitationFundsSafe(): GnosisSafeProxy {
return new GnosisSafeProxy(
RpcGateway.get(),
RpcGateway.get().utils.toChecksumAddress(
<string>process.env.INVITATION_FUNDS_SAFE_ADDRESS
)
RpcGateway.get().utils.toChecksumAddress(<string>process.env.INVITATION_FUNDS_SAFE_ADDRESS)
);
}

Expand All @@ -398,7 +359,7 @@ export class Environment {
);
}

static get googleCloudStorageCredentials(): GoogleCloudCredentials|undefined {
static get googleCloudStorageCredentials(): GoogleCloudCredentials | undefined {
try {
return JSON.parse(<string>process.env.GCS_CREDENTIALS);
} catch {
Expand All @@ -415,8 +376,7 @@ export class Environment {
*/
static get invitationFundsAmount(): BN {
return new BN(
<string | undefined>process.env.INVITATION_FUNDS_AMOUNT ??
RpcGateway.get().utils.toWei("0.1", "ether")
<string | undefined>process.env.INVITATION_FUNDS_AMOUNT ?? RpcGateway.get().utils.toWei("0.1", "ether")
);
}
}
Expand All @@ -426,5 +386,5 @@ export type GoogleCloudCredentials = {
project_id?: string;
client_email?: string;
private_key?: string;
[x:string]: any
}
[x: string]: any;
};
24 changes: 13 additions & 11 deletions src/resolvers/mutations/addNewLang.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {MutationAddNewLangArgs} from "../../types";
import {Context} from "../../context";
import {isBILMember} from "../../utils/canAccess";
import {Environment} from "../../environment";
import { MutationAddNewLangArgs } from "../../types";
import { Context } from "../../context";
import { isTranslator } from "../../utils/canAccess";
import { Environment } from "../../environment";

export const addNewLang = async (parent: any, args: MutationAddNewLangArgs, context: Context) => {
const callerInfo = await context.callerInfo;
const isBilMember = await isBILMember(callerInfo?.profile?.circlesAddress);
if (!isBilMember) {
throw new Error (`You need to be a member of Basic Income Lab to add a new Language.`)
const canTranslate = await isTranslator(callerInfo?.profile?.circlesAddress);
if (!canTranslate) {
throw new Error(`You need to be a member of the Translator Orga to add or edit Translations.`);
} else {
const queryResult = await Environment.pgReadWriteApiDb.query(`
const queryResult = await Environment.pgReadWriteApiDb.query(
`
insert into i18n (lang, key, "createdBy", version, value)
select $1 as lang
, i18n.key
Expand All @@ -26,7 +27,8 @@ export const addNewLang = async (parent: any, args: MutationAddNewLangArgs, cont
and i18n.lang = max_versions.lang
and i18n.version = max_versions.version;
`,
[args.langToCreate, args.langToCopyFrom]);
return queryResult.rowCount
[args.langToCreate, args.langToCopyFrom]
);
return queryResult.rowCount;
}
}
};
12 changes: 6 additions & 6 deletions src/resolvers/mutations/createNewStringAndKey.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Context } from "../../context";
import { Environment } from "../../environment";
import { MutationCreateNewStringAndKeyArgs } from "../../types";
import { isBILMember } from "../../utils/canAccess";
import { isTranslator } from "../../utils/canAccess";

export const createNewStringAndKey = async (parent: any, args: MutationCreateNewStringAndKeyArgs, context: Context) => {
let callerInfo = await context.callerInfo;
let isBilMember = await isBILMember(callerInfo?.profile?.circlesAddress);
if (!isBilMember) {
throw new Error(`Your need to be a member of Basic Income Lab to edit the content.`);
let canTranslate = await isTranslator(callerInfo?.profile?.circlesAddress);
if (!canTranslate) {
throw new Error(`You need to be a member of the Translator Orga to add or edit Translations.`);
} else {
let createdBy = callerInfo?.profile?.circlesAddress;
const queryResult = await Environment.pgReadWriteApiDb.query(
Expand Down Expand Up @@ -49,7 +49,7 @@ export const createNewStringAndKey = async (parent: any, args: MutationCreateNew
`,
[args.key]
);

return newEntry;
}
}
};
10 changes: 5 additions & 5 deletions src/resolvers/mutations/setStringUpdateState.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Context } from "../../context";
import { Environment } from "../../environment";
import { MutationSetStringUpdateStateArgs } from "../../types";
import { isBILMember } from "../../utils/canAccess";
import { isTranslator } from "../../utils/canAccess";

export const setStringUpdateState = async (parent: any, args: MutationSetStringUpdateStateArgs, context: Context) => {
let callerInfo = await context.callerInfo;
let isBilMember = await isBILMember(callerInfo?.profile?.circlesAddress);
if (!isBilMember) {
throw new Error(`Your need to be a member of Basic Income Lab to edit the content.`);
let canTranslate = await isTranslator(callerInfo?.profile?.circlesAddress);
if (!canTranslate) {
throw new Error(`You need to be a member of the Translator Orga to add or edit Translations.`);
} else {
let queryResult = await Environment.pgReadWriteApiDb.query(
`
Expand All @@ -20,4 +20,4 @@ export const setStringUpdateState = async (parent: any, args: MutationSetStringU
);
return queryResult.rows[0];
}
}
};
Loading

0 comments on commit 29f0c8f

Please sign in to comment.