Skip to content

Commit

Permalink
feat(be): get access status
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Aug 30, 2023
1 parent e2fb605 commit 87726f6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/graphql/graphql.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ export class GraphqlService {
);
return response.data;
}

errorOrEmpty(result: any, fieldName: string) {
if (result.errors && result.errors.length > 0) {
return true;
}

if (result.data[fieldName].length === 0) {
return true;
}

return false;
}
}
8 changes: 8 additions & 0 deletions src/manga/dto/get-access-manga-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber } from 'class-validator';

export class GetAccessMangaParamDto {
@ApiProperty()
@IsNumber()
mangaId: number;
}
12 changes: 12 additions & 0 deletions src/manga/manga.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Get,
Param,
Post,
Put,
Expand All @@ -21,6 +22,7 @@ import {
import { Roles } from '../auth/roles.decorator';
import { Role } from '../auth/role.enum';
import { RolesGuard } from '../auth/role.guard';
import { GetAccessMangaParamDto } from './dto/get-access-manga-request.dto';

@Controller('manga')
@ApiTags('manga')
Expand Down Expand Up @@ -55,4 +57,14 @@ export class MangaController {
const { mangaId } = param;
return this.mangaSvc.update(mangaId, data, files);
}

@UseGuards(AuthGuard, RolesGuard)
@ApiBearerAuth()
@Roles(Role.User)
@Get(':mangaId/get-access')
@UseInterceptors(AuthUserInterceptor, AnyFilesInterceptor())
getAccess(@Param() param: GetAccessMangaParamDto) {
const { mangaId } = param;
return this.mangaSvc.getAccess(mangaId);
}
}
83 changes: 83 additions & 0 deletions src/manga/manga.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,87 @@ export class MangaService {

return updateResponse;
}

async getAccess(mangaId: number) {
try {
let nft = false;
const { token } = ContextProvider.getAuthUser();
const result = await this.graphqlSvc.query(
this.configSvc.get<string>('graphql.endpoint'),
token,
`query QueryUserAddress {
authorizer_users {
wallet_address
}
}`,
'QueryUserAddress',
{},
);

if (this.graphqlSvc.errorOrEmpty(result, 'authorizer_users')) {
return result;
}

const walletAddress = result.data.authorizer_users[0].wallet_address;

// get contract_addresses
const queryMangaResult = await this.graphqlSvc.query(
this.configSvc.get<string>('graphql.endpoint'),
token,
`query QueryManga($id: Int!) {
manga_by_pk(id: $id) {
contract_addresses
}
}`,
'QueryManga',
{
id: mangaId,
},
);

if (this.graphqlSvc.errorOrEmpty(queryMangaResult, 'manga_by_pk')) {
return queryMangaResult;
}

const contract_addresses =
queryMangaResult.data.manga_by_pk.contract_addresses;

// check data on horoscope
const getCw721TokenResult = await this.graphqlSvc.query(
this.configSvc.get<string>('horosope.endpoint'),
token,
`query QueryCw721Tokens($owner: String = "", $smart_contracts: [String!] = "") {
${this.configSvc.get<string>('horosope.network')} {
cw721_contract(where: {smart_contract: {address: {_in: $smart_contracts}}}) {
id
cw721_tokens(where: {burned: {_eq: false}, owner: {_eq: $owner}}) {
token_id
}
}
}
}`,
'QueryCw721Tokens',
{
smart_contracts: contract_addresses,
owner: walletAddress,
},
);

if (
getCw721TokenResult.data.euphoria.cw721_contract.length > 0 &&
getCw721TokenResult.data.euphoria.cw721_contract[0].cw721_tokens
.length > 0
) {
nft = true;
}

return {
nft,
};
} catch (errors) {
return {
errors,
};
}
}
}

0 comments on commit 87726f6

Please sign in to comment.