Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated location Param to Address Object param in Organizations (Updated). #1740

Merged
merged 22 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c5ce942
Made changes to the Organization location parameter in both types and…
Anubhav-2003 Jan 8, 2024
1702d25
Added tests for the changes
Anubhav-2003 Jan 10, 2024
19b4192
Changed sample data
Anubhav-2003 Jan 10, 2024
e93673b
Changed data in sample_data
Anubhav-2003 Jan 10, 2024
0efb279
Fixed the failing tests
Anubhav-2003 Jan 12, 2024
11753f0
Merge branch 'develop' into Issue_1650
Anubhav-2003 Jan 13, 2024
d6ff936
Fixed linting issues
Anubhav-2003 Jan 13, 2024
0643cc6
Added extra tests
Anubhav-2003 Jan 13, 2024
a3adebe
Made address compulsory for organizations
Anubhav-2003 Jan 13, 2024
d4b56d2
Removed IsPublic param
Anubhav-2003 Jan 13, 2024
566a846
Merge branch 'develop' into Issue_1650
Anubhav-2003 Jan 14, 2024
d2813b2
Made AddressInput Non-nullable
Anubhav-2003 Jan 14, 2024
3bd0140
Added address to sample organizations
Anubhav-2003 Jan 14, 2024
995ee14
Merge branch 'develop' into Issue_1650
Anubhav-2003 Jan 15, 2024
1757e0b
Updated createSampleOrg utility functions
Anubhav-2003 Jan 20, 2024
aedee07
Changed countryCode type to string
Anubhav-2003 Jan 22, 2024
161ce7c
Updated the inputType for UpdateOrganization Mutation
Anubhav-2003 Jan 23, 2024
e2cbeb8
Restored creatorId for sample organizations
Anubhav-2003 Jan 23, 2024
4be5e62
Made line2,and sortingcode optional in address
Anubhav-2003 Jan 23, 2024
c3a6264
Made countrycode and city mandatory fields, rest optional
Anubhav-2003 Jan 25, 2024
c1ec450
Fixed a bug in tests
Anubhav-2003 Jan 26, 2024
1080e53
Fixed test cases
Anubhav-2003 Jan 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions sample_data/organizations.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
"blockedUsers": [],
"name": "The Unity Foundation",
"description": "A foundation aimed at uniting the world and making it a better place for all.",
"location": "Delhi, India",
"address": {
"city": "Delhi",
"countryCode": "IN",
"dependentLocality": "Some Dependent Locality",
"line1": "123 Random Street",
"line2": "Apartment 456",
"postalCode": "110001",
"sortingCode": "ABC-123",
"state": "Delhi"
},
"userRegistrationRequired": false,
"visibleInSearch": true,
"image": null,
Expand All @@ -36,12 +45,20 @@
"blockedUsers": [],
"name": "Angel Foundation",
"description": "We are aimed at improving the education spaces for the under privileged girl child.",
"location": "Kolkata, West Bengal",
"address": {
"city": "Kolkata",
"countryCode": "IN",
"dependentLocality": "Some Dependent Locality",
"line1": "line1",
"line2": "line2",
"postalCode": "711205",
"sortingCode": "ABC-124",
"state": "West Bengal"
},
"userRegistrationRequired": false,
"visibleInSearch": true,
"image": null,
"creatorId": "64378abd85008f171cf2990d",
"updatedAt": "2023-04-13T05:16:52.827Z",
"createdAt": "2023-04-13T05:16:52.827Z",
"__v": 0
},
Expand All @@ -57,12 +74,20 @@
"blockedUsers": [],
"name": "Hope Foundation",
"description": "A place where money and power doesn't matter. Free legal aid to everyone.",
"location": "Mountain View, CA",
"address": {
"city": "Mountain View",
"countryCode": "US",
"dependentLocality": "Some Dependent Locality",
"line1": "123 Main Street",
"line2": "Apt 456",
"postalCode": "94040",
"sortingCode": "XYZ-789",
"state": "CA"
},
"userRegistrationRequired": false,
"visibleInSearch": true,
"image": null,
"creatorId": "64378abd85008f171cf2990d",
"updatedAt": "2023-04-13T05:16:52.827Z",
"createdAt": "2023-04-13T05:16:52.827Z",
"__v": 0
},
Expand All @@ -78,12 +103,20 @@
"blockedUsers": [],
"name": "Dignity Foundation",
"description": "A foundation aimed at improving the lives of old age citizens.",
"location": "Brooklyn, NYC",
"address": {
"city": "Brooklyn",
"countryCode": "US",
"dependentLocality": "Sample Dependent Locality",
"line1": "123 Main Street",
"line2": "Apt 456",
"postalCode": "11234",
"sortingCode": "ABC-789",
"state": "NY"
},
"userRegistrationRequired": false,
"visibleInSearch": true,
"image": null,
"creatorId": "64378abd85008f171cf2990d",
"updatedAt": "2023-04-13T05:16:52.827Z",
"createdAt": "2023-04-13T05:16:52.827Z",
"__v": 0
}
Expand Down
10 changes: 5 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ directive @role(requires: UserType) on FIELD_DEFINITION

