Skip to content

Commit

Permalink
Set project on records
Browse files Browse the repository at this point in the history
Fixes

Fixes

Fixes

Fixes

Fixes

Fixes

Fixes

Fixes
  • Loading branch information
Boldizsar Mezei committed Sep 25, 2023
1 parent 3ce9ff7 commit c411af9
Show file tree
Hide file tree
Showing 115 changed files with 998 additions and 338 deletions.
1 change: 1 addition & 0 deletions packages/functions/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
testEnvironment: 'node',
testTimeout: 900000,
globalSetup: './test/set-up.ts',
globalTeardown: './test/teardown.ts',
reporters: ['default', 'github-actions'],
};
2 changes: 1 addition & 1 deletion packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"export-online-test-credentials": "export GOOGLE_APPLICATION_CREDENTIALS=\"./test-service-account-key.json\"",
"test": "export LOCAL_TEST=true && jest packages/functions/test/auth.spec.ts --runInBand",
"test": "export LOCAL_TEST=true && jest --runInBand",
"test-online": "npm run export-online-test-credentials && jest test/ --runInBand",
"test-tangle": "export LOCAL_TEST=true && jest test-tangle/ --runInBand ",
"test-tangle-online": "npm run export-online-test-credentials && jest test-tangle/ --runInBand",
Expand Down
2 changes: 2 additions & 0 deletions packages/functions/scripts/manualRefund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ db.collection(COL.MEMBER)
.collection(COL.TRANSACTION)
.doc(tranId)
.set(<Transaction>{
project: '',
projects: {},
type: TransactionType.CREDIT,
uid: tranId,
space: '',
Expand Down
4 changes: 2 additions & 2 deletions packages/functions/src/controls/address.control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createAddressValidationOrder } from '../services/payment/tangle-service
import { invalidArgument } from '../utils/error.utils';

export const validateAddressControl = async (
{ owner }: Context,
{ project, owner }: Context,
params: AddressValidationRequest,
) => {
const network = (params.network as Network) || DEFAULT_NETWORK;
Expand All @@ -22,7 +22,7 @@ export const validateAddressControl = async (
throw invalidArgument(WenError.member_does_not_exists);
}

const order = await createAddressValidationOrder(member.uid, network, params.space);
const order = await createAddressValidationOrder(project, member.uid, network, params.space);
await build5Db().doc(`${COL.TRANSACTION}/${order.uid}`).create(order);

return order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Context } from '../../runtime/firebase/common';
import { approveAwardParticipant } from '../../services/payment/tangle-service/award/award.approve.participant.service';

export const approveAwardParticipantControl = async (
{ owner }: Context,
{ project, owner }: Context,
params: AwardApproveParticipantRequest,
): Promise<AwardApproveParticipantResponse> => {
const members = params.members.map((m) => m.toLowerCase());
Expand All @@ -21,7 +21,7 @@ export const approveAwardParticipantControl = async (
for (const member of members) {
try {
const badge = await build5Db().runTransaction(
approveAwardParticipant(owner, awardId, member),
approveAwardParticipant(project, owner, awardId, member),
);
badges[badge.uid] = badge;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
7 changes: 5 additions & 2 deletions packages/functions/src/controls/award/award.create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { Award, AwardCreateRequest, COL, SUB_COL } from '@build-5/interfaces';
import { Context } from '../../runtime/firebase/common';
import { createAward } from '../../services/payment/tangle-service/award/award.create.service';

export const createAwardControl = async ({ owner }: Context, params: AwardCreateRequest) => {
const { owner: awardOwner, award } = await createAward(owner, params);
export const createAwardControl = async (
{ project, owner }: Context,
params: AwardCreateRequest,
) => {
const { owner: awardOwner, award } = await createAward(project, owner, params);

const batch = build5Db().batch();

Expand Down
9 changes: 3 additions & 6 deletions packages/functions/src/controls/award/award.fund.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { build5Db } from '@build-5/database';
import { AwardFundRequest, COL, Transaction } from '@build-5/interfaces';
import { AwardFundRequest, COL } from '@build-5/interfaces';
import { Context } from '../../runtime/firebase/common';
import {
createAwardFundOrder,
getAwardForFunding,
} from '../../services/payment/tangle-service/award/award.fund.service';

export const fundAwardControl = async (
{ owner }: Context,
params: AwardFundRequest,
): Promise<Transaction> => {
export const fundAwardControl = async ({ project, owner }: Context, params: AwardFundRequest) => {
const award = await getAwardForFunding(owner, params.uid);
const order = await createAwardFundOrder(owner, award);
const order = await createAwardFundOrder(project, owner, award);

await build5Db().doc(`${COL.TRANSACTION}/${order.uid}`).create(order);

Expand Down
7 changes: 5 additions & 2 deletions packages/functions/src/controls/award/award.owner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { build5Db } from '@build-5/database';
import {
Award,
AwardAddOwnerRequest,
Expand All @@ -8,12 +9,12 @@ import {
} from '@build-5/interfaces';
import dayjs from 'dayjs';
import { Context } from '../../runtime/firebase/common';
import { getProjects } from '../../utils/common.utils';
import { dateToTimestamp } from '../../utils/dateTime.utils';
import { invalidArgument } from '../../utils/error.utils';
import { build5Db } from '@build-5/database';

export const addOwnerControl = async (
{ owner }: Context,
{ project, owner }: Context,
params: AwardAddOwnerRequest,
): Promise<AwardOwner> => {
const awardDocRef = build5Db().doc(`${COL.AWARD}/${params.uid}`);
Expand All @@ -33,6 +34,8 @@ export const addOwnerControl = async (
}

const newOwner: AwardOwner = {
project,
projects: getProjects([award], project),
uid: params.member,
parentId: award.uid,
parentCol: COL.AWARD,
Expand Down
5 changes: 4 additions & 1 deletion packages/functions/src/controls/award/award.participate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
} from '@build-5/interfaces';
import dayjs from 'dayjs';
import { Context } from '../../runtime/firebase/common';
import { getProjects } from '../../utils/common.utils';
import { dateToTimestamp } from '../../utils/dateTime.utils';
import { invalidArgument } from '../../utils/error.utils';

export const awardParticipateControl = async (
{ owner }: Context,
{ project, owner }: Context,
params: AwardParticpateRequest,
): Promise<AwardParticipant> => {
const awardDocRef = build5Db().doc(`${COL.AWARD}/${params.uid}`);
Expand Down Expand Up @@ -41,6 +42,8 @@ export const awardParticipateControl = async (

const participant: AwardParticipant = {
uid: owner,
project,
projects: getProjects([award], project),
comment: params.comment || null,
parentId: award.uid,
completed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
createNftOutput,
nftToMetadata,
} from '../../utils/collection-minting-utils/nft.utils';
import { getProjects } from '../../utils/common.utils';
import { isProdEnv } from '../../utils/config.utils';
import { dateToTimestamp } from '../../utils/dateTime.utils';
import { invalidArgument } from '../../utils/error.utils';
Expand All @@ -38,7 +39,7 @@ import { assertIsGuardian } from '../../utils/token.utils';
import { getRandomEthAddress } from '../../utils/wallet.utils';

export const mintCollectionOrderControl = async (
{ owner }: Context,
{ project, owner }: Context,
params: CollectionMintRequest,
) => {
const network = params.network as Network;
Expand Down Expand Up @@ -106,6 +107,8 @@ export const mintCollectionOrderControl = async (
const aliasStorageDeposit = Number(createAliasOutput(targetAddress, wallet.info).amount);

const order: Transaction = {
project,
projects: getProjects([collection], project),
type: TransactionType.ORDER,
uid: getRandomEthAddress(),
member: owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { Context } from '../../runtime/firebase/common';
import { hasStakedTokens } from '../../services/stake.service';
import { assertSpaceHasValidAddress } from '../../utils/address.utils';
import { getProjects } from '../../utils/common.utils';
import { dateToTimestamp, serverTime } from '../../utils/dateTime.utils';
import { invalidArgument } from '../../utils/error.utils';
import { getRandomEthAddress } from '../../utils/wallet.utils';
Expand Down Expand Up @@ -57,6 +58,8 @@ export const createCollectionControl = async (
const placeholderNftId = params.type !== CollectionType.CLASSIC ? getRandomEthAddress() : null;
const collection = {
...params,
project,
projects: getProjects([], project),
discounts: await populateTokenUidOnDiscounts(discounts),
uid: getRandomEthAddress(),
total: 0,
Expand All @@ -75,6 +78,8 @@ export const createCollectionControl = async (

if (placeholderNftId) {
const placeholderNft = {
project,
projects: getProjects([collection], project),
uid: placeholderNftId,
name: params.name,
description: params.description,
Expand Down
10 changes: 7 additions & 3 deletions packages/functions/src/controls/credit/credit.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import {
import dayjs from 'dayjs';
import { Context } from '../../runtime/firebase/common';
import { WalletService } from '../../services/wallet/wallet';
import { getProjects } from '../../utils/common.utils';
import { dateToTimestamp } from '../../utils/dateTime.utils';
import { invalidArgument } from '../../utils/error.utils';
import { getRandomEthAddress } from '../../utils/wallet.utils';

export const creditUnrefundableControl = (
{ owner }: Context,
{ project, owner }: Context,
params: CreditUnrefundableRequest,
): Promise<Transaction> =>
) =>
build5Db().runTransaction(async (transaction) => {
const transactionDocRef = build5Db().doc(`${COL.TRANSACTION}/${params.transaction}`);
const creditTransaction = await transaction.get<Transaction>(transactionDocRef);
Expand All @@ -41,18 +42,21 @@ export const creditUnrefundableControl = (
const wallet = await WalletService.newWallet(creditTransaction.network);
const targetAddress = await wallet.getNewIotaAddressDetails();

const creditOrder = createCreditOrder(creditTransaction, owner, targetAddress.bech32);
const creditOrder = createCreditOrder(project, creditTransaction, owner, targetAddress.bech32);
const creditDocRef = build5Db().doc(`${COL.TRANSACTION}/${creditOrder.uid}`);
transaction.create(creditDocRef, creditOrder);

return creditOrder;
});

const createCreditOrder = (
project: string,
creditTtransaction: Transaction,
owner: string,
targetAddress: NetworkAddress,
): Transaction => ({
project: project,
projects: getProjects([creditTtransaction], project),
type: TransactionType.ORDER,
uid: getRandomEthAddress(),
member: owner,
Expand Down
9 changes: 3 additions & 6 deletions packages/functions/src/controls/nft/nft.bid.control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import { Context } from '../../runtime/firebase/common';
import { createNftBidOrder } from '../../services/payment/tangle-service/nft/nft-bid.service';
import { invalidArgument } from '../../utils/error.utils';

export const nftBidControl = async (
{ owner, ip }: Context,
params: NftBidRequest,
): Promise<Transaction> => {
export const nftBidControl = async ({ project, owner, ip }: Context, params: NftBidRequest) => {
const memberDocRef = build5Db().doc(`${COL.MEMBER}/${owner}`);
const member = await memberDocRef.get();
if (!member) {
throw invalidArgument(WenError.member_does_not_exists);
}

const bidTransaction = await createNftBidOrder(params.nft, owner, ip || '');
const bidTransaction = await createNftBidOrder(project, params.nft, owner, ip || '');

const transactionDocRef = build5Db().doc(`${COL.TRANSACTION}/${bidTransaction.uid}`);
await transactionDocRef.create(bidTransaction);

return (await transactionDocRef.get())!;
return (await transactionDocRef.get<Transaction>())!;
};
12 changes: 8 additions & 4 deletions packages/functions/src/controls/nft/nft.create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import {
import dayjs from 'dayjs';
import { isEmpty } from 'lodash';
import { Context } from '../../runtime/firebase/common';
import { getProjects } from '../../utils/common.utils';
import { dateToTimestamp } from '../../utils/dateTime.utils';
import { invalidArgument } from '../../utils/error.utils';
import { getRandomEthAddress } from '../../utils/wallet.utils';

export const createNftControl = async (
{ owner }: Context,
{ project, owner }: Context,
params: NftCreateRequest,
): Promise<Nft> => {
const collection = await getCollection(owner, params.collection as string);
return await processOneCreateNft(params, collection, collection.total + 1);
return await processOneCreateNft(project, params, collection, collection.total + 1);
};

export const createBatchNftControl = async (
{ owner }: Context,
{ project, owner }: Context,
params: NftCreateRequest[],
): Promise<string[]> => {
const collection = await getCollection(owner, params[0].collection);
const promises = params.map((param, i) =>
processOneCreateNft(param, collection, collection.total + i + 1),
processOneCreateNft(project, param, collection, collection.total + i + 1),
);
return (await Promise.all(promises)).map((n) => n.uid);
};
Expand Down Expand Up @@ -63,6 +64,7 @@ const getCollection = async (owner: string, collectionId: string) => {
};

const processOneCreateNft = async (
project: string,
params: NftCreateRequest,
collection: Collection,
position: number,
Expand Down Expand Up @@ -100,6 +102,8 @@ const processOneCreateNft = async (
const price = Math.max(Number(params.price) || 0, MIN_IOTA_AMOUNT);
const nft = {
...params,
project,
projects: getProjects([], project),
uid: getRandomEthAddress(),
locked: false,
price,
Expand Down
8 changes: 4 additions & 4 deletions packages/functions/src/controls/nft/nft.deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import {
import dayjs from 'dayjs';
import { Context } from '../../runtime/firebase/common';
import { WalletService } from '../../services/wallet/wallet';
import { getProjects } from '../../utils/common.utils';
import { dateToTimestamp } from '../../utils/dateTime.utils';
import { getRandomEthAddress } from '../../utils/wallet.utils';

export const depositNftControl = async (
{ owner }: Context,
params: NftDepositRequest,
): Promise<Transaction> => {
export const depositNftControl = async ({ project, owner }: Context, params: NftDepositRequest) => {
const network = params.network as Network;
const wallet = await WalletService.newWallet(network);
const targetAddress = await wallet.getNewIotaAddressDetails();

const order: Transaction = {
project,
projects: getProjects([], project),
type: TransactionType.ORDER,
uid: getRandomEthAddress(),
member: owner,
Expand Down
12 changes: 9 additions & 3 deletions packages/functions/src/controls/nft/nft.puchase.control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { Context } from '../../runtime/firebase/common';
import { createNftPuchaseOrder } from '../../services/payment/tangle-service/nft/nft-purchase.service';

export const orderNftControl = async (
{ owner, ip }: Context,
{ project, owner, ip }: Context,
params: NftPurchaseRequest,
): Promise<Transaction> => {
const order = await createNftPuchaseOrder(params.collection, params.nft, owner, ip || '');
) => {
const order = await createNftPuchaseOrder(
project,
params.collection,
params.nft,
owner,
ip || '',
);
const orderDocRef = build5Db().doc(`${COL.TRANSACTION}/${order.uid}`);
await orderDocRef.create(order);

Expand Down
8 changes: 3 additions & 5 deletions packages/functions/src/controls/nft/nft.stake.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { build5Db } from '@build-5/database';
import { COL, Network, NftStakeRequest, StakeType, Transaction } from '@build-5/interfaces';
import { COL, Network, NftStakeRequest, StakeType } from '@build-5/interfaces';
import { Context } from '../../runtime/firebase/common';
import { createNftStakeOrder } from '../../services/payment/nft/nft-stake.service';

export const nftStakeControl = async (
{ owner }: Context,
params: NftStakeRequest,
): Promise<Transaction> => {
export const nftStakeControl = async ({ project, owner }: Context, params: NftStakeRequest) => {
const order = await createNftStakeOrder(
project,
owner,
params.network as Network,
params.weeks,
Expand Down
3 changes: 2 additions & 1 deletion packages/functions/src/controls/nft/nft.withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createNftWithdrawOrder } from '../../services/payment/tangle-service/nf
import { assertMemberHasValidAddress, getAddress } from '../../utils/address.utils';
import { invalidArgument } from '../../utils/error.utils';

export const withdrawNftControl = async ({ owner }: Context, params: NftWithdrawRequest) =>
export const withdrawNftControl = async ({ project, owner }: Context, params: NftWithdrawRequest) =>
build5Db().runTransaction(async (transaction) => {
const nftDocRef = build5Db().doc(`${COL.NFT}/${params.nft}`);
const nft = await transaction.get<Nft>(nftDocRef);
Expand Down Expand Up @@ -49,6 +49,7 @@ export const withdrawNftControl = async ({ owner }: Context, params: NftWithdraw
assertMemberHasValidAddress(member, nft.mintingData?.network!);

const { order, nftUpdateData } = createNftWithdrawOrder(
project,
nft,
member!.uid,
getAddress(member, nft.mintingData?.network!),
Expand Down
2 changes: 2 additions & 0 deletions packages/functions/src/controls/project/init.soon.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const initSoonProject = functions.https.onRequest(async () => {

const guardianDocRef = projectDocRef.collection(SUB_COL.GUARDIANS).doc(GUARDIAN_ID);
const guardian: ProjectGuardian = {
project: SOON_PROJECT_ID,
projects: { [SOON_PROJECT_ID]: true },
uid: GUARDIAN_ID,
createdOn: dateToTimestamp(dayjs()),
parentCol: COL.PROJECT,
Expand Down
Loading

0 comments on commit c411af9

Please sign in to comment.