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

Fix(mgmt_api) :Newly created companies should be assigned Canada as their country in the user profile [CPCN-385]] #139

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
75 changes: 48 additions & 27 deletions server/cp/mgmt_api/companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@

def init_app(app):
CompaniesResource.internal_resource = False
superdesk.register_resource('companies', CompaniesResource, CPCompaniesService, _app=app)
superdesk.register_resource('company_products', CompanyProductsResource, CompanyProductsService, _app=app)
superdesk.register_resource(
"companies", CPCompaniesResource, CPCompaniesService, _app=app
)
superdesk.register_resource(
"company_products", CompanyProductsResource, CompanyProductsService, _app=app
)


class CPCompaniesResource(CompaniesResource):
"""
CP Companies Schema
"""

schema = {
**CompaniesResource.schema,
"country": {"type": "string", "default": "CAN"},
}


class CPCompaniesService(CompaniesService):
Expand All @@ -23,46 +38,44 @@ def on_create(self, docs):
for doc in docs:
errors = get_errors_company(doc)
if errors:
message = ("invalid ip address")
raise SuperdeskApiError.badRequestError(message=message, payload=message)
message = "invalid ip address"
raise SuperdeskApiError.badRequestError(
message=message, payload=message
)

def on_created(self, docs):
super().on_created(docs)
for doc in docs:
app.cache.set(str(doc['_id']), doc)
app.cache.set(str(doc["_id"]), doc)

def on_update(self, updates, original):
super().on_update(updates, original)
app.cache.delete(str(original['_id']))
app.cache.delete(str(original["_id"]))

def on_delete(self, doc):
super().on_delete(doc)
app.cache.delete(str(doc['_id']))
app.cache.delete(str(doc["_id"]))


class CompanyProductsResource(newsroom.Resource):
endpoint_name = "company-product"
url = 'companies/<regex("[a-f0-9]{24}"):companies>/products'
resource_methods = ["GET", "POST"]
schema = {
'product': {
'type': 'objectid',
'required': True,
"product": {
"type": "objectid",
"required": True,
},
'seats': {
'type': 'number',
"seats": {
"type": "number",
},
'link': {
'type': 'boolean',
'nullable': False
}
"link": {"type": "boolean", "nullable": False},
}
datasource = {
"source": "products",
"projection": {
name: 1
for name in ProductsResource.schema.keys() if name != "companies"
}
name: 1 for name in ProductsResource.schema.keys() if name != "companies"
},
}


Expand Down Expand Up @@ -91,15 +104,17 @@ def find_one(self, req, **lookup):
def create(self, docs, **kwargs):
ids = []
for doc in docs:
id = doc.pop('product')
link = doc.pop('link')
product = find_one('products', _id=ObjectId(id))
id = doc.pop("product")
link = doc.pop("link")
product = find_one("products", _id=ObjectId(id))
company = get_company()
assert product
company_products = [p for p in get_company_products(company) if p["_id"] != product["_id"]]
company_products = [
p for p in get_company_products(company) if p["_id"] != product["_id"]
]
if link:
company_products.append(get_product_ref(product, doc.get("seats")))
superdesk.get_resource_service('companies').system_update(
superdesk.get_resource_service("companies").system_update(
company["_id"], {"products": company_products}, company
)
ids.append(id)
Expand All @@ -108,7 +123,11 @@ def create(self, docs, **kwargs):
def on_fetched(self, doc):
for item in doc["_items"]:
self._fix_link(item)
if hasattr(self, "company") and self.company and self.company.get("products"):
if (
hasattr(self, "company")
and self.company
and self.company.get("products")
):
for product_ref in self.company["products"]:
if product_ref["_id"] == item["_id"]:
item["seats"] = product_ref["seats"]
Expand All @@ -120,5 +139,7 @@ def on_fetched_item(self, doc):
return super().on_fetched_item(doc)

def _fix_link(self, item):
company_id = request.view_args['companies']
item["_links"]["self"]["href"] = f"companies/{company_id}/products/{item['_id']}"
company_id = request.view_args["companies"]
item["_links"]["self"][
"href"
] = f"companies/{company_id}/products/{item['_id']}"
30 changes: 30 additions & 0 deletions server/features/mgmt_api_companies.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,33 @@ Feature: Management API - Companies
"""
{"name": "xyz company"}
"""

Scenario: Create a company with default country
Given empty "companies"
When we post to "/companies"
"""
{
"name": "zzz company",
"contact_name": "zzz company",
"contact_email": "[email protected]",
"phone": "99999999"
}
"""
Then we get response code 201
When we get "/companies"
Then we get existing resource
"""
{
"_items" :
[
{
"name": "zzz company",
"contact_name": "zzz company",
"country": "CAN",
"contact_email": "[email protected]",
"phone": "99999999"
}
]
}
"""