type Address {
city: String
countryCode: CountryCode
countryCode: String
dependentLocality: String
line1: String
line2: String
Expand All @@ -15,7 +15,7 @@ type Address {

input AddressInput {
city: String
countryCode: CountryCode
countryCode: String
dependentLocality: String
line1: String
line2: String
Expand Down Expand Up @@ -570,6 +570,7 @@ input OTPInput {

type Organization {
_id: ID!
address: Address
admins(adminId: ID): [User!]
apiUrl: URL!
blockedUsers: [User]
Expand All @@ -578,7 +579,6 @@ type Organization {
customFields: [OrganizationCustomField!]!
description: String!
image: String
location: String
members: [User]
membershipRequests: [MembershipRequest]
name: String!
Expand Down Expand Up @@ -608,11 +608,11 @@ type OrganizationInfoNode {
}

input OrganizationInput {
address: AddressInput!
apiUrl: URL
attendees: String
description: String!
image: String
location: String
name: String!
userRegistrationRequired: Boolean
visibleInSearch: Boolean
Expand Down Expand Up @@ -921,8 +921,8 @@ input UpdateEventInput {
}

input UpdateOrganizationInput {
address: AddressInput
description: String
location: String
name: String
userRegistrationRequired: Boolean
visibleInSearch: Boolean
Expand Down
41 changes: 36 additions & 5 deletions src/models/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ export interface InterfaceOrganization {
image: string | undefined;
name: string;
description: string;
location: string | undefined;
address: {
city: string;
countryCode: string;
dependentLocality: string;
line1: string;
line2: string;
postalCode: string;
sortingCode: string;
state: string;
};
creatorId: PopulatedDoc<InterfaceUser & Document>;
status: string;
members: PopulatedDoc<InterfaceUser & Document>[];
Expand All @@ -36,7 +45,7 @@ export interface InterfaceOrganization {
* @param image - Organization image URL.
* @param name - Organization name.
* @param description - Organization description.
* @param location - Organization location.
* @param address - Organization address, stored as an object.
* @param creatorId - Organization creator, referring to `User` model.
* @param status - Status.
* @param members - Collection of members, each object refer to `User` model.
Expand All @@ -49,7 +58,6 @@ export interface InterfaceOrganization {
* @param createdAt - Time stamp of data creation.
* @param updatedAt - Time stamp of data updation.
*/

const organizationSchema = new Schema(
{
apiUrl: {
Expand All @@ -66,8 +74,31 @@ const organizationSchema = new Schema(
type: String,
required: true,
},
location: {
type: String,
address: {
Copy link
Contributor

@xoldd xoldd Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirm which fields are non nullable and add required: true to them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. City and country code should be mandatory for the purposes of this PR
  2. As country code is unchanging it should be an enum and added to this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sir, I will make those two mandatory. As for the enums, I have already hardcoded an entire list of objects with country codes and country names in the admin portal for the drop down list. I found that creating an object would be more self-explanatory and better as compared to just an enum. you can find that change in the other PR. Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@palisadoes @xoldyckk Respected sir, I have made the changes requested, and made countrycode, and city mandatory. But due to this change. Some tests are failing. I will be fixing those tests as early as possible.

Thank You.

city: {
type: String,
},
countryCode: {
type: String,
},
dependentLocality: {
type: String,
},
line1: {
type: String,
},
line2: {
type: String,
},
postalCode: {
type: String,
},
sortingCode: {
type: String,
},
state: {
type: String,
},
},
userRegistrationRequired: {
type: Boolean,
Expand Down
96 changes: 83 additions & 13 deletions src/resolvers/Mutation/createOrganization.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import "dotenv/config";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import type {
MutationResolvers,
Address,
} from "../../types/generatedGraphQLTypes";
import { User, Organization } from "../../models";
import { errors, requestContext } from "../../libraries";
import { LENGTH_VALIDATION_ERROR } from "../../constants";
Expand Down Expand Up @@ -39,14 +42,17 @@
let validationResultDescription = {
isLessThanMaxLength: false,
};
let validationResultLocation = {
isLessThanMaxLength: false,
let validationResultAddress = {
isAddressValid: false,
};

if (args.data?.name && args.data?.description && args.data?.location) {
if (args.data?.name && args.data?.description) {
validationResultName = isValidString(args.data?.name, 256);
validationResultDescription = isValidString(args.data?.description, 500);
validationResultLocation = isValidString(args.data?.location, 50);
}

if (args.data?.address) {
validationResultAddress = validateAddress(args.data?.address);
}

if (!validationResultName.isLessThanMaxLength) {
Expand All @@ -65,25 +71,21 @@
LENGTH_VALIDATION_ERROR.CODE
);
}
if (!validationResultLocation.isLessThanMaxLength) {
throw new errors.InputValidationError(
requestContext.translate(
`${LENGTH_VALIDATION_ERROR.MESSAGE} 50 characters in location`
),
LENGTH_VALIDATION_ERROR.CODE
);
if (!validationResultAddress.isAddressValid) {
throw new errors.InputValidationError("Not a Valid Address");
}

// Creates new organization.
const createdOrganization = await Organization.create({
...args.data,
address: args.data?.address,
image: uploadImageFileName ? uploadImageFileName : null,
creatorId: context.userId,
admins: [context.userId],
members: [context.userId],
});

await cacheOrganizations([createdOrganization.toObject()!]);
await cacheOrganizations([createdOrganization.toObject()]);

/*
Adds createdOrganization._id to joinedOrganizations, createdOrganizations
Expand All @@ -105,3 +107,71 @@
// Returns createdOrganization.
return createdOrganization.toObject();
};
/**
* Validates an address object to ensure its fields meet specified criteria.
* @param address - The address object to validate
* @returns An object containing the validation result: isAddressValid (true if the address is valid, false otherwise)
*/
function validateAddress(address: Address | undefined): {
isAddressValid: boolean;
} {
if (!address) {
return { isAddressValid: false };
}

Check warning on line 121 in src/resolvers/Mutation/createOrganization.ts

View check run for this annotation

Codecov / codecov/patch

src/resolvers/Mutation/createOrganization.ts#L119-L121

Added lines #L119 - L121 were not covered by tests
const {
city,
countryCode,
dependentLocality,
line1,
line2,
postalCode,
sortingCode,
state,
} = address;

// Mandatory: It should be a valid country code.
const isCountryCodeValid = !!countryCode && countryCode.length >= 2;

// Mandatory: It should exist and have a length greater than 0
const isCityValid = !!city && city.length > 0;

// Optional: It should exist and have a length greater than 0
const isDependentLocalityValid =
dependentLocality === undefined ||
(typeof dependentLocality === "string" && dependentLocality.length >= 0);

// Optional: Line 1 should exist and have a length greater than 0
const isLine1Valid =
line1 === undefined || (typeof line1 === "string" && line1.length >= 0);

// Optional: Line 2 should exist and have a length greater than 0, if provided
const isLine2Valid =
line2 === undefined || (typeof line2 === "string" && line2.length >= 0);

// Optional: It should exist and have a valid format.
const isPostalCodeValid =
postalCode === undefined ||
(typeof postalCode === "string" && /^\d*$/.test(postalCode));

// Optional: It should exist and have a length greater than 0, if provided
const isSortingCodeValid =
sortingCode === undefined ||
(typeof sortingCode === "string" && sortingCode.length >= 0);

// Optional: It should exist and have a length greater than 0, if provided
const isStateValid =
state === undefined || (typeof state === "string" && state.length >= 0);

const isAddressValid =
isCityValid &&
isCountryCodeValid &&
isDependentLocalityValid &&
isLine1Valid &&
isLine2Valid &&
isPostalCodeValid &&
isSortingCodeValid &&
isStateValid;

return { isAddressValid: isAddressValid };
}
6 changes: 3 additions & 3 deletions src/typeDefs/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const inputs = gql`
input OrganizationInput {
name: String!
description: String!
location: String
address: AddressInput!
attendees: String
apiUrl: URL
image: String
Expand Down Expand Up @@ -280,7 +280,7 @@ export const inputs = gql`
input UpdateOrganizationInput {
name: String
description: String
location: String
address: AddressInput
userRegistrationRequired: Boolean
visibleInSearch: Boolean
}
Expand All @@ -292,7 +292,7 @@ export const inputs = gql`

input AddressInput {
city: String
countryCode: CountryCode
countryCode: String
dependentLocality: String
line1: String
line2: String
Expand Down
4 changes: 2 additions & 2 deletions src/typeDefs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export const types = gql`
_id: ID!
name: String!
description: String!
location: String
address: Address
creator: User
createdAt: DateTime!
updatedAt: DateTime!
Expand Down Expand Up @@ -347,7 +347,7 @@ export const types = gql`

type Address {
city: String
countryCode: CountryCode
countryCode: String
dependentLocality: String
line1: String
line2: String
Expand Down
Loading
Loading