Does a creature under the effect of Motivational Speech still get advantage on their next attack if the spell ends for them?

The spell motivational speech (Acquisitions Incorporated, pg. 77) says:

For the duration, each affected creature gains 5 temporary hit points and has advantage on Wisdom saving throws. If an affected creature is hit by an attack, it has advantage on the next attack roll it makes. Once an affected creature loses the temporary hit points granted by this spell, the spell ends for that creature.

So when the effected creature is hit by an attack, the spell grants the creature advantage on their next attack. But getting hit by an attack is going to be accompanied by damage – and 5 points seems like it will very often be gone with a single attack.

Does a creature hit by an attack still get advantage on its next attack if that attack dealt 5 or more damage? Or does the spell end for them immediately and they do not get advantage on the next attack?

It just seems like this effect is going to be self-defeating a lot of the time – the thing that grants the advantage is the thing that takes it away. Am I missing something?

Note, temporary hitpoints are always lost first, so no holding on to them while subtracting damage from your standard hitpoint pool. Sorry Jim, it was a clever thought.