Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
13 kB
2
Indexable
Never
import 'package:uniondigital_superapp/super_app/core/data/response/ud_api_response.dart';
import 'package:uniondigital_superapp/super_app/features/retail_account_opening/domain/entities/get_user_profile_entity.dart';
import 'package:uniondigital_superapp/super_app/utils/extensions/json_extensions.dart';

class UserProfileModel extends UserProfileEntity
    implements Serializable<UserProfileModel> {
  UserProfileModel({
    required String userId,
    required String userName,
    required String title,
    required String firstName,
    required String middleName,
    required String lastName,
    required String gender,
    required String placeOfBirth,
    required String countryOfBirth,
    required String maritalStatus,
    required String dateOfBirth,
    required String email,
    required String countryCode,
    required String mobileNumber,
    required bool isCitizen,
    required String nationality,
    required String status,
    required bool isLock,
    required String lockedBy,
    required String lockedAt,
    required String lastLoginAt,
    required bool requireChangePass,
    required bool isPresentAsPermAddress,
    required List<AddressModel> addresses,
    required List<ContactModel> contacts,
  }) : super(
          userId: userId,
          userName: userName,
          title: title,
          firstName: firstName,
          middleName: middleName,
          lastName: lastName,
          gender: gender,
          placeOfBirth: placeOfBirth,
          countryOfBirth: countryOfBirth,
          maritalStatus: maritalStatus,
          dateOfBirth: dateOfBirth,
          email: email,
          countryCode: countryCode,
          mobileNumber: mobileNumber,
          isCitizen: isCitizen,
          nationality: nationality,
          status: status,
          isLock: isLock,
          lockedBy: lockedBy,
          lockedAt: lockedAt,
          lastLoginAt: lastLoginAt,
          requireChangePass: requireChangePass,
          isPresentAsPermAddress: isPresentAsPermAddress,
          addresses: addresses,
          contacts: contacts,
        );

  factory UserProfileModel.empty() {
    return UserProfileModel(
      userId: '',
      userName: '',
      title: '',
      firstName: '',
      middleName: '',
      lastName: '',
      gender: '',
      placeOfBirth: '',
      countryOfBirth: '',
      maritalStatus: '',
      dateOfBirth: '',
      email: '',
      countryCode: '',
      mobileNumber: '',
      isCitizen: false,
      nationality: '',
      status: '',
      isLock: false,
      lockedBy: '',
      lockedAt: '',
      lastLoginAt: '',
      requireChangePass: false,
      isPresentAsPermAddress: false,
      addresses: [],
      contacts: [],
    );
  }

  factory UserProfileModel.fromJson(Map<String, dynamic> json) {
    return UserProfileModel(
      userId: json.toNonNullableString('userId'),
      userName: json.toNonNullableString('userName'),
      title: json.toNonNullableString('title'),
      firstName: json.toNonNullableString('firstName'),
      middleName: json.toNonNullableString('middleName'),
      lastName: json.toNonNullableString('lastName'),
      gender: json.toNonNullableString('gender'),
      placeOfBirth: json.toNonNullableString('placeOfBirth'),
      countryOfBirth: json.toNonNullableString('countryOfBirth'),
      maritalStatus: json.toNonNullableString('maritalStatus'),
      dateOfBirth: json.toNonNullableString('dateOfBirth'),
      email: json.toNonNullableString('email'),
      countryCode: json.toNonNullableString('countryCode'),
      mobileNumber: json.toNonNullableString('mobileNumber'),
      isCitizen: json.toNonNullableBool('isCitizen'),
      nationality: json.toNonNullableString('nationality'),
      status: json.toNonNullableString('status'),
      isLock: json.toNonNullableBool('isLock'),
      lockedBy: json.toNonNullableString('lockedBy'),
      lockedAt: json.toNonNullableString('lockedAt'),
      lastLoginAt: json.toNonNullableString('lastLoginAt'),
      requireChangePass: json.toNonNullableBool('requireChangePass'),
      isPresentAsPermAddress: json.toNonNullableBool('isPresentAsPermAddress'),
      addresses: AddressModel.toList(json.toNonNullableListMap('addresses')),
      contacts: ContactModel.toList(json.toNonNullableListMap('contacts')),
    );
  }

  @override
  UserProfileModel fromJson(Map<String, dynamic> json) {
    print(json.toNonNullableListMap('addresses').toString());
    return UserProfileModel(
      userId: json.toNonNullableString('userId'),
      userName: json.toNonNullableString('userName'),
      title: json.toNonNullableString('title'),
      firstName: json.toNonNullableString('firstName'),
      middleName: json.toNonNullableString('middleName'),
      lastName: json.toNonNullableString('lastName'),
      gender: json.toNonNullableString('gender'),
      placeOfBirth: json.toNonNullableString('placeOfBirth'),
      countryOfBirth: json.toNonNullableString('countryOfBirth'),
      maritalStatus: json.toNonNullableString('maritalStatus'),
      dateOfBirth: json.toNonNullableString('dateOfBirth'),
      email: json.toNonNullableString('email'),
      countryCode: json.toNonNullableString('countryCode'),
      mobileNumber: json.toNonNullableString('mobileNumber'),
      isCitizen: json.toNonNullableBool('isCitizen'),
      nationality: json.toNonNullableString('nationality'),
      status: json.toNonNullableString('status'),
      isLock: json.toNonNullableBool('isLock'),
      lockedBy: json.toNonNullableString('lockedBy'),
      lockedAt: json.toNonNullableString('lockedAt'),
      lastLoginAt: json.toNonNullableString('lastLoginAt'),
      requireChangePass: json.toNonNullableBool('requireChangePass'),
      isPresentAsPermAddress: json.toNonNullableBool('isPresentAsPermAddress'),
      addresses: AddressModel.toList(json.toNonNullableListMap('addresses')),
      contacts: ContactModel.toList(json.toNonNullableListMap('contacts')),
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'userId': userId,
      'userName': userName,
      'title': title,
      'firstName': firstName,
      'middleName': middleName,
      'lastName': lastName,
      'gender': gender,
      'placeOfBirth': placeOfBirth,
      'countryOfBirth': countryOfBirth,
      'maritalStatus': maritalStatus,
      'dateOfBirth': dateOfBirth,
      'email': email,
      'countryCode': countryCode,
      'mobileNumber': mobileNumber,
      'isCitizen': isCitizen,
      'nationality': nationality,
      'status': status,
      'isLock': isLock,
      'lockedBy': lockedBy,
      'lockedAt': lockedAt,
      'lastLoginAt': lastLoginAt,
      'requireChangePass': requireChangePass,
      'isPresentAsPermAddress': isPresentAsPermAddress,
      'addresses': addresses.map((e) => e.toModel().toJson()).toList(),
      'contacts': contacts.map((e) => e.toModel().toJson()).toList(),
    };
  }
}

