Skip to content

Commit

Permalink
Merge pull request #1570 from Giveth/improve-projectBySlug-query
Browse files Browse the repository at this point in the history
improve projectBySlug query
  • Loading branch information
RamRamez authored May 19, 2024
2 parents 129e051 + 33091be commit 3dadb9f
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 148 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"form-data": "^3.0.1",
"google-spreadsheet": "^3.2.0",
"graphql": "16.8.1",
"graphql-fields": "^2.0.3",
"graphql-tag": "^2.12.6",
"graphql-upload": "15.0.2",
"handlebars": "4.7.7",
Expand Down Expand Up @@ -202,6 +203,7 @@
"test:bootstrap": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/server/bootstrap.test.ts",
"test:utils": "NODE_ENV=test mocha ./src/utils/utils.test.ts",
"start": "NODE_ENV=development ts-node-dev --project ./tsconfig.json --respawn ./src/index.ts",
"start:test": "NODE_ENV=development ts-node-dev --project ./tsconfig.json --respawn ./test.ts",
"serve": "pm2 startOrRestart ecosystem.config.js --node-args='--max-old-space-size=8192'",
"db:migrate:run:test": "NODE_ENV=test npx typeorm-ts-node-commonjs migration:run -d ./src/ormconfig.ts",
"db:migrate:revert:test": "NODE_ENV=test npx typeorm-ts-node-commonjs migration:revert -d ./src/ormconfig.ts",
Expand Down
10 changes: 10 additions & 0 deletions src/repositories/projectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@ export const findProjectBySlug = (slug: string): Promise<Project | null> => {
);
};

export const findProjectIdBySlug = (slug: string): Promise<Project | null> => {
// check current slug and previous slugs
return Project.createQueryBuilder('project')
.select('project.id')
.where(`:slug = ANY(project."slugHistory") or project.slug = :slug`, {
slug,
})
.getOne();
};

export const findProjectBySlugWithoutAnyJoin = (
slug: string,
): Promise<Project | null> => {
Expand Down
11 changes: 11 additions & 0 deletions src/repositories/projectVerificationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ export const updateManagingFundsOfProjectVerification = async (params: {
return projectVerificationForm?.save();
};

export const getVerificationFormStatusByProjectId = async (
projectId: number,
): Promise<ProjectVerificationForm | null> => {
return ProjectVerificationForm.createQueryBuilder('project_verification_form')
.select(['project_verification_form.status'])
.where(`project_verification_form.projectId=:projectId`, {
projectId,
})
.getOne();
};

export const getVerificationFormByProjectId = async (
projectId: number,
): Promise<ProjectVerificationForm | null> => {
Expand Down
79 changes: 32 additions & 47 deletions src/resolvers/projectResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4287,7 +4287,7 @@ function getProjectUpdatesTestCases() {
}

function projectBySlugTestCases() {
it('should return projects with indicated slug and verification form if owner', async () => {
it('should return projects with indicated slug and verification form status if owner', async () => {
const project1 = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
Expand Down Expand Up @@ -4326,68 +4326,53 @@ function projectBySlugTestCases() {

const project = result.data.data.projectBySlug;
assert.equal(Number(project.id), project1.id);
assert.isOk(project.projectVerificationForm);
assert.equal(project.projectVerificationForm.id, verificationForm.id);
assert.isOk(project.verificationFormStatus);
assert.equal(project.verificationFormStatus, verificationForm.status);
assert.isOk(project.adminUser.walletAddress);
assert.isOk(project.adminUser.firstName);
assert.isNotOk(project.adminUser.email);
assert.isOk(project.categories[0].mainCategory.title);
});
it('should return verificationFormStatus if its not owner', async () => {
const project1 = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});

const user =
(await User.findOne({
where: {
id: project1.adminUserId,
},
})) || undefined;

const verificationForm = await ProjectVerificationForm.create({
project: project1,
user,
status: PROJECT_VERIFICATION_STATUSES.DRAFT,
}).save();

const result = await axios.post(graphqlUrl, {
query: fetchProjectBySlugQuery,
variables: {
slug: project1.slug,
connectedWalletUserId: user!.id,
},
});

const project = result.data.data.projectBySlug;
assert.equal(Number(project.id), project1.id);
assert.isNotOk(project.projectVerificationForm);
assert.equal(project.verificationFormStatus, verificationForm.status);
});

it('should return projects with indicated slug', async () => {
const walletAddress = generateRandomEtheriumAddress();
const project1 = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
walletAddress,
});

const accessToken = await generateTestAccessToken(SEED_DATA.FIRST_USER.id);
const sampleProject1 = {
title: walletAddress,
adminUserId: SEED_DATA.FIRST_USER.id,
addresses: [
{
address: walletAddress,
networkId: NETWORK_IDS.XDAI,
chainType: ChainType.EVM,
},
],
};
const res1 = await axios.post(
graphqlUrl,
{
query: createProjectQuery,
variables: {
project: sampleProject1,
},
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
const _project = res1.data.data.createProject;
const result = await axios.post(graphqlUrl, {
query: fetchProjectBySlugQuery,
variables: {
slug: project1.slug,
slug: _project.slug,
},
});

const project = result.data.data.projectBySlug;
assert.equal(Number(project.id), project1.id);
assert.equal(Number(project.id), Number(_project.id));
assert.isOk(project.adminUser.walletAddress);
assert.isOk(project.givbackFactor);
assert.isNull(project.projectVerificationForm);
assert.isOk(project.adminUser.firstName);
assert.isNotOk(project.adminUser.email);
assert.isNotEmpty(project.addresses);
Expand Down
Loading

0 comments on commit 3dadb9f

Please sign in to comment.