Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

feat(works): add teammate#439

Merged
mydearxym merged 3 commits intodevfromworks-teammate
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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
3 changes: 3 additions & 0 deletionslib/groupher_server/accounts/models/embeds/user_meta.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,7 @@ defmodule GroupherServer.Accounts.Model.Embeds.UserMeta do
@article_threads get_config(:article, :threads)

@general_options %{
is_maker: false,
reported_count: 0,
reported_user_ids: [],
follower_user_ids: [],
Expand All@@ -48,6 +49,8 @@ defmodule GroupherServer.Accounts.Model.Embeds.UserMeta do
end

embedded_schema do
field(:is_maker, :boolean, default: false)

field(:reported_count, :integer, default: 0)
field(:reported_user_ids, {:array, :integer}, default: [])

Expand Down
39 changes: 34 additions & 5 deletionslib/groupher_server/cms/delegates/works_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,8 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
alias CMS.Model.{Community, Techstack, City, Works}
alias Accounts.Model.User

@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()

alias Helper.ORM
alias Ecto.Multi

Expand DownExpand Up@@ -48,21 +50,24 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do

# update works spec fields
defp update_works_fields(%Works{} = works, attrs) do
works = Repo.preload(works, [:techstacks, :cities])
works = Repo.preload(works, [:techstacks, :cities, :teammates])

cover = Map.get(attrs, :cover, works.cover)
desc = Map.get(attrs, :desc, works.desc)
home_link = Map.get(attrs, :home_link, works.home_link)
techstacks = Map.get(attrs, :techstacks, works.techstacks)
teammates = Map.get(attrs, :teammates, works.teammates)
cities = Map.get(attrs, :cities, works.cities)
social_info = Map.get(attrs, :social_info, works.social_info)
app_store = Map.get(attrs, :app_store, works.app_store)

with {:ok, techstacks} <- get_or_create_techstacks(techstacks),
{:ok, cities} <- get_or_create_cities(cities) do
{:ok, cities} <- get_or_create_cities(cities),
{:ok, teammates} <- get_teammates(teammates) do
works
|> Ecto.Changeset.change(%{cover: cover, desc: desc, home_link: home_link})
|> Ecto.Changeset.put_assoc(:techstacks, uniq_by_raw(techstacks))
|> Ecto.Changeset.put_assoc(:teammates, uniq_by_login(teammates))
|> Ecto.Changeset.put_assoc(:cities, uniq_by_raw(cities))
|> Ecto.Changeset.put_embed(:social_info, social_info)
|> Ecto.Changeset.put_embed(:app_store, app_store)
Expand DownExpand Up@@ -109,6 +114,29 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
ORM.create(City, attrs)
end

defp get_teammates([]), do: {:ok, []}

defp get_teammates(teammates) do
teammates
|> Enum.reduce([], fn login, acc ->
with {:ok, teammate} <- ORM.find_by(User, login: login),
{:ok, _} <- set_teammate_flag(teammate) do
acc ++ [teammate]
end
end)
|> done
end

defp set_teammate_flag(%User{meta: nil} = teammate) do
meta = Map.merge(@default_user_meta, %{is_maker: true})
ORM.update_meta(teammate, meta)
end

defp set_teammate_flag(%User{} = teammate) do
meta = Map.merge(teammate.meta, %{is_maker: true})
ORM.update_meta(teammate, meta)
end

defp get_or_create_techstacks([]), do: {:ok, []}

defp get_or_create_techstacks(techstacks) do
Expand DownExpand Up@@ -148,9 +176,10 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
ORM.create(Techstack, attrs)
end

defp uniq_by_raw(list) do
Enum.uniq_by(list, & &1.raw)
end
defp uniq_by_raw([]), do: []
defp uniq_by_raw(list), do: Enum.uniq_by(list, & &1.raw)
defp uniq_by_login([]), do: []
defp uniq_by_login(list), do: Enum.uniq_by(list, & &1.login)

# defp result({:ok, %{create_works: result}}), do: {:ok, result}
defp result({:ok, %{update_works_fields: result}}), do: {:ok, result}
Expand Down
10 changes: 9 additions & 1 deletionlib/groupher_server/cms/models/works.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,8 +8,9 @@ defmodule GroupherServer.CMS.Model.Works do
import Ecto.Changeset
import GroupherServer.CMS.Helper.Macros

alias GroupherServer.CMS
alias GroupherServer.{CMS, Accounts}
alias CMS.Model.{Embeds, Techstack, City}
alias Accounts.Model.User

@timestamps_opts [type: :utc_datetime_usec]

Expand DownExpand Up@@ -42,6 +43,13 @@ defmodule GroupherServer.CMS.Model.Works do
on_replace: :delete
)

