Skip to content

feature: project payment #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions bin/nosql-client.js

This file was deleted.

33 changes: 0 additions & 33 deletions bin/rdb-client.js

This file was deleted.

18 changes: 12 additions & 6 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
const path = require("path");
const rimraf = require("rimraf");

Expand All @@ -7,26 +8,31 @@ const PROTO_DIR = path.join(__dirname, "../node_modules/topcoder-interface");
const PROTO_REFLECTIONS = path.join(__dirname, "../reflections/reflection.bin");

const MODEL_DIR = path.join(__dirname, "../src/models/");

const PROTOC_PATH = "protoc";
const PLUGIN_PATH = path.join(__dirname, "../node_modules/.bin/protoc-gen-ts_proto");

rimraf.sync(`${MODEL_DIR}/*`, {
glob: { ignore: `${MODEL_DIR}/tsconfig.json` },
glob: { ignore: `${MODEL_DIR}/*.json` },
});

const protoConfig = [
`--plugin=${PLUGIN_PATH}`,
// https://github.com/stephenh/ts-proto/blob/main/README.markdown
"--ts_proto_opt=outputServices=grpc-js,env=node,useOptionals=messages,exportCommonSymbols=false,esModuleInterop=true",
`--ts_proto_out=${MODEL_DIR}`,
`--ts_proto_opt=stringEnums=true`,
`--ts_proto_opt=useDate=string`,
`--ts_proto_opt=oneof=unions`,
`--ts_proto_opt=addGrpcMetadata=true`,
`--ts_proto_opt=outputClientImpl=false`,
`--ts_proto_opt=useDate=string`,
`--include_imports`,
`--descriptor_set_out ${PROTO_REFLECTIONS}`,
`--proto_path ${PROTO_DIR} ${PROTO_DIR}/domain-layer/legacy/**/*.proto`,
`--include_imports`,
`--ts_proto_opt=Mcommon/common.proto=@topcoder-framework/lib-common`,
`--ts_proto_opt=Mgoogle/protobuf/struct.proto=@topcoder-framework/lib-common`,
`--ts_proto_opt=Mgoogle/protobuf/timestamp.proto=@topcoder-framework/lib-common`,
`--ts_proto_opt=Mgoogle/protobuf/empty.proto=@topcoder-framework/lib-common`,
`--proto_path ${PROTO_DIR} ${PROTO_DIR}/domain-layer/legacy/*.proto`,
`--proto_path ${PROTO_DIR} ${PROTO_DIR}/domain-layer/legacy/services/*.proto`,
`--ts_proto_out=${MODEL_DIR}`,
];