extension AddressEntityExtensions on AddressEntity {
  AddressModel toModel() {
    return AddressModel(
      addressType: addressType,
      line1: line1,
      line2: line2,
      line3: line3,
      buildingName: buildingName,
      buildingNumber: buildingNumber,
      apartmentName: apartmentName,
      city: city,
      county: county,
      region: region,
      province: province,
      postcode: postcode,
      countryCode: countryCode,
      country: country,
    );
  }
}

class AddressModel extends AddressEntity implements Serializable<AddressModel> {
  AddressModel({
    required String addressType,
    required String line1,
    required String line2,
    required String line3,
    required String buildingName,
    required String buildingNumber,
    required String apartmentName,
    required String city,
    required String county,
    required String region,
    required String province,
    required String postcode,
    required String countryCode,
    required String country,
  }) : super(
          addressType: addressType,
          line1: line1,
          line2: line2,
          line3: line3,
          buildingName: buildingName,
          buildingNumber: buildingNumber,
          apartmentName: apartmentName,
          city: city,
          county: county,
          region: region,
          province: province,
          postcode: postcode,
          countryCode: countryCode,
          country: country,
        );

  factory AddressModel.fromJson(Map<String, dynamic> json) {
    return AddressModel(
      addressType: json.toNonNullableString('addressType'),
      line1: json.toNonNullableString('line1'),
      line2: json.toNonNullableString('line2'),
      line3: json.toNonNullableString('line3'),
      buildingName: json.toNonNullableString('buildingName'),
      buildingNumber: json.toNonNullableString('buildingNumber'),
      apartmentName: json.toNonNullableString('apartmentName'),
      city: json.toNonNullableString('city'),
      county: json.toNonNullableString('county'),
      region: json.toNonNullableString('region'),
      province: json.toNonNullableString('province'),
      postcode: json.toNonNullableString('postcode'),
      countryCode: json.toNonNullableString('countryCode'),
      country: json.toNonNullableString('country'),
    );
  }