many_to_many(
:teammates,
User,
join_through: "works_join_teammates",
on_replace: :delete
)

many_to_many(
:cities,
City,
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@ defmodule GroupherServerWeb.Schema.Account.Types do

object :user_meta do
field(:reported_count, :integer)
field(:is_maker, :boolean)
field(:published_posts_count, :integer)
field(:published_jobs_count, :integer)
field(:published_radars_count, :integer)
Expand Down
1 change: 1 addition & 0 deletionslib/groupher_server_web/schema/cms/cms_types.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,6 +102,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:profit_mode, :string)
field(:working_mode, :string)
field(:cities, list_of(:city), resolve: dataloader(CMS, :cities))
field(:teammates, list_of(:common_user), resolve: dataloader(CMS, :teammates))
field(:techstacks, list_of(:techstack), resolve: dataloader(CMS, :techstacks))
field(:social_info, list_of(:social))
field(:app_store, list_of(:app_store))
Expand Down
2 changes: 2 additions & 0 deletionslib/groupher_server_web/schema/cms/mutations/works.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Works do
arg(:article_tags, list_of(:id))

arg(:techstacks, list_of(:string))
arg(:teammates, list_of(:string))
arg(:cities, list_of(:string))

arg(:profit_mode, :profit_mode)
Expand All@@ -45,6 +46,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Works do
arg(:article_tags, list_of(:id))

arg(:techstacks, list_of(:string))
arg(:teammates, list_of(:string))
arg(:cities, list_of(:string))

arg(:profit_mode, :profit_mode)
Expand Down
12 changes: 12 additions & 0 deletionspriv/repo/migrations/20211109080329_works_join_teammates.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
defmodule GroupherServer.Repo.Migrations.WorksJoinTeammates do
use Ecto.Migration

def change do
create table(:works_join_teammates) do
add(:works_id, references(:cms_works, on_delete: :delete_all), null: false)
add(:user_id, references(:users, on_delete: :delete_all), null: false)
end

create(unique_index(:works_join_teammates, [:works_id, :user_id]))
end
end
19 changes: 17 additions & 2 deletionstest/groupher_server/cms/articles/works_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -45,6 +45,7 @@ defmodule GroupherServer.Test.Articles.Works do
working_mode: "FULLTIME",
techstacks: ["elixir", "React"],
cities: ["chengdu", "xiamen"],
teammates: [user.login],
social_info: social_info,
app_store: app_store
})
Expand All@@ -56,6 +57,8 @@ defmodule GroupherServer.Test.Articles.Works do
assert works.profit_mode == "FREE"
assert works.working_mode == "FULLTIME"

assert works.teammates |> List.first() |> Map.get(:login) === user.login

assert not is_nil(works.social_info)
assert not is_nil(works.app_store)
assert not is_nil(works.cities)
Expand DownExpand Up@@ -93,8 +96,10 @@ defmodule GroupherServer.Test.Articles.Works do
assert techstack.raw == "elixir"
end