// https://github.com/stephenh/ts-proto#usage
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
"description": "",
"main": "index.js",
"scripts": {
"build:proto:client:nosql": "node bin/nosql-client",
"build:proto:client:rdb": "node bin/rdb-client",
"build:proto:server": "node bin/server",
"build:proto": "node bin/rdb-client && node bin/nosql-client && node bin/server",
"build:proto": "node bin/server",
"build:app": "rimraf dist && tsc -b",
"build": "node bin/rdb-client && node bin/nosql-client && node bin/server && rimraf dist && tsc -b",
"build": "node bin/server && rimraf dist && tsc -b",
"clean": "rimraf dist",
"start": "ts-node-dev --respawn --transpile-only src/server.ts"
},
"keywords": [],
"author": "Rakib Ansary <rakibansary@topcoder.com>",
"license": "ISC",
"dependencies": {
"@grpc/grpc-js": "^1.7.1",
"@topcoder-framework/client-relational": "^0.4.23-ci.0",
"@topcoder-framework/lib-common": "^0.4.23-ci.0",
"dayjs": "^1.11.5",
"dotenv": "^16.0.3",
"grpc-server-reflection": "^0.1.5",
Expand Down
76 changes: 40 additions & 36 deletions src/common/QueryRunner.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/* TODO:
1. Move this to @topcoder-framework
2. Cleanup the exported interfaces
3. Make "Client" a constructor parameter that implements a "Client" interface
4 "ExecuteSqlQuery" should return a Promise<T> where T is the type of the result for "read" queries, but should return "number" for "write" queries indicating either
a) the number of rows affected or
b) the ID of the row insertede
*/

import { ColumnType, Operator, Query, QueryRequest, Value } from "../grpc/models/rdb/relational";

import { relationalClient } from "../grpc/client/relational";
import { TableColumns, TableColumn } from "./TableColumn";
const { GRPC_RDB_SERVER_HOST, GRPC_RDB_SERVER_PORT } = process.env;

import {
ColumnType,
Operator,
Query,
QueryRequest,
RelationalClient,
Value,
} from "@topcoder-framework/client-relational";
import { TableColumn, TableColumns } from "./TableColumn";

export type Schema = {
dbSchema: string;
Expand All @@ -26,21 +24,18 @@ interface ExecuteSqlQuery {
}

type JoinAndWhereClause = JoinClause & WhereClause & ExecuteSqlQuery;
type JoinWhereLimitAndOffset = JoinAndWhereClause & LimitClause & OffsetClause;

export interface SelectQuery {
select(columns: TableColumn[]): JoinAndWhereClause & LimitClause & OffsetClause;
select(columns: TableColumn[]): JoinWhereLimitAndOffset;
}

export interface JoinClause {
join(): JoinAndWhereClause;
}

export interface WhereClause {
where(whereCriteria: {
key: string,
operator: Operator,
value: Value
}): JoinAndWhereClause & LimitClause & OffsetClause;
where(whereCriteria: { key: string; operator: Operator; value: Value }): JoinWhereLimitAndOffset;
}

export interface LimitClause {
Expand All @@ -56,26 +51,37 @@ export interface InsertQuery<CreateInput> {
}

export interface UpdateQuery<UpdateInput> {
update(lookupCriteria: {[key: string]: unknown} ,input: UpdateInput): ExecuteSqlQuery;
update(lookupCriteria: { [key: string]: unknown }, input: UpdateInput): ExecuteSqlQuery;
}

export interface DeleteQuery {
delete(): ExecuteSqlQuery;
}

export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, UpdateInput extends {[key: string]: unknown}>
implements
SelectQuery, JoinClause, WhereClause, LimitClause, OffsetClause,
export class QueryRunner<
T,
CreateInput extends { [key: string]: unknown },
UpdateInput extends { [key: string]: unknown }
> implements
SelectQuery,
JoinClause,
WhereClause,
LimitClause,
OffsetClause,
InsertQuery<CreateInput>,
UpdateQuery<UpdateInput>,
DeleteQuery,
ExecuteSqlQuery
{
#query: Query | null = null;
#client: RelationalClient;

constructor(private schema: Schema) {}
constructor(private schema: Schema) {
console.log("Connecting to GRPC server at", GRPC_RDB_SERVER_HOST, GRPC_RDB_SERVER_PORT, "...");
this.#client = new RelationalClient(GRPC_RDB_SERVER_HOST!, parseInt(GRPC_RDB_SERVER_PORT!));
}

select(columns: TableColumn[]): JoinAndWhereClause & LimitClause & OffsetClause {
select(columns: TableColumn[]): JoinWhereLimitAndOffset {
this.#query = {
query: {
$case: "select",
Expand All @@ -85,7 +91,7 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
column: columns.map((col) => ({
tableName: this.schema.tableName,
name: col.name,
type: col.type
type: col.type,
})),
where: [],
join: [],
Expand All @@ -96,15 +102,13 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
},
},
};

console.log("Query", JSON.stringify(this.#query, null, 2));
return this;
}

// TODO: use "convenience" methods from lib-util to build the clause
where(whereCriteria: {
key: string,
operator: Operator,
value: Value
}): JoinAndWhereClause & LimitClause & OffsetClause {
where(whereCriteria: { key: string; operator: Operator; value: Value }): JoinWhereLimitAndOffset {
if (this.#query?.query?.$case != "select") {
throw new Error("Cannot set where clause on a non-select query");
}
Expand All @@ -119,15 +123,15 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
}

limit(limit: number): OffsetClause & ExecuteSqlQuery {
if (this.#query?.query?.$case != "select") {
if (this.#query?.query?.$case != "select") {
throw new Error("Cannot set limit on a non-select query");
}
this.#query.query.select.limit = limit;
return this;
}

offset(offset: number): ExecuteSqlQuery {
if (this.#query?.query?.$case != "select") {
if (this.#query?.query?.$case != "select") {
throw new Error("Cannot set offset on a non-select query");
}
this.#query.query.select.offset = offset;
Expand Down Expand Up @@ -168,8 +172,8 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
})),
],
idTable: this.schema.tableName,
idColumn: "project_phase_id",
idSequence: "project_phase_id_seq",
idColumn: this.schema.idColumn ?? undefined,
idSequence: this.schema.idSequence ?? undefined,
},
},
};
Expand All @@ -194,7 +198,7 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
query: this.#query,
};

const queryResponse = await relationalClient.query(queryRequest);
const queryResponse = await this.#client.query(queryRequest);

switch (this.#query.query?.$case) {
case "select":
Expand Down
2 changes: 1 addition & 1 deletion src/common/TableColumn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ColumnType } from "../../dist/grpc/models/rdb/relational";
import { ColumnType } from "@topcoder-framework/client-relational";

export type TableColumn = {
name: string;
Expand Down
58 changes: 4 additions & 54 deletions src/domain/LegacyChallenge.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,10 @@
import _ from "lodash";
import { ColumnType, Operator, QueryRequest } from "@topcoder-framework/client-relational";
import { Value } from "@topcoder-framework/lib-common";
import { QueryRunner } from "../common/QueryRunner";

import { relationalClient } from "../grpc/client/relational";
import { ColumnType, Operator, QueryRequest, QueryResponse } from "../grpc/models/rdb/relational";
import { CheckChallengeExistsResponse } from "../models/domain-layer/legacy/legacy_challenge";
import { Value } from "../models/google/protobuf/struct";
import { Project } from "../schema/Project";
import { CheckChallengeExistsResponse } from "../models/domain-layer/legacy/challenge";
import { Project } from "../schema/project/Project";

class LegacyChallengeDomain {
constructor(private tableName: string = "project") {}

// public async lookup(lookupCriteria: LookupCriteria): Promise<LegacyChallenge[]> {
// const queryRequest: QueryRequest = {
// query: {
// query: {
// $case: "select",
// select: {
// table: this.tableName,
// join: [],
// column: [
// {
// name: "project_id",
// type: ColumnType.COLUMN_TYPE_INT,
// },
// ],
// where: [
// {
// key: "project_id",
// operator: Operator.OPERATOR_EQUAL,
// value: {
// value: {
// $case: "intValue",
// intValue: 123,
// },
// },
// },
// ],
// groupBy: [],
// orderBy: [],
// limit: 1,
// offset: 0,
// },
// },
// },
// };

// const queryResponse: QueryResponse = await relationalClient.query(queryRequest);

// if (queryResponse.result?.$case == "selectResult") {
// const rows = queryResponse.result.selectResult.rows;
// return rows.map((row) => LegacyChallenge.fromJSON(row.values));
// }

// return [];
// }

public async checkChallengeExists(
legacyChallengeId: number
): Promise<CheckChallengeExistsResponse> {
Expand Down
Loading