Skip to content

Commit

Permalink
Merge pull request #325 from Build-Squad/ENG-242-243-Business-Profile…
Browse files Browse the repository at this point in the history
…-Refactoring

Business profile refactoring
  • Loading branch information
Parikshit85 authored Apr 19, 2024
2 parents 9a7a7be + 7154a6c commit 51a4b37
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 85 deletions.
86 changes: 46 additions & 40 deletions src/api/marketplace/accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,6 @@
from django.shortcuts import get_object_or_404
from django.db.models import Avg


class BusinessAccountMetaDataSerializer(serializers.ModelSerializer):
influencer_ids = serializers.SerializerMethodField(read_only=True)
is_twitter_connected = serializers.SerializerMethodField(read_only=True)
is_wallet_connected = serializers.SerializerMethodField(read_only=True)

class Meta:
model = BusinessAccountMetaData
fields = "__all__"

def get_influencer_ids(self, business_meta_data):
completed_orders = Order.objects.filter(
buyer=business_meta_data.user_account, status="completed"
)
influencer_ids = set()

for completed_order in completed_orders:
completed_order_items = OrderItem.objects.filter(order_id=completed_order)
package_ids = completed_order_items.values_list("package_id", flat=True)
influencers = Package.objects.filter(id__in=package_ids).values_list(
"influencer_id", flat=True
)
influencer_ids.update(influencers)

return list(influencer_ids)

def get_is_twitter_connected(self, business_meta_data):
userAccount = business_meta_data.user_account
if userAccount.twitter_account:
return True
return False


def get_is_wallet_connected(self, business_meta_data):
user_wallet = Wallet.objects.filter(user_id = business_meta_data.user_account)
if(user_wallet):
return True
return False


class CategoryMasterSerializer(serializers.ModelSerializer):
class Meta:
model = CategoryMaster
Expand Down Expand Up @@ -268,6 +228,52 @@ def update(self, instance, validated_data):

return instance

class BusinessAccountMetaDataSerializer(serializers.ModelSerializer):
influencer_ids = serializers.SerializerMethodField(read_only=True)
is_twitter_connected = serializers.SerializerMethodField(read_only=True)
is_wallet_connected = serializers.SerializerMethodField(read_only=True)
user_twitter_profile_image = serializers.SerializerMethodField(read_only=True)
user_account = UserSerializer(read_only=True)

class Meta:
model = BusinessAccountMetaData
fields = "__all__"

def get_influencer_ids(self, business_meta_data):
completed_orders = Order.objects.filter(
buyer=business_meta_data.user_account, status="completed"
)
influencer_ids = set()

for completed_order in completed_orders:
completed_order_items = OrderItem.objects.filter(order_id=completed_order)
package_ids = completed_order_items.values_list("package_id", flat=True)
influencers = Package.objects.filter(id__in=package_ids).values_list(
"influencer_id", flat=True
)
influencer_ids.update(influencers)

return list(influencer_ids)

def get_is_twitter_connected(self, business_meta_data):
userAccount = business_meta_data.user_account
if userAccount.twitter_account:
return True
return False


def get_is_wallet_connected(self, business_meta_data):
user_wallet = Wallet.objects.filter(user_id = business_meta_data.user_account)
if(user_wallet):
return True
return False

def get_user_twitter_profile_image(self, business_meta_data):
userAccount = business_meta_data.user_account
try:
return userAccount.twitter_account.profile_image_url
except Exception as e:
return ""

class TwitterReadSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
40 changes: 13 additions & 27 deletions src/ui/app/business/profile-preview/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Image from "next/image";
import NotFound from "@/public/svg/not_found.svg";
import { stringToColor } from "@/src/utils/helper";
import { BADGES } from "@/src/utils/consts";
import { getProfileCompletedStatus } from "@/src/services/profileCompletion";