test "update works with full attrs", ~m(user works_attrs)a do
test "update works with full attrs", ~m(user user2 works_attrs)a do
works_attrs = works_attrs |> Map.merge(%{teammates: [user.login]})
{:ok, works} = CMS.create_works(works_attrs, user)
assert works.teammates |> length == 1

social_info = [
%{platform: "github", link: "https://github.com/xxx"},
Expand All@@ -107,7 +112,17 @@ defmodule GroupherServer.Test.Articles.Works do
%{platform: "others", link: "https://others.com/xxx"}
]

{:ok, works} = CMS.update_works(works, %{social_info: social_info, app_store: app_store})
teammates = [user.login, user2.login]

{:ok, works} =
CMS.update_works(works, %{
social_info: social_info,
app_store: app_store,
teammates: teammates
})

assert works.teammates |> length == 2

assert not is_nil(works.social_info)
assert not is_nil(works.app_store)
end
Expand Down
27 changes: 24 additions & 3 deletionstest/groupher_server_web/mutation/cms/articles/works_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,12 +3,14 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do

import Helper.Utils, only: [keys_to_atoms: 1, camelize_map_key: 1]
alias Helper.ORM
alias GroupherServer.{CMS, Repo}
alias GroupherServer.{Accounts,CMS, Repo}

alias CMS.Model.Works
alias Accounts.Model.User

setup do
{:ok, user} = db_insert(:user)
{:ok, user2} = db_insert(:user)
{:ok, community} = db_insert(:community, %{raw: "home"})

works_attrs = mock_attrs(:works, %{community_id: community.id})
Expand All@@ -18,7 +20,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
user_conn = simu_conn(:user)
owner_conn = simu_conn(:owner, works)

{:ok, ~m(user_conn guest_conn owner_conn community user works)a}
{:ok, ~m(user_connuser user2guest_conn owner_conn community user works)a}
end

describe "[mutation works curd]" do
Expand All@@ -34,6 +36,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
$workingMode: WorkingMode,
$cities: [String],
$techstacks: [String],
$teammates: [String],
$socialInfo: [SocialInfo],
$appStore: [AppStoreInfo],
$articleTags: [Id]
Expand All@@ -49,6 +52,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
workingMode: $workingMode,
cities: $cities,
techstacks: $techstacks,
teammates: $teammates,
socialInfo: $socialInfo,
appStore: $appStore,
articleTags: $articleTags
Expand All@@ -70,6 +74,11 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
desc
logo
}
teammates {
login
nickname
avatar
}
socialInfo {
platform
link
Expand All@@ -91,7 +100,8 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
}
}
"""
test "create works with valid attrs and make sure author exsit", ~m(community)a do
@tag :wip
test "create works with valid attrs and make sure author exsit", ~m(community user2)a do
{:ok, user} = db_insert(:user)
user_conn = simu_conn(:user, user)

Expand All@@ -104,6 +114,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
workingMode: "FULLTIME",
cities: ["chengdu", "xiamen"],
techstacks: ["elixir", "React"],
teammates: [user.login, user2.login],
socialInfo: [
%{
platform: "TWITTER",
Expand DownExpand Up@@ -137,6 +148,10 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
assert created["cover"] == "cool cover"
assert created["homeLink"] == "homeLink"

assert created["teammates"] |> length == 2
assert user_exist_in?(user, created["teammates"])
assert user_exist_in?(user2, created["teammates"])

assert created["profitMode"] == "FREE"
assert created["workingMode"] == "FULLTIME"
assert created["originalCommunity"]["id"] == to_string(community.id)
Expand All@@ -146,6 +161,12 @@ defmodule GroupherServer.Test.Mutation.Articles.Works do
assert not is_nil(created["appStore"])

assert created["id"] == to_string(found.id)

{:ok, user} = ORM.find(User, user.id)
{:ok, user2} = ORM.find(User, user2.id)

assert user.meta.is_maker
assert user2.meta.is_maker
end

test "create works with valid tags id list", ~m(user_conn user community)a do
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp