mnemo_cards/mnemo_cards_backend/lib/packs/pack_dto_converter.dart
2025-11-22 22:06:27 +03:00

230 lines
7.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:injectable/injectable.dart';
import 'package:mnemo_cards_backend/api/ads/ads_manager.dart';
import 'package:mnemo_cards_backend/extensions.dart';
import 'package:mnemo_cards_backend/promo_codes/promo_codes_manager.dart';
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
import 'package:mnemo_cards_backend/packs/card_model_extension.dart';
import 'package:mnemo_cards_backend/packs/pack_manager.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import 'products_price_resolver.dart';
bool canOpenForAd(int? id) => id != null;
@LazySingleton()
class PackDtoConverter {
final ProductsPriceResolver _productsPriceResolver;
final AdsManager _adsManager;
const PackDtoConverter(
this._productsPriceResolver,
this._adsManager,
);
Future<CardPackPreviewDto> toCardPackPreviewDto(
CardPackModel model,
UserModel? userModel,
) async {
bool available = model.users.contains(userModel);
if (userModel != null && !available) {
await userModel.subscriptionModel.load();
available = userModel.subscriptionModel.value?.features
.contains(SubscriptionFeatureEnum.packs) ==
true;
}
String? price = model.price;
if (userModel != null && !available) {
price = await _productsPriceResolver.userPackPrice(userModel, model);
}
final canOpenForAdVal = !available && canOpenForAd(model.id);
return CardPackPreviewDto(
id: model.id.toString(),
title: model.title,
subtitle: model.subtitle,
cards: model.cards.length,
tests: model.tests.length,
price: price,
imageBase64: await model.cover?.smallBase64Image,
color: model.color,
isAvailable: available,
// trail: 'asset:icons/gift.png',
tip: available
? null
: canOpenForAdVal
? AssetPackTip(
'asset:icons/ad.webp',
position: PackTipPosition.bottomRight,
)
: AssetPackTip('asset:icons/lock.webp',
position: PackTipPosition.bottomRight,),
version: model.version,
);
}
Future<CardPackBuyDto> toCardPackBuyDto(
CardPackModel model,
UserModel? userModel,
) async {
final price = userModel == null
? model.price
: await _productsPriceResolver.userPackPrice(userModel, model);
final canOpenForAdVal = userModel != null && canOpenForAd(model.id);
String? _adsKey;
if (canOpenForAdVal) {
_adsKey = _adsManager.getAdsKey([model], userModel);
}
return CardPackBuyDto(
id: model.id.toString(),
title: model.title,
subtitle: model.subtitle,
appStoreId: model.appStoreId,
rustoreId: model.rustoreId,
googlePlayId: model.googlePlayId,
cards: await Future.wait(
(model.previewCards.isNotEmpty
? model.previewCards
: model.cards.take(3))
.toDtosList(model.cardsOrder)
.map(
(dto) async => dto.copyWith(
image: await dto.image?.smallBase64Image,
),
),
),
items: [
if (model.description != null)
TextItem(
title: model.description,
),
if (_adsKey == null) ...[
SpacerItem(),
TextItem(
subtitle: 'Открыть все темы с подпиской!',
deeplink: SubscriptionOpenDeeplink().uri.toString(),
),
],
if (_adsKey != null) ...[
SpacerItem(),
TextItem(
subtitle: 'Эту тему можно открыть, посмотрев рекламу',
deeplink: ProductForAdDeeplink(
model.toDto(),
_adsKey,
).uri.toString(),
),
],
SpacerItem(
flex: 0,
height: 10,
),
if (_adsKey == null) ...[
TextItem(
subtitle: 'Оплатить российской картой',
deeplink: PurchaseInitDeeplink(
id: model.id.toString(),
productType: MnemoCardsProductType.pack,
paymentId: model.id.toString(),
paymentSystem: PaymentSystem.yookassa,
).uri.toString(),
),
],
if (model.googlePlayId != null)
ButtonItem(
height: 110,
children: [
TextItem(
title: 'Оплатить через',
subtitle: 'Google Pay',
)
],
deeplink: PurchaseInitDeeplink(
id: model.id.toString(),
productType: MnemoCardsProductType.pack,
paymentId: model.googlePlayId!,
paymentSystem: PaymentSystem.google,
).uri.toString(),
)
else
SpacerItem(height: 20, flex: 0)
],
color: model.color,
version: model.version,
price: price,
);
}
Future<EditCardPackDto> toEditDto(
CardPackModel model, {
bool withCards = true,
bool withTests = false,
bool withUsers = false,
}) async {
await model.cards.load();
await model.tests.load();
await model.previewCards.load();
return EditCardPackDto(
id: model.id.toString(),
title: model.title,
subtitle: model.subtitle,
color: model.color,
version: model.version,
cover: await model.cover?.base64Image,
size: model.size,
googlePlayId: model.googlePlayId,
rustoreId: model.rustoreId,
appStoreId: model.appStoreId,
price: model.price,
description: model.description,
enabled: model.enabled,
addCardIds:
withCards ? model.cards.map((c) => c.id.toString()).toList() : null,
addTestIds:
withTests ? model.tests.map((t) => t.id.toString()).toList() : null,
removeCardIds: null,
removeTestIds: null,
previewCards: model.previewCards
.map((card) => card.id?.toString())
.whereNotNull()
.toList(),
order: model.order,
cardsOrder: model.cardsOrder,
);
}
CardPackDto toDto(CardPackModel model) {
return CardPackDto(
id: model.id.toString(),
title: model.title,
subtitle: model.subtitle,
cards: model.cards.toDtosList(model.cardsOrder),
color: model.color,
version: model.version,
);
}
CardPackModel toModel(EditCardPackDto dto, CardPackModel? model) {
return CardPackModel(
title: dto.title ?? model?.title ?? '',
subtitle: dto.subtitle ?? model?.subtitle ?? '',
size: dto.size ?? model?.size ?? 0,
color: dto.color ?? model?.color,
cover: dto.cover ?? model?.cover,
description: dto.description ?? model?.description,
version: dto.version ?? model?.version,
id: model?.id == null ? int.tryParse(dto.id ?? '') : model!.id,
googlePlayId: dto.googlePlayId ?? model?.googlePlayId,
rustoreId: dto.rustoreId ?? model?.rustoreId,
appStoreId: dto.appStoreId ?? model?.appStoreId,
price: dto.price ?? model?.price,
enabled: dto.enabled ?? model?.enabled ?? false,
order: dto.order ?? model?.order ?? 0,
cardsOrder: dto.cardsOrder ?? model?.cardsOrder ?? const [],
);
}
}