type Props = {
params: {
Expand All @@ -48,22 +49,6 @@ const styles = {
},
};

const getProfileCompletedStatus: (businessDetails: any) => string = (
businessDetails
) => {
if (businessDetails) {
let count = 0;
if (businessDetails?.isTwitterAccountConnected) count += 5;
if (businessDetails?.isWalletConnected) count += 5;
count +=
Object.values(businessDetails).filter(
(value) => value !== "" && value !== null
).length - 7;
return `${count} / ${10 + Object.keys(businessDetails).length - 7}`;
}
return "-";
};

const formatTwitterFollowers = (followersCount: any) => {
if (followersCount >= 1000000) {
// Convert to millions format
Expand All @@ -80,8 +65,7 @@ const formatTwitterFollowers = (followersCount: any) => {
export default function BusinessProfilePreview({ params }: Props) {
const [isProfileComplete, setIsProfileComplete] = useState(false);
const [collaborations, setCollaborations] = useState([]);
const [businessDetails, setBusinessDetails] =
useState<BasicBusinessDetailsType>();
const [businessDetails, setBusinessDetails] = useState<any>();
const user = useAppSelector((state) => state.user?.user);

const router = useRouter();
Expand All @@ -104,6 +88,8 @@ export default function BusinessProfilePreview({ params }: Props) {
.replace(/\s/g, "")
.split("/");

console.log(completionStringArr);

return (
(parseInt(completionStringArr[0]) / parseInt(completionStringArr[1])) *
100
Expand Down Expand Up @@ -160,8 +146,6 @@ export default function BusinessProfilePreview({ params }: Props) {
if (isSuccess) {
setBusinessDetails({
...data?.data,
isTwitterAccountConnected: !!user?.twitter_account,
isWalletConnected: !!user?.wallets?.length,
});
getCollaborators(data?.data?.influencer_ids);
}
Expand Down Expand Up @@ -211,23 +195,25 @@ export default function BusinessProfilePreview({ params }: Props) {
alignItems: "center",
}}
>
{user?.twitter_account?.profile_image_url ? (
{!!businessDetails?.user_twitter_profile_image ? (
<Avatar
alt={"Business Account Image"}
src={user?.twitter_account?.profile_image_url}
src={businessDetails?.user_twitter_profile_image}
sx={{
width: 138,
height: 138,
}}
/>
) : (
<Avatar
alt={businessDetails?.business_name}
src={businessDetails?.business_name}
alt={businessDetails?.user_account?.username}
src={businessDetails?.user_account?.username}
sx={{
width: 138,
height: 138,
bgcolor: stringToColor(user?.username ?? ""),
bgcolor: stringToColor(
businessDetails?.user_account?.username ?? ""
),
}}
/>
)}
Expand Down Expand Up @@ -412,7 +398,7 @@ export default function BusinessProfilePreview({ params }: Props) {
margin: "20px",
}}
>
{user?.role?.name === "business_owner" ? (
{user?.id == params?.id ? (
<Button
fullWidth
variant={"contained"}
Expand Down Expand Up @@ -511,7 +497,7 @@ export default function BusinessProfilePreview({ params }: Props) {
<Typography fontWeight={"bold"} sx={{ mt: 2 }}>
{BADGES[getCurrentBadgeIndex()].name}
</Typography>
{user?.role?.name === "business_owner" ? (
{user?.id == params?.id ? (
isProfileComplete ? (
<Typography variant="subtitle1" sx={{ color: "#626262" }}>
Profile complete! Enjoy your upgraded badge & enhanced
Expand Down
20 changes: 2 additions & 18 deletions src/ui/app/influencer/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import Joyride, { ACTIONS, EVENTS, STATUS } from "react-joyride";
import XfluencerLogo from "@/public/svg/Xfluencer_Logo_Beta.svg";
import { DriveEta } from "@mui/icons-material";
import WalletConnectModal from "@/src/components/web3Components/walletConnectModal";
import { getProfileCompletedStatus } from "@/src/services/profileCompletion";

const tabs = [
{
Expand All @@ -72,22 +73,6 @@ const tabs = [
},
];

const getProfileCompletedStatus: (businessDetails: any) => string = (
businessDetails
) => {
if (businessDetails) {
let count = 0;
if (businessDetails?.isTwitterAccountConnected) count += 5;
if (businessDetails?.isWalletConnected) count += 5;
count +=
Object.values(businessDetails).filter(
(value) => value !== "" && value !== null
).length - 7;
return `${count} / ${10 + Object.keys(businessDetails).length - 7}`;
}
return "-";
};

export default function BusinessDashboardPage() {
const router = useRouter();
const searchParams = useSearchParams();
Expand Down Expand Up @@ -640,8 +625,7 @@ export default function BusinessDashboardPage() {
)
.replace(/\s/g, "")
.split("/");

return (
return (
(parseInt(completionStringArr[0]) / parseInt(completionStringArr[1])) *
100
);
Expand Down
37 changes: 37 additions & 0 deletions src/ui/src/services/profileCompletion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const getProfileCompletedStatus = (businessDetails: any): string => {
if (!businessDetails) {
return "-";
}

let count = 0;

const fields = [
{ key: 'is_twitter_connected', weight: 5 },
{ key: 'is_wallet_connected', weight: 5 },
'business_name',
'industry',
'founded',
'headquarters',
'bio',
'user_email',
'phone',
'website',
'twitter_account',
'linked_in',
];

fields.forEach((field) => {
if (typeof field === 'string') {
if (!!businessDetails[field]) {
count++;
}
} else {
if (businessDetails[field.key]) {
count += field.weight;
}
}
});
console.log(count)
return `${count} / 20`;
};

0 comments on commit 51a4b37

Please sign in to comment.