  static List<AddressModel> toList(List<Map<String, dynamic>> jsonList) {
    return jsonList.map((json) => AddressModel.fromJson(json)).toList();
  }

  @override
  AddressModel fromJson(Map<String, dynamic> json) {
    return AddressModel(
      addressType: json.toNonNullableString('addressType'),
      line1: json.toNonNullableString('line1'),
      line2: json.toNonNullableString('line2'),
      line3: json.toNonNullableString('line3'),
      buildingName: json.toNonNullableString('buildingName'),
      buildingNumber: json.toNonNullableString('buildingNumber'),
      apartmentName: json.toNonNullableString('apartmentName'),
      city: json.toNonNullableString('city'),
      county: json.toNonNullableString('county'),
      region: json.toNonNullableString('region'),
      province: json.toNonNullableString('province'),
      postcode: json.toNonNullableString('postcode'),
      countryCode: json.toNonNullableString('countryCode'),
      country: json.toNonNullableString('country'),
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'addressType': addressType,
      'line1': line1,
      'line2': line2,
      'line3': line3,
      'buildingName': buildingName,
      'buildingNumber': buildingNumber,
      'city': city,
      'county': county,
      'region': region,
      'province': province,
      'postcode': postcode,
      'countryCode': countryCode,
      'country': country,
    };
  }
}

class ContactModel extends ContactEntity implements Serializable<ContactModel> {
  ContactModel({
    required String id,
    required String userId,
    required String contactType,
    required String contactValue,
    required bool isPrimary,
    required String createdAt,
  }) : super(
          id: id,
          userId: userId,
          contactType: contactType,
          contactValue: contactValue,
          isPrimary: isPrimary,
          createdAt: createdAt,
        );

  factory ContactModel.fromJson(Map<String, dynamic> json) {
    return ContactModel(
      id: json.toNonNullableString('id'),
      userId: json.toNonNullableString('userId'),
      contactType: json.toNonNullableString('contactType'),
      contactValue: json.toNonNullableString('contactValue'),
      isPrimary: json.toNonNullableBool('isPrimary'),
      createdAt: json.toNonNullableString('createdAt'),
    );
  }

  static List<ContactModel> toList(List<Map<String, dynamic>> jsonList) {
    return jsonList.map((json) => ContactModel.fromJson(json)).toList();
  }

  @override
  ContactModel fromJson(Map<String, dynamic> json) {
    return ContactModel(
      id: json.toNonNullableString('id'),
      userId: json.toNonNullableString('userId'),
      contactType: json.toNonNullableString('contactType'),
      contactValue: json.toNonNullableString('contactValue'),
      isPrimary: json.toNonNullableBool('isPrimary'),
      createdAt: json.toNonNullableString('createdAt'),
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'contactType': contactType,
      'contactValue': contactValue,
      'isPrimary': isPrimary,
      'createdAt': createdAt,
    };
  }
}

extension ContactEntityExtensions on ContactEntity {
  ContactModel toModel() {
    return ContactModel(
      id: id,
      userId: userId,
      contactType: contactType,
      contactValue: contactValue,
      isPrimary: isPrimary,
      createdAt: createdAt,
    );
  }
}